4 COMMAND MACRO PLUGIN FOR HOOKE
6 Records, saves and executes batches of commands
7 (c)Alberto Gomez-Casado 2008
10 import libhookecurve as lhc
11 import libinput as linput
25 self.auxprompt=self.prompt
26 self.macrodir=self.config['workdir']
27 if not os.path.exists(os.path.join(self.macrodir,'macros')):
31 print 'Warning: cannot create macros folder.'
32 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
33 self.macrodir=os.path.join(self.macrodir,'macros')
37 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
39 while not(line=='STOP' or line=='PAUSE'):
40 line=raw_input('hooke (macroREC): ')
43 self.prompt='hooke (macroPAUSE): '
46 self.prompt=self.auxprompt
47 self.do_recordmacro('stop')
50 self.currentmacro.pop()
54 #FIXME check if accessing param[2] when it doesnt exist breaks something
55 if param[0] =='export':
56 exportline=param[0]+' __curve__ '
58 exportline=exportline+param[2]
59 self.currentmacro.append(exportline)
66 exportline=exportline+' '+param[2]
67 exportline=exportline+'__curve__'
68 self.currentmacro.append(exportline)
74 self.currentmacro.append(line)
77 def do_recordmacro(self, args):
79 Stores input commands to create script files
81 Syntax: recordmacro [start / stop]
82 If a macro is currently paused start resumes recording
91 self.prompt=self.auxprompt
92 if len(self.currentmacro) != 0:
93 answer=linput.safeinput('Do you want to save this macro? ',['y'])
94 if answer[0].lower() == 'y':
97 print 'Macro discarded'
100 print 'Macro was empty'
108 if len(self.currentmacro) != 0:
109 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
110 if answer[0].lower() == 'y':
111 self.do_savemacro('')
113 print 'Old macro discarded, you can start recording the new one'
119 def do_savemacro(self, macroname):
122 Saves previously recorded macro into a script file for future use
124 Syntax: savemacro [macroname]
125 If no macroname is supplied one will be interactively asked
129 if self.currentmacro==None:
130 print 'No macro is being recorded!'
132 if len(macroname)==0:
133 macroname=linput.safeinput('Enter new macro name: ')
134 if len(macroname) == 0:
137 macroname=os.path.join(self.macrodir,macroname+'.hkm')
138 if os.path.exists(macroname):
139 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
140 if overwrite[0].lower()!='y':
141 print 'Cancelled save'
143 txtfile=open(macroname,'w+')
144 self.currentmacro='\n'.join(self.currentmacro)
145 txtfile.write(self.currentmacro)
147 print 'Saved on '+macroname
150 def do_execmacro (self, args):
153 Loads a macro and executes it over current curve / playlist
155 Syntax: execmacro macroname [playlist] [v]
157 macroname.hkm should be present at [hooke]/macros directory
158 By default the macro will be executed over current curve
159 passing 'playlist' word as second argument executes macroname
161 By default curve(s) will be processed silently, passing 'v'
162 as second/third argument will print each command that is
165 Note that macros applied to playlists should end by export
166 commands so the processed curves are not lost
172 if len(self.currentmacro) != 0:
173 print 'Warning!: you are calling a macro while recording other'
175 print 'You must provide a macro name'
179 #print 'args ' + ' '.join(args)
182 if args[1] == 'playlist':
184 print 'Remember! macros applied over playlists should include export orders'
185 if len(args)>2 and args[2] == 'v':
193 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
194 if not os.path.exists(macropath):
195 print 'Could not find a macro named '+macropath
197 txtfile=open(macropath)
199 #print self.current_list
200 for item in self.current_list:
204 for command in txtfile:
207 print 'Executing command '+command
208 testcmd=command.split()
211 if word=='__curve__':
212 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
214 self.onecmd(' '.join(testcmd))
215 self.current.curve.close_all()
218 for command in txtfile:
219 testcmd=command.split()
222 if word=='__curve__':
224 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
226 print 'Executing command '+' '.join(testcmd)
227 self.onecmd(' '.join(testcmd))