2 COMMAND MACRO PLUGIN FOR HOOKE
4 Records, saves and executes batches of commands
5 (c)Alberto Gomez-Casado 2008
11 from .. import curve as lhc
12 from .. import libinput as linput
14 class macroCommands(object):
24 self.auxprompt=self.prompt
25 self.macrodir=self.config['workdir']
26 if not os.path.exists(os.path.join(self.macrodir,'macros')):
30 print 'Warning: cannot create macros folder.'
31 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
32 self.macrodir=os.path.join(self.macrodir,'macros')
36 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
38 while not(line=='STOP' or line=='PAUSE'):
39 line=raw_input('hooke (macroREC): ')
42 self.prompt='hooke (macroPAUSE): '
45 self.prompt=self.auxprompt
46 self.do_recordmacro('stop')
49 self.currentmacro.pop()
53 #FIXME check if accessing param[2] when it doesnt exist breaks something
54 if param[0] =='export':
55 exportline=param[0]+' __curve__ '
57 exportline=exportline+param[2]
58 self.currentmacro.append(exportline)
65 exportline=exportline+' '+param[2]
66 exportline=exportline+'__curve__'
67 self.currentmacro.append(exportline)
73 self.currentmacro.append(line)
76 def do_recordmacro(self, args):
78 Stores input commands to create script files
80 Syntax: recordmacro [start / stop]
81 If a macro is currently paused start resumes recording
90 self.prompt=self.auxprompt
91 if len(self.currentmacro) != 0:
92 answer=linput.safeinput('Do you want to save this macro? ',['y'])
93 if answer[0].lower() == 'y':
96 print 'Macro discarded'
99 print 'Macro was empty'
107 if len(self.currentmacro) != 0:
108 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
109 if answer[0].lower() == 'y':
110 self.do_savemacro('')
112 print 'Old macro discarded, you can start recording the new one'
118 def do_savemacro(self, macroname):
121 Saves previously recorded macro into a script file for future use
123 Syntax: savemacro [macroname]
124 If no macroname is supplied one will be interactively asked
128 if self.currentmacro==None:
129 print 'No macro is being recorded!'
131 if len(macroname)==0:
132 macroname=linput.safeinput('Enter new macro name: ')
133 if len(macroname) == 0:
136 macroname=os.path.join(self.macrodir,macroname+'.hkm')
137 if os.path.exists(macroname):
138 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
139 if overwrite[0].lower()!='y':
140 print 'Cancelled save'
142 txtfile=open(macroname,'w+')
143 self.currentmacro='\n'.join(self.currentmacro)
144 txtfile.write(self.currentmacro)
146 print 'Saved on '+macroname
149 def do_execmacro (self, args):
152 Loads a macro and executes it over current curve / playlist
154 Syntax: execmacro macroname [playlist] [v]
156 macroname.hkm should be present at [hooke]/macros directory
157 By default the macro will be executed over current curve
158 passing 'playlist' word as second argument executes macroname
160 By default curve(s) will be processed silently, passing 'v'
161 as second/third argument will print each command that is
164 Note that macros applied to playlists should end by export
165 commands so the processed curves are not lost
171 if len(self.currentmacro) != 0:
172 print 'Warning!: you are calling a macro while recording other'
174 print 'You must provide a macro name'
178 #print 'args ' + ' '.join(args)
181 if args[1] == 'playlist':
183 print 'Remember! macros applied over playlists should include export orders'
184 if len(args)>2 and args[2] == 'v':
192 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
193 if not os.path.exists(macropath):
194 print 'Could not find a macro named '+macropath
196 txtfile=open(macropath)
198 #print self.current_list
199 for item in self.current_list:
203 for command in txtfile:
206 print 'Executing command '+command
207 testcmd=command.split()
210 if word=='__curve__':
211 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
213 self.onecmd(' '.join(testcmd))
214 self.current.curve.close_all()
217 for command in txtfile:
218 testcmd=command.split()
221 if word=='__curve__':
223 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
225 print 'Executing command '+' '.join(testcmd)
226 self.onecmd(' '.join(testcmd))