From 6f4ea3baf193df8e8226e65be16e147ab6887104 Mon Sep 17 00:00:00 2001 From: Keegi Date: Mon, 4 Jul 2011 12:02:30 +0300 Subject: [PATCH] command line parsing, -c for overriding .pronsolerc filename/location, -e for commands from command line --- .pronsolerc | 1 + pronsole.py | 63 +++++++++++++++++++++++++++++++++----------------- pronterface.py | 4 +++- 3 files changed, 46 insertions(+), 22 deletions(-) diff --git a/.pronsolerc b/.pronsolerc index 819eaf3..e8028c2 100644 --- a/.pronsolerc +++ b/.pronsolerc @@ -1,4 +1,5 @@ # Sample .pronsolerc file - copy this into your home directory +!print "Loaded " + self.rc_filename macro up move Z 10 macro loud !self.p.loud = 1 macro quiet !self.p.loud = 0 diff --git a/pronsole.py b/pronsole.py index f4ba563..f2ef176 100644 --- a/pronsole.py +++ b/pronsole.py @@ -85,7 +85,9 @@ class pronsole(cmd.Cmd): self.percentdone=0 self.tempreadings="" self.macros={} + self.rc_loaded=False self.processing_rc=False + self.processing_args=False self.settings = Settings() self.settings._port_list = self.scanserial self.settings._temperature_abs_cb = self.set_temp_preset @@ -167,14 +169,15 @@ class pronsole(cmd.Cmd): if not self.processing_rc: print "Macro '"+self.cur_macro_name+"' defined" # save it - macro_key = "macro "+self.cur_macro_name - macro_def = macro_key - if "\n" in self.cur_macro_def: - macro_def += "\n" - else: - macro_def += " " - macro_def += self.cur_macro_def - self.save_in_rc(macro_key,macro_def) + if not self.processing_args: + macro_key = "macro "+self.cur_macro_name + macro_def = macro_key + if "\n" in self.cur_macro_def: + macro_def += "\n" + else: + macro_def += " " + macro_def += self.cur_macro_def + self.save_in_rc(macro_key,macro_def) else: print "Empty macro - cancelled" del self.cur_macro,self.cur_macro_name,self.cur_macro_def @@ -195,7 +198,7 @@ class pronsole(cmd.Cmd): delattr(self.__class__,"do_"+macro_name) del self.macros[macro_name] print "Macro '"+macro_name+"' removed" - if not self.processing_rc: + if not self.processing_rc and not self.processing_args: self.save_in_rc("macro "+macro_name,"") else: print "Macro '"+macro_name+"' is not defined" @@ -243,7 +246,7 @@ class pronsole(cmd.Cmd): try: t = type(getattr(self.settings,var)) value = self.settings._set(var,str) - if not self.processing_rc: + if not self.processing_rc and not self.processing_args: self.save_in_rc("set "+var,"set %s %s" % (var,value)) except AttributeError: print "Unknown variable '%s'" % var @@ -282,24 +285,29 @@ class pronsole(cmd.Cmd): self.p.disconnect() cmd.Cmd.postloop(self) - def load_rc(self,rc_filename=".pronsolerc"): + def load_rc(self,rc_filename): self.processing_rc=True try: - try: - self.rc_filename = os.path.join(os.path.expanduser("~"),rc_filename) - rc=open(self.rc_filename) - except IOError: - rc=open(rc_filename) - self.rc_filename = os.path.abspath(rc_filename) + rc=open(rc_filename) + self.rc_filename = os.path.abspath(rc_filename) for rc_cmd in rc: if not rc_cmd.lstrip().startswith("#"): self.onecmd(rc_cmd) rc.close() + if hasattr(self,"cur_macro"): + self.end_macro() + self.rc_loaded = True + finally: + self.processing_rc=False + + def load_default_rc(self,rc_filename=".pronsolerc"): + try: + try: + self.load_rc(os.path.join(os.path.expanduser("~"),rc_filename)) + except IOError: + self.load_rc(rc_filename) except IOError: pass - if hasattr(self,"cur_macro"): - self.end_macro() - self.processing_rc=False def save_in_rc(self,key,definition): """ @@ -351,7 +359,8 @@ class pronsole(cmd.Cmd): del rci,rco def preloop(self): - self.load_rc() + if not self.rc_loaded: + self.load_default_rc() print "Welcome to the printer console! Type \"help\" for a list of available commands." cmd.Cmd.preloop(self) @@ -1017,10 +1026,22 @@ class pronsole(cmd.Cmd): print "home xyz - homes all axes (Using G1 and G92)" print "home xyze - homes all axes and zeroes the extruder (Using G1 and G92)" + def parse_cmdline(self,args): + import getopt + opts,args = getopt.getopt(args, "c:e:", ["conf=","config="]) + for o,a in opts: + #print repr((o,a)) + if o in ("-c","--conf","--config"): + self.load_rc(a) + elif o == "-e": + self.processing_args = True + self.onecmd(a) + self.processing_args = False if __name__=="__main__": interp=pronsole() + interp.parse_cmdline(sys.argv[1:]) try: interp.cmdloop() except: diff --git a/pronterface.py b/pronterface.py index 99134c6..69b3e85 100644 --- a/pronterface.py +++ b/pronterface.py @@ -83,7 +83,9 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ] self.custombuttons=[] self.btndict={} - self.load_rc(".pronsolerc") + self.parse_cmdline(sys.argv[1:]) + if not self.rc_loaded: + self.load_default_rc(".pronsolerc") customdict={} try: execfile("custombtn.txt",customdict)