1 # Copyright (C) 2008-2010 Alberto Gomez-Casado
2 # Massimo Sandal <devicerandom@gmail.com>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Hooke.
7 # Hooke is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # Hooke is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
15 # Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke. If not, see
19 # <http://www.gnu.org/licenses/>.
21 """Records, saves and executes batches of commands
27 from .. import curve as lhc
28 from .. import libinput as linput
30 class macroCommands(object):
40 self.auxprompt=self.prompt
41 self.macrodir=self.config['workdir']
42 if not os.path.exists(os.path.join(self.macrodir,'macros')):
46 print 'Warning: cannot create macros folder.'
47 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
48 self.macrodir=os.path.join(self.macrodir,'macros')
52 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
54 while not(line=='STOP' or line=='PAUSE'):
55 line=raw_input('hooke (macroREC): ')
58 self.prompt='hooke (macroPAUSE): '
61 self.prompt=self.auxprompt
62 self.do_recordmacro('stop')
65 self.currentmacro.pop()
69 #FIXME check if accessing param[2] when it doesnt exist breaks something
70 if param[0] =='export':
71 exportline=param[0]+' __curve__ '
73 exportline=exportline+param[2]
74 self.currentmacro.append(exportline)
81 exportline=exportline+' '+param[2]
82 exportline=exportline+'__curve__'
83 self.currentmacro.append(exportline)
89 self.currentmacro.append(line)
92 def do_recordmacro(self, args):
94 Stores input commands to create script files
96 Syntax: recordmacro [start / stop]
97 If a macro is currently paused start resumes recording
106 self.prompt=self.auxprompt
107 if len(self.currentmacro) != 0:
108 answer=linput.safeinput('Do you want to save this macro? ',['y'])
109 if answer[0].lower() == 'y':
110 self.do_savemacro('')
112 print 'Macro discarded'
115 print 'Macro was empty'
123 if len(self.currentmacro) != 0:
124 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
125 if answer[0].lower() == 'y':
126 self.do_savemacro('')
128 print 'Old macro discarded, you can start recording the new one'
134 def do_savemacro(self, macroname):
137 Saves previously recorded macro into a script file for future use
139 Syntax: savemacro [macroname]
140 If no macroname is supplied one will be interactively asked
144 if self.currentmacro==None:
145 print 'No macro is being recorded!'
147 if len(macroname)==0:
148 macroname=linput.safeinput('Enter new macro name: ')
149 if len(macroname) == 0:
152 macroname=os.path.join(self.macrodir,macroname+'.hkm')
153 if os.path.exists(macroname):
154 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
155 if overwrite[0].lower()!='y':
156 print 'Cancelled save'
158 txtfile=open(macroname,'w+')
159 self.currentmacro='\n'.join(self.currentmacro)
160 txtfile.write(self.currentmacro)
162 print 'Saved on '+macroname
165 def do_execmacro (self, args):
168 Loads a macro and executes it over current curve / playlist
170 Syntax: execmacro macroname [playlist] [v]
172 macroname.hkm should be present at [hooke]/macros directory
173 By default the macro will be executed over current curve
174 passing 'playlist' word as second argument executes macroname
176 By default curve(s) will be processed silently, passing 'v'
177 as second/third argument will print each command that is
180 Note that macros applied to playlists should end by export
181 commands so the processed curves are not lost
187 if len(self.currentmacro) != 0:
188 print 'Warning!: you are calling a macro while recording other'
190 print 'You must provide a macro name'
194 #print 'args ' + ' '.join(args)
197 if args[1] == 'playlist':
199 print 'Remember! macros applied over playlists should include export orders'
200 if len(args)>2 and args[2] == 'v':
208 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
209 if not os.path.exists(macropath):
210 print 'Could not find a macro named '+macropath
212 txtfile=open(macropath)
214 #print self.current_list
215 for item in self.current_list:
219 for command in txtfile:
222 print 'Executing command '+command
223 testcmd=command.split()
226 if word=='__curve__':
227 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
229 self.onecmd(' '.join(testcmd))
230 self.current.curve.close_all()
233 for command in txtfile:
234 testcmd=command.split()
237 if word=='__curve__':
239 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
241 print 'Executing command '+' '.join(testcmd)
242 self.onecmd(' '.join(testcmd))