2 COMMAND MACRO PLUGIN FOR HOOKE
4 Records, saves and executes batches of commands
5 (c)Alberto Gomez-Casado 2008
8 import libhookecurve as lhc
9 import libinput as linput
23 self.auxprompt=self.prompt
24 self.macrodir=self.config['workdir']
25 if not os.path.exists(os.path.join(self.macrodir,'macros')):
29 print 'Warning: cannot create macros folder.'
30 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
31 self.macrodir=os.path.join(self.macrodir,'macros')
35 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
37 while not(line=='STOP' or line=='PAUSE'):
38 line=raw_input('hooke (macroREC): ')
41 self.prompt='hooke (macroPAUSE): '
44 self.prompt=self.auxprompt
45 self.do_recordmacro('stop')
48 self.currentmacro.pop()
52 #FIXME check if accessing param[2] when it doesnt exist breaks something
53 if param[0] =='export':
54 exportline=param[0]+' __curve__ '
56 exportline=exportline+param[2]
57 self.currentmacro.append(exportline)
64 exportline=exportline+' '+param[2]
65 exportline=exportline+'__curve__'
66 self.currentmacro.append(exportline)
72 self.currentmacro.append(line)
75 def do_recordmacro(self, args):
77 Stores input commands to create script files
79 Syntax: recordmacro [start / stop]
80 If a macro is currently paused start resumes recording
89 self.prompt=self.auxprompt
90 if len(self.currentmacro) != 0:
91 answer=linput.safeinput('Do you want to save this macro? ',['y'])
92 if answer[0].lower() == 'y':
95 print 'Macro discarded'
98 print 'Macro was empty'
106 if len(self.currentmacro) != 0:
107 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
108 if answer[0].lower() == 'y':
109 self.do_savemacro('')
111 print 'Old macro discarded, you can start recording the new one'
117 def do_savemacro(self, macroname):
120 Saves previously recorded macro into a script file for future use
122 Syntax: savemacro [macroname]
123 If no macroname is supplied one will be interactively asked
127 if self.currentmacro==None:
128 print 'No macro is being recorded!'
130 if len(macroname)==0:
131 macroname=linput.safeinput('Enter new macro name: ')
132 if len(macroname) == 0:
135 macroname=os.path.join(self.macrodir,macroname+'.hkm')
136 if os.path.exists(macroname):
137 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
138 if overwrite[0].lower()!='y':
139 print 'Cancelled save'
141 txtfile=open(macroname,'w+')
142 self.currentmacro='\n'.join(self.currentmacro)
143 txtfile.write(self.currentmacro)
145 print 'Saved on '+macroname
148 def do_execmacro (self, args):
151 Loads a macro and executes it over current curve / playlist
153 Syntax: execmacro macroname [playlist] [v]
155 macroname.hkm should be present at [hooke]/macros directory
156 By default the macro will be executed over current curve
157 passing 'playlist' word as second argument executes macroname
159 By default curve(s) will be processed silently, passing 'v'
160 as second/third argument will print each command that is
163 Note that macros applied to playlists should end by export
164 commands so the processed curves are not lost
170 if len(self.currentmacro) != 0:
171 print 'Warning!: you are calling a macro while recording other'
173 print 'You must provide a macro name'
177 #print 'args ' + ' '.join(args)
180 if args[1] == 'playlist':
182 print 'Remember! macros applied over playlists should include export orders'
183 if len(args)>2 and args[2] == 'v':
191 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
192 if not os.path.exists(macropath):
193 print 'Could not find a macro named '+macropath
195 txtfile=open(macropath)
197 #print self.current_list
198 for item in self.current_list:
202 for command in txtfile:
205 print 'Executing command '+command
206 testcmd=command.split()
209 if word=='__curve__':
210 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
212 self.onecmd(' '.join(testcmd))
213 self.current.curve.close_all()
216 for command in txtfile:
217 testcmd=command.split()
220 if word=='__curve__':
222 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
224 print 'Executing command '+' '.join(testcmd)
225 self.onecmd(' '.join(testcmd))