Commit 7db2f9b0 authored by Keegi's avatar Keegi

macro work (in progress)

parent b636b316
...@@ -39,9 +39,9 @@ class pronsole(cmd.Cmd): ...@@ -39,9 +39,9 @@ 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)
self.macros=[]
def scanserial(self): def scanserial(self):
"""scan for available ports. return a list of device names.""" """scan for available ports. return a list of device names."""
...@@ -130,57 +130,96 @@ class pronsole(cmd.Cmd): ...@@ -130,57 +130,96 @@ class pronsole(cmd.Cmd):
ls = l.lstrip() ls = l.lstrip()
ws = l[:len(l)-len(ls)] # just leading whitespace ws = l[:len(l)-len(ls)] # just leading whitespace
if len(ws)==0: if len(ws)==0:
del self.onecmd # remove override self.end_macro()
print "Macro '"+self.cur_macro_name+"' defined" # pass the unprocessed line to regular command processor to not require empty line in .pronsolerc
print self.cur_macro # debug
print "------------" # debug
self.prompt="PC>"
exec self.cur_macro
if self.cur_macro_name not in self.macros:
self.macros.append(self.cur_macro_name)
#global 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
# pass the unprocessed line to regular command processor
return self.onecmd(l) return self.onecmd(l)
if ls.startswith('#'): return if ls.startswith('#'): return
if ls.startswith('!'): if ls.startswith('!'):
self.cur_macro += ws + ls[1:] + "\n" # python mode self.cur_macro += ws + ls[1:] + "\n" # python mode
else: else:
self.cur_macro += ws + 'self.onecmd("'+ls+'".format(args))\n' # parametric command mode self.cur_macro += ws + 'self.onecmd("'+ls+'".format(arg))\n' # parametric command mode
self.cur_macro_def += l + "\n"
def do_macro(self,l): def end_macro(self):
if len(l.strip())==0: if self.__dict__.has_key("onecmd"): del self.onecmd # remove override
self.print_topics("User-defined macros",self.macros,15,80) if not self.processing_rc:
return print "Macro '"+self.cur_macro_name+"' defined"
self.cur_macro_name = l.strip() print self.cur_macro+"------------" # debug
if self.cur_macro_name.lower().endswith("/d"): self.prompt="PC>"
self.cur_macro_name = self.cur_macro_name.split()[0] if self.cur_macro_def=="":
self.macros.remove(self.cur_macro_name) print "Empty macro - cancelled"
delattr(self.__class__,"do_"+self.cur_macro_name) del self.cur_macro,self.cur_macro_name,self.cur_macro_def
delattr(self.__class__,"help_"+self.cur_macro_name) return
return self.macros[self.cur_macro_name] = self.cur_macro_def
if self.cur_macro_name not in self.macros and hasattr(self.__class__,"do_"+self.cur_macro_name): exec self.cur_macro
print "Name '"+self.cur_macro_name+"' is already being used by built-in command or alias" setattr(self.__class__,"do_"+self.cur_macro_name,lambda self,largs,macro=macro:macro(self,*largs.split()))
return setattr(self.__class__,"help_"+self.cur_macro_name,lambda self,macro_name=self.cur_macro_name: self.subhelp_macro(macro_name))
print "Enter macro using indented lines, end with empty line" 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:
print "Macro '"+macro_name+"' is not defined"
return
if macro_def.lower() == "/s":
self.subhelp_macro(macro_name)
return
self.cur_macro_def = macro_def
self.cur_macro_name = macro_name
if macro_def.startswith("!"):
self.cur_macro = "def macro(self,*arg):\n "+macro_def[1:]+"\n"
else:
self.cur_macro = "def macro(self,*arg):\n self.onecmd('"+macro_def+"'.format(arg))\n"
self.end_macro()
return
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.onecmd = self.hook_macro # override onecmd temporarily
self.cur_macro = "def macro(self,*args):\n"
self.prompt="..>" self.prompt="..>"
def help_macro(self): def help_macro(self):
print "Define macro: macro <name>" 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 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"
def subhelp_macro(self,macro): print "Delete macro: macro <name> /d"
print "'"+macro+"' is an user-defined macro." 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:
...@@ -188,6 +227,7 @@ class pronsole(cmd.Cmd): ...@@ -188,6 +227,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