comparison cmd2.py @ 0:febfdc79550b

moved repository to Assembla
author catherine@DellZilla.myhome.westell.com
date Wed, 05 Mar 2008 12:16:19 -0500
parents
children 1ea887b51cad
comparison
equal deleted inserted replaced
-1:000000000000 0:febfdc79550b
1 """Variant on standard library's cmd with extra features.
2
3 To use, simply import cmd2.Cmd instead of cmd.Cmd; use precisely as though you
4 were using the standard library's cmd, while enjoying the extra features.
5
6 Searchable command history (commands: "hi", "li", "run")
7 Load commands from file, save to file, edit commands in file
8 Multi-line commands
9 Case-insensitive commands
10 Special-character shortcut commands (beyond cmd's "@" and "!")
11 Settable environment parameters
12 Parsing commands with flags
13 Redirection to file with >, >>; input from file with <
14 """
15
16 """
17 todo:
18 edited commands end with "EOF". Hmm.
19 example of flag usage
20
21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com
22 """
23 import cmd, re, os, sys
24 import flagReader
25
26 class Cmd(cmd.Cmd):
27 caseInsensitive = True
28 multilineCommands = []
29 continuationPrompt = '> '
30 shortcuts = {'?': 'help', '!': 'shell', '@': 'load'}
31 excludeFromHistory = '''run r list l history hi ed li eof'''.split()
32 defaultExtension = 'txt'
33 defaultFileName = 'command.txt'
34 editor = os.environ.get('EDITOR')
35 if not editor:
36 if sys.platform[:3] == 'win':
37 editor = 'notepad'
38 else:
39 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']:
40 if not os.system('which %s' % (editor)):
41 break
42
43 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive']
44 terminators = ';\n'
45 def do_cmdenvironment(self, args):
46 self.stdout.write("""
47 Commands are %(casesensitive)scase-sensitive.
48 Commands may be terminated with: %(terminators)s
49 Settable parameters: %(settable)s
50 """ %
51 { 'casesensitive': 'not ' if self.caseInsensitive else '',
52 'terminators': ' '.join(self.terminators),
53 'settable': ' '.join(self.settable)
54 })
55
56 def __init__(self, *args, **kwargs):
57 cmd.Cmd.__init__(self, *args, **kwargs)
58 self.history = History()
59
60 def do_shortcuts(self, args):
61 """Lists single-key shortcuts available."""
62 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
63 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
64
65 legalFileName = re.compile(r'''^[^"'\s]+$''')
66 def parseRedirector(self, statement, symbol):
67 parts = statement.split(symbol)
68 if len(parts) < 2:
69 return statement, None
70 (newStatement, redirect) = (' '.join(parts[:-1]), parts[-1].strip())
71 if not self.legalFileName.search(redirect):
72 return statement, None
73 return newStatement, redirect
74
75 def parseRedirectors(self, statement):
76 newStatement, redirect = self.parseRedirector(statement, '>>')
77 if redirect:
78 return newStatement, redirect, 'a'
79 newStatement, redirect = self.parseRedirector(statement, '>')
80 if redirect:
81 return newStatement, redirect, 'w'
82 newStatement, redirect = self.parseRedirector(statement, '<')
83 if redirect:
84 return newStatement, redirect, 'r'
85 return statement, '', ''
86
87 def onecmd(self, line):
88 """Interpret the argument as though it had been typed in response
89 to the prompt.
90
91 This may be overridden, but should not normally need to be;
92 see the precmd() and postcmd() methods for useful execution hooks.
93 The return value is a flag indicating whether interpretation of
94 commands by the interpreter should stop.
95
96 """
97 try:
98 (command, args) = line.split(None,1)
99 except ValueError:
100 (command, args) = line, ''
101 if self.caseInsensitive:
102 command = command.lower()
103 statement = ' '.join([command, args])
104 if command in self.multilineCommands:
105 statement = self.finishStatement(statement)
106 statekeeper = None
107 statement, redirect, mode = self.parseRedirectors(statement)
108 if redirect:
109 if mode in ('w','a'):
110 statekeeper = Statekeeper(self, ('stdout',))
111 self.stdout = open(redirect, mode)
112 else:
113 statement = '%s %s' % (statement, self.fileimport(statement=statement, source=redirect))
114 stop = cmd.Cmd.onecmd(self, statement)
115 try:
116 command = statement.split(None,1)[0].lower()
117 if command not in self.excludeFromHistory:
118 self.history.append(statement)
119 finally:
120 if statekeeper:
121 self.stdout.close()
122 statekeeper.restore()
123 return stop
124
125 statementEndPattern = re.compile(r'[%s]\s*$' % terminators)
126 def statementHasEnded(self, lines):
127 return bool(self.statementEndPattern.search(lines)) \
128 or lines[-3:] == 'EOF' \
129 or self.parseRedirectors(lines)[1]
130
131 def finishStatement(self, firstline):
132 statement = firstline
133 while not self.statementHasEnded(statement):
134 inp = self.pseudo_raw_input(self.continuationPrompt)
135 statement = '%s\n%s' % (statement, inp)
136 return statement
137 # assembling a list of lines and joining them at the end would be faster,
138 # but statementHasEnded needs a string arg; anyway, we're getting
139 # user input and users are slow.
140
141 def pseudo_raw_input(self, prompt):
142 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
143
144 if self.use_rawinput:
145 try:
146 line = raw_input(prompt)
147 except EOFError:
148 line = 'EOF'
149 else:
150 self.stdout.write(prompt)
151 self.stdout.flush()
152 line = self.stdin.readline()
153 if not len(line):
154 line = 'EOF'
155 else:
156 if line[-1] == '\n': # this was always true in Cmd
157 line = line[:-1]
158 return line
159
160 def cmdloop(self, intro=None):
161 """Repeatedly issue a prompt, accept input, parse an initial prefix
162 off the received input, and dispatch to action methods, passing them
163 the remainder of the line as argument.
164 """
165
166 # An almost perfect copy from Cmd; however, the pseudo_raw_input portion
167 # has been split out so that it can be called separately
168
169 self.preloop()
170 if self.use_rawinput and self.completekey:
171 try:
172 import readline
173 self.old_completer = readline.get_completer()
174 readline.set_completer(self.complete)
175 readline.parse_and_bind(self.completekey+": complete")
176 except ImportError:
177 pass
178 try:
179 if intro is not None:
180 self.intro = intro
181 if self.intro:
182 self.stdout.write(str(self.intro)+"\n")
183 stop = None
184 while not stop:
185 if self.cmdqueue:
186 line = self.cmdqueue.pop(0)
187 else:
188 line = self.pseudo_raw_input(self.prompt)
189 line = self.precmd(line)
190 stop = self.onecmd(line)
191 stop = self.postcmd(stop, line)
192 self.postloop()
193 finally:
194 if self.use_rawinput and self.completekey:
195 try:
196 import readline
197 readline.set_completer(self.old_completer)
198 except ImportError:
199 pass
200
201 def do_EOF(self, arg):
202 return True
203 do_eof = do_EOF
204
205 def clean(self, s):
206 """cleans up a string"""
207 if self.caseInsensitive:
208 return s.strip().lower()
209 return s.strip()
210
211 def parseline(self, line):
212 """Parse the line into a command name and a string containing
213 the arguments. Returns a tuple containing (command, args, line).
214 'command' and 'args' may be None if the line couldn't be parsed.
215 """
216 line = line.strip()
217 if not line:
218 return None, None, line
219 shortcut = self.shortcuts.get(line[0])
220 if shortcut and hasattr(self, 'do_%s' % shortcut):
221 line = '%s %s' % (shortcut, line[1:])
222 i, n = 0, len(line)
223 while i < n and line[i] in self.identchars: i = i+1
224 cmd, arg = line[:i], line[i:].strip().strip(self.terminators)
225 return cmd, arg, line
226
227 def showParam(self, param):
228 param = self.clean(param)
229 if param in self.settable:
230 val = getattr(self, param)
231 self.stdout.write('%s: %s\n' % (param, str(getattr(self, param))))
232
233 def do_quit(self, arg):
234 return 1
235 do_exit = do_quit
236 do_q = do_quit
237
238 def do_show(self, arg):
239 'Shows value of a parameter'
240 if arg.strip():
241 self.showParam(arg)
242 else:
243 for param in self.settable:
244 self.showParam(param)
245
246 def do_set(self, arg):
247 'Sets a parameter'
248 try:
249 paramName, val = arg.split(None, 1)
250 paramName = self.clean(paramName)
251 if paramName not in self.settable:
252 raise NotSettableError
253 currentVal = getattr(self, paramName)
254 val = cast(currentVal, val.strip(self.terminators))
255 setattr(self, paramName, val)
256 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val))
257 except (ValueError, AttributeError, NotSettableError), e:
258 self.do_show(arg)
259
260 def do_shell(self, arg):
261 'execute a command as if at the OS prompt.'
262 os.system(arg)
263
264 def do_history(self, arg):
265 """history [arg]: lists past commands issued
266
267 no arg -> list all
268 arg is integer -> list one history item, by index
269 arg is string -> string search
270 arg is /enclosed in forward-slashes/ -> regular expression search
271 """
272 if arg:
273 history = self.history.get(arg)
274 else:
275 history = self.history
276 for hi in history:
277 self.stdout.write(hi.pr())
278 def last_matching(self, arg):
279 try:
280 if arg:
281 return self.history.get(arg)[-1]
282 else:
283 return self.history[-1]
284 except:
285 return None
286 def do_list(self, arg):
287 """list [arg]: lists last command issued
288
289 no arg -> list absolute last
290 arg is integer -> list one history item, by index
291 - arg, arg - (integer) -> list up to or after #arg
292 arg is string -> list last command matching string search
293 arg is /enclosed in forward-slashes/ -> regular expression search
294 """
295 try:
296 self.stdout.write(self.last_matching(arg).pr())
297 except:
298 pass
299 do_hi = do_history
300 do_l = do_list
301 do_li = do_list
302
303 def do_ed(self, arg):
304 """ed: edit most recent command in text editor
305 ed [N]: edit numbered command from history
306 ed [filename]: edit specified file name
307
308 commands are run after editor is closed.
309 "set edit (program-name)" or set EDITOR environment variable
310 to control which editing program is used."""
311 if not self.editor:
312 print "please use 'set editor' to specify your text editing program of choice."
313 return
314 filename = self.defaultFileName
315 buffer = ''
316 try:
317 arg = int(arg)
318 buffer = self.last_matching(arg)
319 except:
320 if arg:
321 filename = arg
322 else:
323 buffer = self.last_matching(arg)
324
325 if buffer:
326 f = open(filename, 'w')
327 f.write(buffer or '')
328 f.close()
329
330 os.system('%s %s' % (self.editor, filename))
331 self.do_load(filename)
332 do_edit = do_ed
333
334 def do_save(self, fname=None):
335 """Saves most recent command to a file."""
336
337 if fname is None:
338 fname = self.defaultFileName
339 try:
340 f = open(fname, 'w')
341 f.write(self.history[-1])
342 f.close()
343 except Exception, e:
344 print 'Error saving %s: %s' % (fname, str(e))
345
346 def do_load(self, fname=None):
347 """Runs command(s) from a file."""
348 if fname is None:
349 fname = self.defaultFileName
350 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt','continuationPrompt'))
351 try:
352 self.stdin = open(fname, 'r')
353 except IOError, e:
354 try:
355 self.stdin = open('%s.%s' % (fname, self.defaultExtension), 'r')
356 except IOError:
357 print 'Problem opening file %s: \n%s' % (fname, e)
358 keepstate.restore()
359 return
360 self.use_rawinput = False
361 self.prompt = self.continuationPrompt = ''
362 self.cmdloop()
363 self.stdin.close()
364 keepstate.restore()
365 self.lastcmd = ''
366
367 def do_run(self, arg):
368 """run [arg]: re-runs an earlier command
369
370 no arg -> run most recent command
371 arg is integer -> run one history item, by index
372 arg is string -> run most recent command by string search
373 arg is /enclosed in forward-slashes/ -> run most recent by regex
374 """
375 'run [N]: runs the SQL that was run N commands ago'
376 runme = self.last_matching(arg)
377 print runme
378 if runme:
379 runme = self.precmd(runme)
380 stop = self.onecmd(runme)
381 stop = self.postcmd(stop, runme)
382 do_r = do_run
383
384 def fileimport(self, statement, source):
385 try:
386 f = open(source)
387 except IOError:
388 self.stdout.write("Couldn't read from file %s\n" % source)
389 return ''
390 data = f.read()
391 f.close()
392 return data
393
394 class HistoryItem(str):
395 def __init__(self, instr):
396 str.__init__(self, instr)
397 self.lowercase = self.lower()
398 self.idx = None
399 def pr(self):
400 return '-------------------------[%d]\n%s\n' % (self.idx, str(self))
401
402 class History(list):
403 rangeFrom = re.compile(r'^([\d])+\s*\-$')
404 def append(self, new):
405 new = HistoryItem(new)
406 list.append(self, new)
407 new.idx = len(self)
408 def extend(self, new):
409 for n in new:
410 self.append(n)
411 def get(self, getme):
412 try:
413 getme = int(getme)
414 if getme < 0:
415 return self[:(-1 * getme)]
416 else:
417 return [self[getme-1]]
418 except IndexError:
419 return []
420 except (ValueError, TypeError):
421 getme = getme.strip()
422 mtch = self.rangeFrom.search(getme)
423 if mtch:
424 return self[(int(mtch.group(1))-1):]
425 if getme.startswith(r'/') and getme.endswith(r'/'):
426 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE)
427 def isin(hi):
428 return finder.search(hi)
429 else:
430 def isin(hi):
431 return (getme.lower() in hi.lowercase)
432 return [itm for itm in self if isin(itm)]
433
434 class NotSettableError(Exception):
435 pass
436
437 def cast(current, new):
438 """Tries to force a new value into the same type as the current."""
439 typ = type(current)
440 if typ == bool:
441 try:
442 return bool(int(new))
443 except ValueError, TypeError:
444 pass
445 try:
446 new = new.lower()
447 except:
448 pass
449 if (new=='on') or (new[0] in ('y','t')):
450 return True
451 if (new=='off') or (new[0] in ('n','f')):
452 return False
453 else:
454 try:
455 return typ(new)
456 except:
457 pass
458 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new)
459 return current
460
461 class Statekeeper(object):
462 def __init__(self, obj, attribs):
463 self.obj = obj
464 self.attribs = attribs
465 self.save()
466 def save(self):
467 for attrib in self.attribs:
468 setattr(self, attrib, getattr(self.obj, attrib))
469 def restore(self):
470 for attrib in self.attribs:
471 setattr(self.obj, attrib, getattr(self, attrib))