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

master
kliment 2011-07-15 12:11:48 +02:00
commit bfe99db55e
2 changed files with 89 additions and 7 deletions

View File

@ -182,6 +182,15 @@ class pronsole(cmd.Cmd):
print "Empty macro - cancelled"
del self.cur_macro,self.cur_macro_name,self.cur_macro_def
def start_macro(self,macro_name,prev_definition="",suppress_instructions=False):
if not self.processing_rc and not suppress_instructions:
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 do_macro(self,args):
if args.strip()=="":
self.print_topics("User-defined macros",self.macros.keys(),15,80)
@ -214,13 +223,10 @@ class pronsole(cmd.Cmd):
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.prompt="..>"
if self.macros.has_key(macro_name):
self.start_macro(macro_name,self.macros[macro_name])
else:
self.start_macro(macro_name)
def help_macro(self):
print "Define single-line macro: macro <name> <definition>"

View File

@ -178,6 +178,28 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
except:
print "You must enter a temperature."
def start_macro(self,macro_name,old_macro_definition=""):
if not self.processing_rc:
def cb(definition):
if "\n" not in definition and len(definition.strip())>0:
macro_def = definition.strip()
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
pronsole.pronsole.start_macro(self,macro_name,True)
for line in definition.split("\n"):
if hasattr(self,"cur_macro_def"):
self.hook_macro(line)
if hasattr(self,"cur_macro_def"):
self.end_macro()
macroed(macro_name,old_macro_definition,cb)
else:
pronsole.pronsole.start_macro(self,macro_name,old_macro_definition)
def catchprint(self,l):
wx.CallAfter(self.logbox.AppendText,l)
@ -831,6 +853,60 @@ class PronterWindow(wx.Frame,pronsole.pronsole):
self.printbtn.SetLabel("Print")
self.paused=0
class macroed(wx.Frame):
"""Really simple editor to edit macro definitions"""
def __init__(self,macro_name,definition,callback):
self.indent_chars = " "
wx.Frame.__init__(self,None,title="macro %s" % macro_name)
self.callback = callback
self.panel=wx.Panel(self,-1)
titlesizer=wx.BoxSizer(wx.HORIZONTAL)
title = wx.StaticText(self.panel,-1," macro %s: "%macro_name)
title.SetFont(wx.Font(11,wx.NORMAL,wx.NORMAL,wx.BOLD))
titlesizer.Add(title,1)
self.okb = wx.Button(self.panel,-1,"Save")
self.okb.Bind(wx.EVT_BUTTON,self.save)
titlesizer.Add(self.okb)
self.cancelb = wx.Button(self.panel,-1,"Cancel")
self.cancelb.Bind(wx.EVT_BUTTON,self.close)
titlesizer.Add(self.cancelb)
topsizer=wx.BoxSizer(wx.VERTICAL)
topsizer.Add(titlesizer,0,wx.EXPAND)
self.e=wx.TextCtrl(self.panel,style=wx.TE_MULTILINE+wx.HSCROLL,size=(200,200))
self.e.SetValue(self.unindent(definition))
topsizer.Add(self.e,1,wx.ALL+wx.EXPAND)
self.panel.SetSizer(topsizer)
topsizer.Layout()
topsizer.Fit(self)
self.Show()
def save(self,ev):
self.Close()
self.callback(self.reindent(self.e.GetValue()))
def close(self,ev):
self.Close()
def unindent(self,text):
import re
self.indent_chars = text[:len(text)-len(text.lstrip())]
unindented = ""
lines = re.split(r"(?:\r\n?|\n)",text)
#print lines
if len(lines) <= 1:
return text
for line in lines:
if line.startswith(self.indent_chars):
unindented += line[len(self.indent_chars):] + "\n"
else:
unindented += line + "\n"
return unindented
def reindent(self,text):
import re
lines = re.split(r"(?:\r\n?|\n)",text)
if len(lines) <= 1:
return text
reindented = ""
for line in lines:
reindented += self.indent_chars + line + "\n"
return reindented
if __name__ == '__main__':