3 """Records, saves and executes batches of commands
9 from .. import curve as lhc
10 from .. import libinput as linput
12 class macroCommands(object):
22 self.auxprompt=self.prompt
23 self.macrodir=self.config['workdir']
24 if not os.path.exists(os.path.join(self.macrodir,'macros')):
28 print 'Warning: cannot create macros folder.'
29 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
30 self.macrodir=os.path.join(self.macrodir,'macros')
34 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
36 while not(line=='STOP' or line=='PAUSE'):
37 line=raw_input('hooke (macroREC): ')
40 self.prompt='hooke (macroPAUSE): '
43 self.prompt=self.auxprompt
44 self.do_recordmacro('stop')
47 self.currentmacro.pop()
51 #FIXME check if accessing param[2] when it doesnt exist breaks something
52 if param[0] =='export':
53 exportline=param[0]+' __curve__ '
55 exportline=exportline+param[2]
56 self.currentmacro.append(exportline)
63 exportline=exportline+' '+param[2]
64 exportline=exportline+'__curve__'
65 self.currentmacro.append(exportline)
71 self.currentmacro.append(line)
74 def do_recordmacro(self, args):
76 Stores input commands to create script files
78 Syntax: recordmacro [start / stop]
79 If a macro is currently paused start resumes recording
88 self.prompt=self.auxprompt
89 if len(self.currentmacro) != 0:
90 answer=linput.safeinput('Do you want to save this macro? ',['y'])
91 if answer[0].lower() == 'y':
94 print 'Macro discarded'
97 print 'Macro was empty'
105 if len(self.currentmacro) != 0:
106 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
107 if answer[0].lower() == 'y':
108 self.do_savemacro('')
110 print 'Old macro discarded, you can start recording the new one'
116 def do_savemacro(self, macroname):
119 Saves previously recorded macro into a script file for future use
121 Syntax: savemacro [macroname]
122 If no macroname is supplied one will be interactively asked
126 if self.currentmacro==None:
127 print 'No macro is being recorded!'
129 if len(macroname)==0:
130 macroname=linput.safeinput('Enter new macro name: ')
131 if len(macroname) == 0:
134 macroname=os.path.join(self.macrodir,macroname+'.hkm')
135 if os.path.exists(macroname):
136 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
137 if overwrite[0].lower()!='y':
138 print 'Cancelled save'
140 txtfile=open(macroname,'w+')
141 self.currentmacro='\n'.join(self.currentmacro)
142 txtfile.write(self.currentmacro)
144 print 'Saved on '+macroname
147 def do_execmacro (self, args):
150 Loads a macro and executes it over current curve / playlist
152 Syntax: execmacro macroname [playlist] [v]
154 macroname.hkm should be present at [hooke]/macros directory
155 By default the macro will be executed over current curve
156 passing 'playlist' word as second argument executes macroname
158 By default curve(s) will be processed silently, passing 'v'
159 as second/third argument will print each command that is
162 Note that macros applied to playlists should end by export
163 commands so the processed curves are not lost
169 if len(self.currentmacro) != 0:
170 print 'Warning!: you are calling a macro while recording other'
172 print 'You must provide a macro name'
176 #print 'args ' + ' '.join(args)
179 if args[1] == 'playlist':
181 print 'Remember! macros applied over playlists should include export orders'
182 if len(args)>2 and args[2] == 'v':
190 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
191 if not os.path.exists(macropath):
192 print 'Could not find a macro named '+macropath
194 txtfile=open(macropath)
196 #print self.current_list
197 for item in self.current_list:
201 for command in txtfile:
204 print 'Executing command '+command
205 testcmd=command.split()
208 if word=='__curve__':
209 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
211 self.onecmd(' '.join(testcmd))
212 self.current.curve.close_all()
215 for command in txtfile:
216 testcmd=command.split()
219 if word=='__curve__':
221 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
223 print 'Executing command '+' '.join(testcmd)
224 self.onecmd(' '.join(testcmd))