Commit d2319d8f authored by kliment's avatar kliment

Merge branch 'master' of github.com:kliment/Printrun

parents 4ed07aef f0168e10
...@@ -39,7 +39,8 @@ class pronsole(cmd.Cmd): ...@@ -39,7 +39,8 @@ class pronsole(cmd.Cmd):
self.bedtemps={"pla":"60","abs":"110","off":"0"} self.bedtemps={"pla":"60","abs":"110","off":"0"}
self.percentdone=0 self.percentdone=0
self.tempreadings="" self.tempreadings=""
self.aliases={} self.macros={}
self.processing_rc=False
self.lastport = (None,None) self.lastport = (None,None)
def scanserial(self): def scanserial(self):
...@@ -71,65 +72,108 @@ class pronsole(cmd.Cmd): ...@@ -71,65 +72,108 @@ class pronsole(cmd.Cmd):
def help_gcodes(self): def help_gcodes(self):
print "Gcodes are passed through to the printer as they are" print "Gcodes are passed through to the printer as they are"
def do_alias(self,l): def complete_macro(self,text,line,begidx,endidx):
if l == "": if (len(line.split())==2 and line[-1] != " ") or (len(line.split())==1 and line[-1]==" "):
# list aliases return [i for i in self.macros.keys() if i.startswith(text)]
if len(self.aliases): elif(len(line.split())==3 or (len(line.split())==2 and line[-1]==" ")):
self.print_topics("Aliases, to display type: alias <name>",self.aliases.keys(),15,80) return ["/D", "/S"] + self.completenames(text)
else: else:
print "No aliases defined, to define see: help alias" return []
return
alias_l = l.split(None,1) def hook_macro(self,l):
alias_name = alias_l[0] ls = l.lstrip()
if len(alias_l) < 2: ws = l[:len(l)-len(ls)] # just leading whitespace
# display alias if len(ws)==0:
if alias_name in self.aliases.keys(): self.end_macro()
print "Alias '"+alias_name+"' stands for '"+self.aliases[alias_name]+"'" # pass the unprocessed line to regular command processor to not require empty line in .pronsolerc
return self.onecmd(l)
if ls.startswith('#'): return
if ls.startswith('!'):
self.cur_macro += ws + ls[1:] + "\n" # python mode
else: else:
print "Alias '"+alias_name+"' is not defined" self.cur_macro += ws + 'self.onecmd("'+ls+'".format(arg))\n' # parametric command mode
return self.cur_macro_def += l + "\n"
alias_name,alias_def = alias_l
if alias_def.lower() == "/d": def end_macro(self):
# delete alias if self.__dict__.has_key("onecmd"): del self.onecmd # remove override
if alias_name in self.aliases.keys(): if not self.processing_rc:
delattr(self.__class__,"do_"+alias_name) print "Macro '"+self.cur_macro_name+"' defined"
del self.aliases[alias_name] #print self.cur_macro+"------------" # debug
print "Alias '"+alias_name+"' removed" self.prompt="PC>"
return if self.cur_macro_def=="":
print "Empty macro - cancelled"
del self.cur_macro,self.cur_macro_name,self.cur_macro_def
return
self.macros[self.cur_macro_name] = self.cur_macro_def
exec self.cur_macro
setattr(self.__class__,"do_"+self.cur_macro_name,lambda self,largs,macro=macro:macro(self,*largs.split()))
setattr(self.__class__,"help_"+self.cur_macro_name,lambda self,macro_name=self.cur_macro_name: self.subhelp_macro(macro_name))
del self.cur_macro,self.cur_macro_name,self.cur_macro_def
def do_macro(self,args):
if args.strip()=="":
self.print_topics("User-defined macros",self.macros.keys(),15,80)
return
arglist = args.split(None,1)
macro_name = arglist[0]
if macro_name not in self.macros and hasattr(self.__class__,"do_"+macro_name):
print "Name '"+macro_name+"' is being used by built-in command"
return
if len(arglist) == 2:
macro_def = arglist[1]
if macro_def.lower() == "/d":
if macro_name in self.macros.keys():
delattr(self.__class__,"do_"+macro_name)
del self.macros[macro_name]
print "Macro '"+macro_name+"' removed"
else: else:
print "Alias '"+alias_name+"' is not defined" print "Macro '"+macro_name+"' is not defined"
return return
# (re)define an alias if macro_def.lower() == "/s":
if alias_name not in self.aliases.keys() and hasattr(self.__class__,"do_"+alias_name): self.subhelp_macro(macro_name)
print "Name '"+alias_name+"' is already being used by built-in command"
return return
func = lambda self,args,alias_def=alias_def: self.onecmd(" ".join((alias_def,args))) self.cur_macro_def = macro_def
self.aliases[alias_name] = alias_def self.cur_macro_name = macro_name
setattr(self.__class__,"do_"+alias_name,func) if macro_def.startswith("!"):
setattr(self.__class__,"help_"+alias_name,lambda self=self,alias_name=alias_name: self.subhelp_alias(alias_name)) self.cur_macro = "def macro(self,*arg):\n "+macro_def[1:]+"\n"
def help_alias(self):
print "Create/modify/view aliases: alias <name> [<command>]"
print "if <command> is not specified, displays the alias definition"
print "without arguments, displays list of all defined aliases"
print "To remove an alias: alias <name> /d"
def complete_alias(self,text,line,begidx,endidx):
if (len(line.split())==2 and line[-1] != " ") or (len(line.split())==1 and line[-1]==" "):
return [i for i in self.aliases.keys() if i.startswith(text)]
elif(len(line.split())==3 or (len(line.split())==2 and line[-1]==" ")):
return self.completenames(text)
else: else:
return [] self.cur_macro = "def macro(self,*arg):\n self.onecmd('"+macro_def+"'.format(arg))\n"
self.end_macro()
def subhelp_alias(self,alias_name): return
print "'"+alias_name+"' is alias for '"+self.aliases[alias_name]+"'" if not self.processing_rc:
print "Enter macro using indented lines, end with empty line"
self.cur_macro_name = macro_name
self.cur_macro_def = ""
self.cur_macro = "def macro(self,*arg):\n"
self.onecmd = self.hook_macro # override onecmd temporarily
self.prompt="..>"
def help_macro(self):
print "Define single-line macro: macro <name> <definition>"
print "Define multi-line macro: macro <name>"
print "Enter macro definition in indented lines. Use {0} .. {N} to substitute macro arguments"
print "Enter python code, prefixed with ! Use arg[0] .. arg[N] to substitute macro arguments"
print "Delete macro: macro <name> /d"
print "Show macro definition: macro <name> /s"
print "'macro' without arguments displays list of defined macros"
def subhelp_macro(self,macro_name):
if macro_name in self.macros.keys():
macro_def = self.macros[macro_name]
if "\n" in macro_def:
print "Macro '"+macro_name+"' defined as:"
print self.macros[macro_name]+"----------------"
else:
print "Macro '"+macro_name+"' defined as: '"+macro_def+"'"
else:
print "Macro '"+macro_name+"' is not defined"
def postloop(self): def postloop(self):
self.p.disconnect() self.p.disconnect()
cmd.Cmd.postloop(self) cmd.Cmd.postloop(self)
def preloop(self): def preloop(self):
self.processing_rc=True
try: try:
with open(os.path.join(os.path.expanduser("~"),".pronsolerc")) as rc: with open(os.path.join(os.path.expanduser("~"),".pronsolerc")) as rc:
for rc_cmd in rc: for rc_cmd in rc:
...@@ -137,6 +181,7 @@ class pronsole(cmd.Cmd): ...@@ -137,6 +181,7 @@ class pronsole(cmd.Cmd):
self.onecmd(rc_cmd) self.onecmd(rc_cmd)
except IOError: except IOError:
pass pass
self.processing_rc=False
print "Welcome to the printer console! Type \"help\" for a list of available commands." print "Welcome to the printer console! Type \"help\" for a list of available commands."
cmd.Cmd.preloop(self) cmd.Cmd.preloop(self)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment