Commit 6f4ea3ba authored by Keegi's avatar Keegi

command line parsing, -c for overriding .pronsolerc filename/location, -e for...

command line parsing, -c for overriding .pronsolerc filename/location, -e for commands from command line
parent 72c68030
# Sample .pronsolerc file - copy this into your home directory # Sample .pronsolerc file - copy this into your home directory
!print "Loaded " + self.rc_filename
macro up move Z 10 macro up move Z 10
macro loud !self.p.loud = 1 macro loud !self.p.loud = 1
macro quiet !self.p.loud = 0 macro quiet !self.p.loud = 0
...@@ -85,7 +85,9 @@ class pronsole(cmd.Cmd): ...@@ -85,7 +85,9 @@ class pronsole(cmd.Cmd):
self.percentdone=0 self.percentdone=0
self.tempreadings="" self.tempreadings=""
self.macros={} self.macros={}
self.rc_loaded=False
self.processing_rc=False self.processing_rc=False
self.processing_args=False
self.settings = Settings() self.settings = Settings()
self.settings._port_list = self.scanserial self.settings._port_list = self.scanserial
self.settings._temperature_abs_cb = self.set_temp_preset self.settings._temperature_abs_cb = self.set_temp_preset
...@@ -167,6 +169,7 @@ class pronsole(cmd.Cmd): ...@@ -167,6 +169,7 @@ class pronsole(cmd.Cmd):
if not self.processing_rc: if not self.processing_rc:
print "Macro '"+self.cur_macro_name+"' defined" print "Macro '"+self.cur_macro_name+"' defined"
# save it # save it
if not self.processing_args:
macro_key = "macro "+self.cur_macro_name macro_key = "macro "+self.cur_macro_name
macro_def = macro_key macro_def = macro_key
if "\n" in self.cur_macro_def: if "\n" in self.cur_macro_def:
...@@ -195,7 +198,7 @@ class pronsole(cmd.Cmd): ...@@ -195,7 +198,7 @@ class pronsole(cmd.Cmd):
delattr(self.__class__,"do_"+macro_name) delattr(self.__class__,"do_"+macro_name)
del self.macros[macro_name] del self.macros[macro_name]
print "Macro '"+macro_name+"' removed" 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,"") self.save_in_rc("macro "+macro_name,"")
else: else:
print "Macro '"+macro_name+"' is not defined" print "Macro '"+macro_name+"' is not defined"
...@@ -243,7 +246,7 @@ class pronsole(cmd.Cmd): ...@@ -243,7 +246,7 @@ class pronsole(cmd.Cmd):
try: try:
t = type(getattr(self.settings,var)) t = type(getattr(self.settings,var))
value = self.settings._set(var,str) 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)) self.save_in_rc("set "+var,"set %s %s" % (var,value))
except AttributeError: except AttributeError:
print "Unknown variable '%s'" % var print "Unknown variable '%s'" % var
...@@ -282,25 +285,30 @@ class pronsole(cmd.Cmd): ...@@ -282,25 +285,30 @@ class pronsole(cmd.Cmd):
self.p.disconnect() self.p.disconnect()
cmd.Cmd.postloop(self) cmd.Cmd.postloop(self)
def load_rc(self,rc_filename=".pronsolerc"): def load_rc(self,rc_filename):
self.processing_rc=True self.processing_rc=True
try: try:
try:
self.rc_filename = os.path.join(os.path.expanduser("~"),rc_filename)
rc=open(self.rc_filename)
except IOError:
rc=open(rc_filename) rc=open(rc_filename)
self.rc_filename = os.path.abspath(rc_filename) self.rc_filename = os.path.abspath(rc_filename)
for rc_cmd in rc: for rc_cmd in rc:
if not rc_cmd.lstrip().startswith("#"): if not rc_cmd.lstrip().startswith("#"):
self.onecmd(rc_cmd) self.onecmd(rc_cmd)
rc.close() rc.close()
except IOError:
pass
if hasattr(self,"cur_macro"): if hasattr(self,"cur_macro"):
self.end_macro() self.end_macro()
self.rc_loaded = True
finally:
self.processing_rc=False 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
def save_in_rc(self,key,definition): def save_in_rc(self,key,definition):
""" """
Saves or updates macro or other definitions in .pronsolerc Saves or updates macro or other definitions in .pronsolerc
...@@ -351,7 +359,8 @@ class pronsole(cmd.Cmd): ...@@ -351,7 +359,8 @@ class pronsole(cmd.Cmd):
del rci,rco del rci,rco
def preloop(self): 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." print "Welcome to the printer console! Type \"help\" for a list of available commands."
cmd.Cmd.preloop(self) cmd.Cmd.preloop(self)
...@@ -1017,10 +1026,22 @@ class pronsole(cmd.Cmd): ...@@ -1017,10 +1026,22 @@ class pronsole(cmd.Cmd):
print "home xyz - homes all axes (Using G1 and G92)" print "home xyz - homes all axes (Using G1 and G92)"
print "home xyze - homes all axes and zeroes the extruder (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__": if __name__=="__main__":
interp=pronsole() interp=pronsole()
interp.parse_cmdline(sys.argv[1:])
try: try:
interp.cmdloop() interp.cmdloop()
except: except:
......
...@@ -83,7 +83,9 @@ class PronterWindow(wx.Frame,pronsole.pronsole): ...@@ -83,7 +83,9 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
] ]
self.custombuttons=[] self.custombuttons=[]
self.btndict={} self.btndict={}
self.load_rc(".pronsolerc") self.parse_cmdline(sys.argv[1:])
if not self.rc_loaded:
self.load_default_rc(".pronsolerc")
customdict={} customdict={}
try: try:
execfile("custombtn.txt",customdict) execfile("custombtn.txt",customdict)
......
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