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 """The ``command_stack`` module provides :class:`CommandStackPlugin`
22 and several associated :class:`~hooke.command.Command`\s for managing
23 stacks of :class:`~hooke.engine.CommandMessage`\s and applying those
24 stacks to :class:`~hooke.curve.Curve`\s.
30 from .. import curve as lhc
31 from .. import libinput as linput
33 class macroCommands(object):
43 self.auxprompt=self.prompt
44 self.macrodir=self.config['workdir']
45 if not os.path.exists(os.path.join(self.macrodir,'macros')):
49 print 'Warning: cannot create macros folder.'
50 print 'Probably you do not have permissions in your Hooke folder, use macro at your own risk.'
51 self.macrodir=os.path.join(self.macrodir,'macros')
55 print 'Enter STOP / PAUSE to go back to normal mode\nUNDO to remove last command'
57 while not(line=='STOP' or line=='PAUSE'):
58 line=raw_input('hooke (macroREC): ')
61 self.prompt='hooke (macroPAUSE): '
64 self.prompt=self.auxprompt
65 self.do_recordmacro('stop')
68 self.currentmacro.pop()
72 #FIXME check if accessing param[2] when it doesnt exist breaks something
73 if param[0] =='export':
74 exportline=param[0]+' __curve__ '
76 exportline=exportline+param[2]
77 self.currentmacro.append(exportline)
84 exportline=exportline+' '+param[2]
85 exportline=exportline+'__curve__'
86 self.currentmacro.append(exportline)
92 self.currentmacro.append(line)
95 def do_recordmacro(self, args):
97 Stores input commands to create script files
99 Syntax: recordmacro [start / stop]
100 If a macro is currently paused start resumes recording
109 self.prompt=self.auxprompt
110 if len(self.currentmacro) != 0:
111 answer=linput.safeinput('Do you want to save this macro? ',['y'])
112 if answer[0].lower() == 'y':
113 self.do_savemacro('')
115 print 'Macro discarded'
118 print 'Macro was empty'
126 if len(self.currentmacro) != 0:
127 answer=linput.safeinput('Another macro is already beign recorded\nDo you want to save it?',['y'])
128 if answer[0].lower() == 'y':
129 self.do_savemacro('')
131 print 'Old macro discarded, you can start recording the new one'
137 def do_savemacro(self, macroname):
140 Saves previously recorded macro into a script file for future use
142 Syntax: savemacro [macroname]
143 If no macroname is supplied one will be interactively asked
147 if self.currentmacro==None:
148 print 'No macro is being recorded!'
150 if len(macroname)==0:
151 macroname=linput.safeinput('Enter new macro name: ')
152 if len(macroname) == 0:
155 macroname=os.path.join(self.macrodir,macroname+'.hkm')
156 if os.path.exists(macroname):
157 overwrite=linput.safeinput('That name is in use, overwrite?',['n'])
158 if overwrite[0].lower()!='y':
159 print 'Cancelled save'
161 txtfile=open(macroname,'w+')
162 self.currentmacro='\n'.join(self.currentmacro)
163 txtfile.write(self.currentmacro)
165 print 'Saved on '+macroname
168 def do_execmacro (self, args):
171 Loads a macro and executes it over current curve / playlist
173 Syntax: execmacro macroname [playlist] [v]
175 macroname.hkm should be present at [hooke]/macros directory
176 By default the macro will be executed over current curve
177 passing 'playlist' word as second argument executes macroname
179 By default curve(s) will be processed silently, passing 'v'
180 as second/third argument will print each command that is
183 Note that macros applied to playlists should end by export
184 commands so the processed curves are not lost
190 if len(self.currentmacro) != 0:
191 print 'Warning!: you are calling a macro while recording other'
193 print 'You must provide a macro name'
197 #print 'args ' + ' '.join(args)
200 if args[1] == 'playlist':
202 print 'Remember! macros applied over playlists should include export orders'
203 if len(args)>2 and args[2] == 'v':
211 macropath=os.path.join(self.macrodir,args[0]+'.hkm')
212 if not os.path.exists(macropath):
213 print 'Could not find a macro named '+macropath
215 txtfile=open(macropath)
217 #print self.current_list
218 for item in self.current_list:
222 for command in txtfile:
225 print 'Executing command '+command
226 testcmd=command.split()
229 if word=='__curve__':
230 testcmd[w]=os.path.splitext(os.path.basename(item.path))[0]
232 self.onecmd(' '.join(testcmd))
233 self.current.curve.close_all()
236 for command in txtfile:
237 testcmd=command.split()
240 if word=='__curve__':
242 testcmd[w]=os.path.splitext(os.path.basename(self.current.path))[0]+'-'+string.lstrip(os.path.splitext(os.path.basename(self.current.path))[1],'.')
244 print 'Executing command '+' '.join(testcmd)
245 self.onecmd(' '.join(testcmd))