Mercurial > python-cmd2
annotate cmd2.py @ 258:7a98d7f2da30
I get it - BASH line expansion of file names gets me
author | Catherine Devlin <catherine.devlin@gmail.com> |
---|---|
date | Thu, 02 Apr 2009 17:22:02 -0400 |
parents | d62bb3dd58a0 |
children | 5147fa4166b0 |
rev | line source |
---|---|
230 | 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 Optional _onchange_{paramname} called when environment parameter changes | |
13 Parsing commands with `optparse` options (flags) | |
14 Redirection to file with >, >>; input from file with < | |
15 Easy transcript-based testing of applications (see example/example.py) | |
16 | |
17 Note that redirection with > and | will only work if `self.stdout.write()` | |
18 is used in place of `print`. The standard library's `cmd` module is | |
19 written to use `self.stdout.write()`, | |
20 | |
21 - Catherine Devlin, Jan 03 2008 - catherinedevlin.blogspot.com | |
22 | |
23 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 | |
24 """ | |
25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
26 import unittest, string, datetime, urllib, glob |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
27 from code import InteractiveConsole, InteractiveInterpreter, softspace |
230 | 28 from optparse import make_option |
248
575652efb3d8
aha - I think I know why comment-in-progress parse failing
catherine@Elli.myhome.westell.com
parents:
247
diff
changeset
|
29 __version__ = '0.5.1' |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
30 import logging |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
31 |
230 | 32 class OptionParser(optparse.OptionParser): |
33 def exit(self, status=0, msg=None): | |
34 self.values._exit = True | |
35 if msg: | |
36 print msg | |
37 | |
38 def print_help(self, *args, **kwargs): | |
39 # now, I need to call help of the calling function. Hmm. | |
40 try: | |
41 print self._func.__doc__ | |
42 except AttributeError: | |
43 pass | |
44 optparse.OptionParser.print_help(self, *args, **kwargs) | |
45 | |
46 def error(self, msg): | |
47 """error(msg : string) | |
48 | |
49 Print a usage message incorporating 'msg' to stderr and exit. | |
50 If you override this in a subclass, it should not return -- it | |
51 should either exit or raise an exception. | |
52 """ | |
53 raise | |
54 | |
55 def remainingArgs(oldArgs, newArgList): | |
56 ''' | |
57 >>> remainingArgs('-f bar bar cow', ['bar', 'cow']) | |
58 'bar cow' | |
59 ''' | |
60 pattern = '\s+'.join(re.escape(a) for a in newArgList) + '\s*$' | |
61 matchObj = re.search(pattern, oldArgs) | |
62 return oldArgs[matchObj.start():] | |
63 | |
64 def options(option_list): | |
65 def option_setup(func): | |
66 optionParser = OptionParser() | |
67 for opt in option_list: | |
68 optionParser.add_option(opt) | |
69 optionParser.set_usage("%s [options] arg" % func.__name__.strip('do_')) | |
70 optionParser._func = func | |
71 def newFunc(instance, arg): | |
72 try: | |
73 opts, newArgList = optionParser.parse_args(arg.split()) # doesn't understand quoted strings shouldn't be dissected! | |
74 newArgs = remainingArgs(arg, newArgList) # should it permit flags after args? | |
75 except (optparse.OptionValueError, optparse.BadOptionError, | |
76 optparse.OptionError, optparse.AmbiguousOptionError, | |
77 optparse.OptionConflictError), e: | |
78 print e | |
79 optionParser.print_help() | |
80 return | |
81 if hasattr(opts, '_exit'): | |
82 return None | |
83 if hasattr(arg, 'parser'): | |
84 terminator = arg.parsed.terminator | |
85 try: | |
86 if arg.parsed.terminator[0] == '\n': | |
87 terminator = arg.parsed.terminator[0] | |
88 except IndexError: | |
89 pass | |
90 arg = arg.parser('%s %s%s%s' % (arg.parsed.command, newArgs, | |
91 terminator, arg.parsed.suffix)) | |
92 else: | |
93 arg = newArgs | |
94 result = func(instance, arg, opts) | |
95 return result | |
96 newFunc.__doc__ = '%s\n%s' % (func.__doc__, optionParser.format_help()) | |
97 return newFunc | |
98 return option_setup | |
99 | |
100 class PasteBufferError(EnvironmentError): | |
101 if sys.platform[:3] == 'win': | |
102 errmsg = """Redirecting to or from paste buffer requires pywin32 | |
103 to be installed on operating system. | |
104 Download from http://sourceforge.net/projects/pywin32/""" | |
105 else: | |
106 errmsg = """Redirecting to or from paste buffer requires xclip | |
107 to be installed on operating system. | |
108 On Debian/Ubuntu, 'sudo apt-get install xclip' will install it.""" | |
109 def __init__(self): | |
110 Exception.__init__(self, self.errmsg) | |
111 | |
112 '''check here if functions exist; otherwise, stub out''' | |
113 pastebufferr = """Redirecting to or from paste buffer requires %s | |
114 to be installed on operating system. | |
115 %s""" | |
116 if subprocess.mswindows: | |
117 try: | |
118 import win32clipboard | |
119 def getPasteBuffer(): | |
120 win32clipboard.OpenClipboard(0) | |
121 try: | |
122 result = win32clipboard.GetClipboardData() | |
123 except TypeError: | |
124 result = '' #non-text | |
125 win32clipboard.CloseClipboard() | |
126 return result | |
127 def writeToPasteBuffer(txt): | |
128 win32clipboard.OpenClipboard(0) | |
129 win32clipboard.EmptyClipboard() | |
130 win32clipboard.SetClipboardText(txt) | |
131 win32clipboard.CloseClipboard() | |
132 except ImportError: | |
133 def getPasteBuffer(*args): | |
134 raise OSError, pastebufferr % ('pywin32', 'Download from http://sourceforge.net/projects/pywin32/') | |
135 setPasteBuffer = getPasteBuffer | |
136 else: | |
137 can_clip = False | |
138 try: | |
139 subprocess.check_call('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
140 can_clip = True | |
141 except AttributeError: # check_call not defined, Python < 2.5 | |
142 teststring = 'Testing for presence of xclip.' | |
143 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
144 xclipproc.stdin.write(teststring) | |
145 xclipproc.stdin.close() | |
146 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
147 if xclipproc.stdout.read() == teststring: | |
148 can_clip = True | |
149 except (subprocess.CalledProcessError, OSError, IOError): | |
150 pass | |
151 if can_clip: | |
152 def getPasteBuffer(): | |
153 xclipproc = subprocess.Popen('xclip -o -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
154 return xclipproc.stdout.read() | |
155 def writeToPasteBuffer(txt): | |
156 xclipproc = subprocess.Popen('xclip -sel clip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
157 xclipproc.stdin.write(txt) | |
158 xclipproc.stdin.close() | |
159 # but we want it in both the "primary" and "mouse" clipboards | |
160 xclipproc = subprocess.Popen('xclip', shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
161 xclipproc.stdin.write(txt) | |
162 xclipproc.stdin.close() | |
163 else: | |
164 def getPasteBuffer(*args): | |
165 raise OSError, pastebufferr % ('xclip', 'On Debian/Ubuntu, install with "sudo apt-get install xclip"') | |
166 setPasteBuffer = getPasteBuffer | |
167 writeToPasteBuffer = getPasteBuffer | |
168 | |
169 pyparsing.ParserElement.setDefaultWhitespaceChars(' \t') | |
170 | |
171 class ParsedString(str): | |
172 pass | |
173 | |
174 class SkipToLast(pyparsing.SkipTo): | |
175 def parseImpl( self, instring, loc, doActions=True ): | |
176 resultStore = [] | |
177 startLoc = loc | |
178 instrlen = len(instring) | |
179 expr = self.expr | |
180 failParse = False | |
181 while loc <= instrlen: | |
182 try: | |
183 if self.failOn: | |
184 failParse = True | |
185 self.failOn.tryParse(instring, loc) | |
186 failParse = False | |
187 loc = expr._skipIgnorables( instring, loc ) | |
188 expr._parse( instring, loc, doActions=False, callPreParse=False ) | |
189 skipText = instring[startLoc:loc] | |
190 if self.includeMatch: | |
191 loc,mat = expr._parse(instring,loc,doActions,callPreParse=False) | |
192 if mat: | |
193 skipRes = ParseResults( skipText ) | |
194 skipRes += mat | |
195 resultStore.append((loc, [ skipRes ])) | |
196 else: | |
197 resultStore,append((loc, [ skipText ])) | |
198 else: | |
199 resultStore.append((loc, [ skipText ])) | |
200 loc += 1 | |
201 except (pyparsing.ParseException,IndexError): | |
202 if failParse: | |
203 raise | |
204 else: | |
205 loc += 1 | |
206 if resultStore: | |
207 return resultStore[-1] | |
208 else: | |
209 exc = self.myException | |
210 exc.loc = loc | |
211 exc.pstr = instring | |
212 raise exc | |
213 | |
214 def replace_with_file_contents(fname): | |
215 if fname: | |
216 try: | |
217 result = open(os.path.expanduser(fname[0])).read() | |
218 except IOError: | |
219 result = '< %s' % fname[0] # wasn't a file after all | |
220 else: | |
221 result = getPasteBuffer() | |
233 | 222 return result |
223 | |
234 | 224 class EmbeddedConsoleExit(Exception): |
225 pass | |
226 | |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
227 class MyInteractiveConsole(InteractiveConsole): |
234 | 228 def runcode(self, code): |
229 """Execute a code object. | |
230 | |
231 When an exception occurs, self.showtraceback() is called to | |
232 display a traceback. All exceptions are caught except | |
233 SystemExit, which is reraised. | |
234 | |
235 A note about KeyboardInterrupt: this exception may occur | |
236 elsewhere in this code, and may not always be caught. The | |
237 caller should be prepared to deal with it. | |
238 | |
239 Copied directly from code.InteractiveInterpreter, except for | |
240 EmbeddedConsoleExit exceptions. | |
241 """ | |
242 try: | |
243 exec code in self.locals | |
244 except (SystemExit, EmbeddedConsoleExit): | |
245 raise | |
246 except: | |
247 self.showtraceback() | |
248 else: | |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
249 if softspace(sys.stdout, 0): |
234 | 250 print |
251 | |
230 | 252 class Cmd(cmd.Cmd): |
253 echo = False | |
254 case_insensitive = True | |
255 continuation_prompt = '> ' | |
256 timing = False | |
257 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit # make sure your terminators are not in here! | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
258 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'} |
230 | 259 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() |
260 noSpecialParse = 'set ed edit exit'.split() | |
261 defaultExtension = 'txt' | |
262 default_file_name = 'command.txt' | |
263 abbrev = True | |
233 | 264 nonpythoncommand = 'cmd' |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
265 current_script_dir = None |
245 | 266 reserved_words = [] |
230 | 267 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', |
245 | 268 'case_insensitive', 'echo', 'timing', 'abbrev'] |
230 | 269 settable.sort() |
270 | |
271 editor = os.environ.get('EDITOR') | |
272 _STOP_AND_EXIT = 2 | |
273 if not editor: | |
274 if sys.platform[:3] == 'win': | |
275 editor = 'notepad' | |
276 else: | |
277 for editor in ['gedit', 'kate', 'vim', 'emacs', 'nano', 'pico']: | |
278 if not os.system('which %s' % (editor)): | |
279 break | |
280 | |
281 def do_cmdenvironment(self, args): | |
282 '''Summary report of interactive parameters.''' | |
283 self.stdout.write(""" | |
284 Commands are %(casesensitive)scase-sensitive. | |
285 Commands may be terminated with: %(terminators)s | |
237 | 286 Settable parameters: %(settable)s\n""" % \ |
230 | 287 { 'casesensitive': (self.case_insensitive and 'not ') or '', |
288 'terminators': str(self.terminators), | |
289 'settable': ' '.join(self.settable) | |
290 }) | |
291 | |
292 def do_help(self, arg): | |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
293 if arg: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
294 funcname = self.func_named(arg) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
295 if funcname: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
296 fn = getattr(self, funcname) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
297 try: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
298 fn.optionParser.print_help(file=self.stdout) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
299 except AttributeError: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
300 cmd.Cmd.do_help(self, funcname[3:]) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
301 else: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
302 cmd.Cmd.do_help(self, arg) |
230 | 303 |
304 def __init__(self, *args, **kwargs): | |
305 cmd.Cmd.__init__(self, *args, **kwargs) | |
306 self.history = History() | |
307 self._init_parser() | |
308 self.pystate = {} | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
309 self.shortcuts = sorted(self.shortcuts.items(), reverse=True) |
247
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
310 self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
311 if fname.startswith('do_')] |
230 | 312 def do_shortcuts(self, args): |
313 """Lists single-key shortcuts available.""" | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
314 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in sorted(self.shortcuts)) |
230 | 315 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) |
316 | |
317 prefixParser = pyparsing.Empty() | |
318 commentGrammars = pyparsing.Or([pyparsing.pythonStyleComment, pyparsing.cStyleComment]) | |
319 commentGrammars.addParseAction(lambda x: '') | |
249
55a12d77a4fa
finally, ls working right
catherine@Elli.myhome.westell.com
parents:
248
diff
changeset
|
320 commentInProgress = pyparsing.Literal('/*') + pyparsing.SkipTo(pyparsing.stringEnd) |
230 | 321 terminators = [';'] |
322 blankLinesAllowed = False | |
323 multilineCommands = [] | |
324 | |
325 def _init_parser(self): | |
326 r''' | |
327 >>> c = Cmd() | |
328 >>> c.multilineCommands = ['multiline'] | |
329 >>> c.case_insensitive = True | |
330 >>> c._init_parser() | |
331 >>> print c.parser.parseString('').dump() | |
332 [] | |
333 >>> print c.parser.parseString('/* empty command */').dump() | |
334 [] | |
335 >>> print c.parser.parseString('plainword').dump() | |
336 ['plainword', ''] | |
337 - command: plainword | |
338 - statement: ['plainword', ''] | |
339 - command: plainword | |
340 >>> print c.parser.parseString('termbare;').dump() | |
341 ['termbare', '', ';', ''] | |
342 - command: termbare | |
343 - statement: ['termbare', '', ';'] | |
344 - command: termbare | |
345 - terminator: ; | |
346 - terminator: ; | |
347 >>> print c.parser.parseString('termbare; suffx').dump() | |
348 ['termbare', '', ';', 'suffx'] | |
349 - command: termbare | |
350 - statement: ['termbare', '', ';'] | |
351 - command: termbare | |
352 - terminator: ; | |
353 - suffix: suffx | |
354 - terminator: ; | |
355 >>> print c.parser.parseString('barecommand').dump() | |
356 ['barecommand', ''] | |
357 - command: barecommand | |
358 - statement: ['barecommand', ''] | |
359 - command: barecommand | |
360 >>> print c.parser.parseString('COMmand with args').dump() | |
361 ['command', 'with args'] | |
362 - args: with args | |
363 - command: command | |
364 - statement: ['command', 'with args'] | |
365 - args: with args | |
366 - command: command | |
367 >>> print c.parser.parseString('command with args and terminator; and suffix').dump() | |
368 ['command', 'with args and terminator', ';', 'and suffix'] | |
369 - args: with args and terminator | |
370 - command: command | |
371 - statement: ['command', 'with args and terminator', ';'] | |
372 - args: with args and terminator | |
373 - command: command | |
374 - terminator: ; | |
375 - suffix: and suffix | |
376 - terminator: ; | |
377 >>> print c.parser.parseString('simple | piped').dump() | |
378 ['simple', '', '|', ' piped'] | |
379 - command: simple | |
380 - pipeTo: piped | |
381 - statement: ['simple', ''] | |
382 - command: simple | |
383 >>> print c.parser.parseString('double-pipe || is not a pipe').dump() | |
384 ['double', '-pipe || is not a pipe'] | |
385 - args: -pipe || is not a pipe | |
386 - command: double | |
387 - statement: ['double', '-pipe || is not a pipe'] | |
388 - args: -pipe || is not a pipe | |
389 - command: double | |
390 >>> print c.parser.parseString('command with args, terminator;sufx | piped').dump() | |
391 ['command', 'with args, terminator', ';', 'sufx', '|', ' piped'] | |
392 - args: with args, terminator | |
393 - command: command | |
394 - pipeTo: piped | |
395 - statement: ['command', 'with args, terminator', ';'] | |
396 - args: with args, terminator | |
397 - command: command | |
398 - terminator: ; | |
399 - suffix: sufx | |
400 - terminator: ; | |
401 >>> print c.parser.parseString('output into > afile.txt').dump() | |
402 ['output', 'into', '>', 'afile.txt'] | |
403 - args: into | |
404 - command: output | |
405 - output: > | |
406 - outputTo: afile.txt | |
407 - statement: ['output', 'into'] | |
408 - args: into | |
409 - command: output | |
410 >>> print c.parser.parseString('output into;sufx | pipethrume plz > afile.txt').dump() | |
411 ['output', 'into', ';', 'sufx', '|', ' pipethrume plz', '>', 'afile.txt'] | |
412 - args: into | |
413 - command: output | |
414 - output: > | |
415 - outputTo: afile.txt | |
416 - pipeTo: pipethrume plz | |
417 - statement: ['output', 'into', ';'] | |
418 - args: into | |
419 - command: output | |
420 - terminator: ; | |
421 - suffix: sufx | |
422 - terminator: ; | |
423 >>> print c.parser.parseString('output to paste buffer >> ').dump() | |
424 ['output', 'to paste buffer', '>>', ''] | |
425 - args: to paste buffer | |
426 - command: output | |
427 - output: >> | |
428 - statement: ['output', 'to paste buffer'] | |
429 - args: to paste buffer | |
430 - command: output | |
431 >>> print c.parser.parseString('ignore the /* commented | > */ stuff;').dump() | |
432 ['ignore', 'the /* commented | > */ stuff', ';', ''] | |
433 - args: the /* commented | > */ stuff | |
434 - command: ignore | |
435 - statement: ['ignore', 'the /* commented | > */ stuff', ';'] | |
436 - args: the /* commented | > */ stuff | |
437 - command: ignore | |
438 - terminator: ; | |
439 - terminator: ; | |
440 >>> print c.parser.parseString('has > inside;').dump() | |
441 ['has', '> inside', ';', ''] | |
442 - args: > inside | |
443 - command: has | |
444 - statement: ['has', '> inside', ';'] | |
445 - args: > inside | |
446 - command: has | |
447 - terminator: ; | |
448 - terminator: ; | |
449 >>> print c.parser.parseString('multiline has > inside an unfinished command').dump() | |
450 ['multiline', ' has > inside an unfinished command'] | |
451 - multilineCommand: multiline | |
452 >>> print c.parser.parseString('multiline has > inside;').dump() | |
453 ['multiline', 'has > inside', ';', ''] | |
454 - args: has > inside | |
455 - multilineCommand: multiline | |
456 - statement: ['multiline', 'has > inside', ';'] | |
457 - args: has > inside | |
458 - multilineCommand: multiline | |
459 - terminator: ; | |
460 - terminator: ; | |
461 >>> print c.parser.parseString('multiline command /* with comment in progress;').dump() | |
462 ['multiline', ' command /* with comment in progress;'] | |
463 - multilineCommand: multiline | |
464 >>> print c.parser.parseString('multiline command /* with comment complete */ is done;').dump() | |
465 ['multiline', 'command /* with comment complete */ is done', ';', ''] | |
466 - args: command /* with comment complete */ is done | |
467 - multilineCommand: multiline | |
468 - statement: ['multiline', 'command /* with comment complete */ is done', ';'] | |
469 - args: command /* with comment complete */ is done | |
470 - multilineCommand: multiline | |
471 - terminator: ; | |
472 - terminator: ; | |
473 >>> print c.parser.parseString('multiline command ends\n\n').dump() | |
474 ['multiline', 'command ends', '\n', '\n'] | |
475 - args: command ends | |
476 - multilineCommand: multiline | |
477 - statement: ['multiline', 'command ends', '\n', '\n'] | |
478 - args: command ends | |
479 - multilineCommand: multiline | |
480 - terminator: ['\n', '\n'] | |
481 - terminator: ['\n', '\n'] | |
482 ''' | |
483 outputParser = (pyparsing.Literal('>>') | (pyparsing.WordStart() + '>') | pyparsing.Regex('[^=]>'))('output') | |
484 | |
485 terminatorParser = pyparsing.Or([(hasattr(t, 'parseString') and t) or pyparsing.Literal(t) for t in self.terminators])('terminator') | |
486 stringEnd = pyparsing.stringEnd ^ '\nEOF' | |
487 self.multilineCommand = pyparsing.Or([pyparsing.Keyword(c, caseless=self.case_insensitive) for c in self.multilineCommands])('multilineCommand') | |
488 oneLineCommand = (~self.multilineCommand + pyparsing.Word(self.legalChars))('command') | |
489 pipe = pyparsing.Keyword('|', identChars='|') | |
490 self.commentGrammars.ignore(pyparsing.quotedString).setParseAction(lambda x: '') | |
491 self.commentInProgress.ignore(pyparsing.quotedString).ignore(pyparsing.cStyleComment) | |
492 afterElements = \ | |
493 pyparsing.Optional(pipe + pyparsing.SkipTo(outputParser ^ stringEnd)('pipeTo')) + \ | |
494 pyparsing.Optional(outputParser + pyparsing.SkipTo(stringEnd).setParseAction(lambda x: x[0].strip())('outputTo')) | |
495 if self.case_insensitive: | |
496 self.multilineCommand.setParseAction(lambda x: x[0].lower()) | |
497 oneLineCommand.setParseAction(lambda x: x[0].lower()) | |
498 if self.blankLinesAllowed: | |
499 self.blankLineTerminationParser = pyparsing.NoMatch | |
500 else: | |
501 self.blankLineTerminator = (pyparsing.lineEnd + pyparsing.lineEnd)('terminator') | |
502 self.blankLineTerminator.setResultsName('terminator') | |
503 self.blankLineTerminationParser = ((self.multilineCommand ^ oneLineCommand) + pyparsing.SkipTo(self.blankLineTerminator).setParseAction(lambda x: x[0].strip())('args') + self.blankLineTerminator)('statement') | |
504 self.multilineParser = (((self.multilineCommand ^ oneLineCommand) + SkipToLast(terminatorParser).setParseAction(lambda x: x[0].strip())('args') + terminatorParser)('statement') + | |
505 pyparsing.SkipTo(outputParser ^ pipe ^ stringEnd).setParseAction(lambda x: x[0].strip())('suffix') + afterElements) | |
250 | 506 self.multilineParser.ignore(self.commentInProgress) |
230 | 507 self.singleLineParser = ((oneLineCommand + pyparsing.SkipTo(terminatorParser ^ stringEnd ^ pipe ^ outputParser).setParseAction(lambda x:x[0].strip())('args'))('statement') + |
508 pyparsing.Optional(terminatorParser) + afterElements) | |
250 | 509 #self.multilineParser = self.multilineParser.setResultsName('multilineParser') |
510 #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') | |
511 #self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('blankLineTerminatorParser') | |
230 | 512 self.parser = ( |
513 stringEnd | | |
514 self.prefixParser + self.multilineParser | | |
515 self.prefixParser + self.singleLineParser | | |
516 self.prefixParser + self.blankLineTerminationParser | | |
517 self.prefixParser + self.multilineCommand + pyparsing.SkipTo(stringEnd) | |
518 ) | |
249
55a12d77a4fa
finally, ls working right
catherine@Elli.myhome.westell.com
parents:
248
diff
changeset
|
519 self.parser.ignore(pyparsing.quotedString).ignore(self.commentGrammars) |
230 | 520 |
521 inputMark = pyparsing.Literal('<') | |
522 inputMark.setParseAction(lambda x: '') | |
523 fileName = pyparsing.Word(self.legalChars + '/\\') | |
524 inputFrom = fileName('inputFrom') | |
525 inputFrom.setParseAction(replace_with_file_contents) | |
526 # a not-entirely-satisfactory way of distinguishing < as in "import from" from < | |
527 # as in "lesser than" | |
528 self.inputParser = inputMark + pyparsing.Optional(inputFrom) + pyparsing.Optional('>') + \ | |
529 pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|') | |
530 self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) | |
531 | |
532 def preparse(self, raw, **kwargs): | |
533 return raw | |
534 | |
535 def parsed(self, raw, **kwargs): | |
536 if isinstance(raw, ParsedString): | |
537 p = raw | |
538 else: | |
539 raw = self.preparse(raw, **kwargs) | |
540 s = self.inputParser.transformString(raw.lstrip()) | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
541 for (shortcut, expansion) in self.shortcuts: |
230 | 542 if s.lower().startswith(shortcut): |
543 s = s.replace(shortcut, expansion + ' ', 1) | |
544 break | |
545 result = self.parser.parseString(s) | |
546 result['command'] = result.multilineCommand or result.command | |
547 result['raw'] = raw | |
548 result['clean'] = self.commentGrammars.transformString(result.args) | |
549 result['expanded'] = s | |
550 p = ParsedString(result.clean) | |
551 p.parsed = result | |
552 p.parser = self.parsed | |
553 for (key, val) in kwargs.items(): | |
554 p.parsed[key] = val | |
555 return p | |
556 | |
557 def postparsing_precmd(self, statement): | |
558 stop = 0 | |
559 return stop, statement | |
247
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
560 |
230 | 561 def postparsing_postcmd(self, stop): |
562 return stop | |
247
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
563 def func_named(self, arg): |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
564 result = None |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
565 target = 'do_' + arg |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
566 if target in dir(self): |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
567 result = target |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
568 else: |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
569 if self.abbrev: # accept shortened versions of commands |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
570 funcs = [fname for fname in self.keywords if fname.startswith(arg)] |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
571 if len(funcs) == 1: |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
572 result = 'do_' + funcs[0] |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
573 return result |
230 | 574 def onecmd(self, line): |
575 """Interpret the argument as though it had been typed in response | |
576 to the prompt. | |
577 | |
578 This may be overridden, but should not normally need to be; | |
579 see the precmd() and postcmd() methods for useful execution hooks. | |
580 The return value is a flag indicating whether interpretation of | |
581 commands by the interpreter should stop. | |
582 | |
583 This (`cmd2`) version of `onecmd` already override's `cmd`'s `onecmd`. | |
584 | |
585 """ | |
586 if not line: | |
587 return self.emptyline() | |
588 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line): | |
589 return 0 # command was empty except for comments | |
590 try: | |
591 statement = self.parsed(line) | |
592 while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): | |
593 statement = '%s\n%s' % (statement.parsed.raw, | |
594 self.pseudo_raw_input(self.continuation_prompt)) | |
595 statement = self.parsed(statement) | |
596 except Exception, e: | |
597 print e | |
598 return 0 | |
246 | 599 if statement.parsed.command not in self.excludeFromHistory: |
600 self.history.append(statement.parsed.raw) | |
230 | 601 try: |
602 (stop, statement) = self.postparsing_precmd(statement) | |
603 except Exception, e: | |
604 print str(e) | |
605 return 0 | |
606 if stop: | |
607 return self.postparsing_postcmd(stop) | |
608 | |
609 if not statement.parsed.command: | |
610 return self.postparsing_postcmd(stop=0) | |
611 | |
612 statekeeper = None | |
613 | |
614 if statement.parsed.pipeTo: | |
615 redirect = subprocess.Popen(statement.parsed.pipeTo, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE) | |
616 statekeeper = Statekeeper(self, ('stdout',)) | |
617 self.stdout = redirect.stdin | |
618 elif statement.parsed.output: | |
619 statekeeper = Statekeeper(self, ('stdout',)) | |
620 if statement.parsed.outputTo: | |
621 mode = 'w' | |
622 if statement.parsed.output == '>>': | |
623 mode = 'a' | |
624 try: | |
625 self.stdout = open(os.path.expanduser(statement.parsed.outputTo), mode) | |
626 except OSError, e: | |
627 print e | |
628 return self.postparsing_postcmd(stop=0) | |
629 else: | |
630 statekeeper = Statekeeper(self, ('stdout',)) | |
631 self.stdout = tempfile.TemporaryFile() | |
632 if statement.parsed.output == '>>': | |
633 self.stdout.write(getPasteBuffer()) | |
634 try: | |
635 # "heart" of the command, replace's cmd's onecmd() | |
636 self.lastcmd = statement.parsed.expanded | |
247
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
637 funcname = self.func_named(statement.parsed.command) |
3db4166a54ce
abbrevs working with help
catherine@Elli.myhome.westell.com
parents:
246
diff
changeset
|
638 if not funcname: |
251 | 639 return self.postparsing_postcmd(self.default(statement)) |
640 try: | |
641 func = getattr(self, funcname) | |
642 except AttributeError: | |
643 return self.postparsing_postcmd(self.default(statement)) | |
230 | 644 timestart = datetime.datetime.now() |
251 | 645 stop = func(statement) |
230 | 646 if self.timing: |
647 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart) | |
648 except Exception, e: | |
649 print e | |
650 finally: | |
651 if statekeeper: | |
652 if statement.parsed.output and not statement.parsed.outputTo: | |
653 self.stdout.seek(0) | |
654 try: | |
655 writeToPasteBuffer(self.stdout.read()) | |
656 except Exception, e: | |
657 print str(e) | |
658 elif statement.parsed.pipeTo: | |
659 for result in redirect.communicate(): | |
660 statekeeper.stdout.write(result or '') | |
661 self.stdout.close() | |
662 statekeeper.restore() | |
663 | |
664 return self.postparsing_postcmd(stop) | |
665 | |
666 def pseudo_raw_input(self, prompt): | |
667 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout""" | |
668 | |
669 if self.use_rawinput: | |
670 try: | |
671 line = raw_input(prompt) | |
672 except EOFError: | |
673 line = 'EOF' | |
674 else: | |
675 self.stdout.write(prompt) | |
676 self.stdout.flush() | |
677 line = self.stdin.readline() | |
678 if not len(line): | |
679 line = 'EOF' | |
680 else: | |
681 if line[-1] == '\n': # this was always true in Cmd | |
682 line = line[:-1] | |
683 return line | |
684 | |
685 def cmdloop(self, intro=None): | |
686 """Repeatedly issue a prompt, accept input, parse an initial prefix | |
687 off the received input, and dispatch to action methods, passing them | |
688 the remainder of the line as argument. | |
689 """ | |
690 | |
691 # An almost perfect copy from Cmd; however, the pseudo_raw_input portion | |
692 # has been split out so that it can be called separately | |
693 | |
694 self.preloop() | |
695 if self.use_rawinput and self.completekey: | |
696 try: | |
697 import readline | |
698 self.old_completer = readline.get_completer() | |
699 readline.set_completer(self.complete) | |
700 readline.parse_and_bind(self.completekey+": complete") | |
701 except ImportError: | |
702 pass | |
703 try: | |
704 if intro is not None: | |
705 self.intro = intro | |
706 if self.intro: | |
707 self.stdout.write(str(self.intro)+"\n") | |
708 stop = None | |
709 while not stop: | |
710 if self.cmdqueue: | |
711 line = self.cmdqueue.pop(0) | |
712 else: | |
713 line = self.pseudo_raw_input(self.prompt) | |
714 if (self.echo) and (isinstance(self.stdin, file)): | |
715 self.stdout.write(line + '\n') | |
716 line = self.precmd(line) | |
717 stop = self.onecmd(line) | |
718 stop = self.postcmd(stop, line) | |
719 self.postloop() | |
720 finally: | |
721 if self.use_rawinput and self.completekey: | |
722 try: | |
723 import readline | |
724 readline.set_completer(self.old_completer) | |
725 except ImportError: | |
726 pass | |
727 return stop | |
728 | |
729 def do_EOF(self, arg): | |
730 return True | |
731 do_eof = do_EOF | |
732 | |
733 def showParam(self, param): | |
734 any_shown = False | |
735 param = param.strip().lower() | |
736 for p in self.settable: | |
737 if p.startswith(param): | |
738 val = getattr(self, p) | |
739 self.stdout.write('%s: %s\n' % (p, str(getattr(self, p)))) | |
740 any_shown = True | |
741 if not any_shown: | |
742 print "Parameter '%s' not supported (type 'show' for list of parameters)." % param | |
743 | |
744 def do_quit(self, arg): | |
745 return self._STOP_AND_EXIT | |
746 do_exit = do_quit | |
747 do_q = do_quit | |
748 | |
749 def do_show(self, arg): | |
750 '''Shows value of a parameter.''' | |
751 if arg.strip(): | |
752 self.showParam(arg) | |
753 else: | |
754 for param in self.settable: | |
755 self.showParam(param) | |
756 | |
757 def do_set(self, arg): | |
758 '''Sets a cmd2 parameter. Accepts abbreviated parameter names so long as there is no ambiguity. | |
759 Call without arguments for a list of settable parameters with their values.''' | |
760 try: | |
761 paramName, val = arg.split(None, 1) | |
762 paramName = paramName.strip().lower() | |
763 hits = [paramName in p for p in self.settable] | |
764 if hits.count(True) == 1: | |
765 paramName = self.settable[hits.index(True)] | |
766 currentVal = getattr(self, paramName) | |
767 if (val[0] == val[-1]) and val[0] in ("'", '"'): | |
768 val = val[1:-1] | |
769 else: | |
770 val = cast(currentVal, val) | |
771 setattr(self, paramName, val) | |
772 self.stdout.write('%s - was: %s\nnow: %s\n' % (paramName, currentVal, val)) | |
773 if currentVal != val: | |
774 try: | |
775 onchange_hook = getattr(self, '_onchange_%s' % paramName) | |
776 onchange_hook(old=currentVal, new=val) | |
777 except AttributeError: | |
778 pass | |
779 else: | |
780 self.do_show(paramName) | |
781 except (ValueError, AttributeError, NotSettableError), e: | |
782 self.do_show(arg) | |
783 | |
784 def do_pause(self, arg): | |
785 'Displays the specified text then waits for the user to press RETURN.' | |
786 raw_input(arg + '\n') | |
787 | |
788 def do_shell(self, arg): | |
789 'execute a command as if at the OS prompt.' | |
790 os.system(arg) | |
791 | |
231 | 792 def _attempt_py_command(self, arg): |
793 try: | |
794 result = eval(arg, self.pystate) | |
795 print repr(result) | |
796 except SyntaxError: | |
797 exec(arg, self.pystate) | |
798 return | |
799 | |
233 | 800 def do_py(self, arg): |
230 | 801 ''' |
802 py <command>: Executes a Python command. | |
242 | 803 py: Enters interactive Python mode. |
804 End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`. | |
241 | 805 Non-python commands can be issued with `cmd("your command")`. |
230 | 806 ''' |
807 if arg.strip(): | |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
808 interp = InteractiveInterpreter(locals=self.pystate) |
233 | 809 interp.runcode(arg) |
230 | 810 else: |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
811 interp = MyInteractiveConsole(locals=self.pystate) |
233 | 812 def quit(): |
234 | 813 raise EmbeddedConsoleExit |
236 | 814 def onecmd(arg): |
815 return self.onecmd(arg + '\n') | |
233 | 816 self.pystate['quit'] = quit |
817 self.pystate['exit'] = quit | |
236 | 818 self.pystate[self.nonpythoncommand] = onecmd |
234 | 819 try: |
240 | 820 cprt = 'Type "help", "copyright", "credits" or "license" for more information.' |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
821 keepstate = Statekeeper(sys, ('stdin','stdout')) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
822 sys.stdout = self.stdout |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
823 sys.stdin = self.stdin |
240 | 824 interp.interact(banner= "Python %s on %s\n%s\n(%s)\n%s" % |
825 (sys.version, sys.platform, cprt, self.__class__.__name__, self.do_py.__doc__)) | |
235
78ad20c2eed0
py working better now; still needs a iscomplete=True on onecmd
catherine@dellzilla
parents:
234
diff
changeset
|
826 except EmbeddedConsoleExit: |
245 | 827 pass |
828 keepstate.restore() | |
233 | 829 |
230 | 830 def do_history(self, arg): |
831 """history [arg]: lists past commands issued | |
832 | |
833 no arg -> list all | |
834 arg is integer -> list one history item, by index | |
835 arg is string -> string search | |
836 arg is /enclosed in forward-slashes/ -> regular expression search | |
837 """ | |
838 if arg: | |
839 history = self.history.get(arg) | |
840 else: | |
841 history = self.history | |
842 for hi in history: | |
843 self.stdout.write(hi.pr()) | |
844 def last_matching(self, arg): | |
845 try: | |
846 if arg: | |
847 return self.history.get(arg)[-1] | |
848 else: | |
849 return self.history[-1] | |
850 except IndexError: | |
851 return None | |
852 def do_list(self, arg): | |
853 """list [arg]: lists last command issued | |
854 | |
855 no arg -> list absolute last | |
856 arg is integer -> list one history item, by index | |
857 - arg, arg - (integer) -> list up to or after #arg | |
858 arg is string -> list last command matching string search | |
859 arg is /enclosed in forward-slashes/ -> regular expression search | |
860 """ | |
861 try: | |
862 self.stdout.write(self.last_matching(arg).pr()) | |
863 except: | |
864 pass | |
865 do_hi = do_history | |
866 do_l = do_list | |
867 do_li = do_list | |
868 | |
869 def do_ed(self, arg): | |
870 """ed: edit most recent command in text editor | |
871 ed [N]: edit numbered command from history | |
872 ed [filename]: edit specified file name | |
873 | |
874 commands are run after editor is closed. | |
875 "set edit (program-name)" or set EDITOR environment variable | |
876 to control which editing program is used.""" | |
877 if not self.editor: | |
878 print "please use 'set editor' to specify your text editing program of choice." | |
879 return | |
880 filename = self.default_file_name | |
881 if arg: | |
882 try: | |
883 buffer = self.last_matching(int(arg)) | |
884 except ValueError: | |
885 filename = arg | |
886 buffer = '' | |
887 else: | |
888 buffer = self.history[-1] | |
889 | |
890 if buffer: | |
891 f = open(os.path.expanduser(filename), 'w') | |
892 f.write(buffer or '') | |
893 f.close() | |
894 | |
895 os.system('%s %s' % (self.editor, filename)) | |
896 self.do__load(filename) | |
897 do_edit = do_ed | |
898 | |
899 saveparser = (pyparsing.Optional(pyparsing.Word(pyparsing.nums)^'*')("idx") + | |
900 pyparsing.Optional(pyparsing.Word(legalChars + '/\\'))("fname") + | |
901 pyparsing.stringEnd) | |
902 def do_save(self, arg): | |
903 """`save [N] [filename.ext]` | |
904 Saves command from history to file. | |
905 N => Number of command (from history), or `*`; | |
906 most recent command if omitted""" | |
907 | |
908 try: | |
909 args = self.saveparser.parseString(arg) | |
910 except pyparsing.ParseException: | |
911 print self.do_save.__doc__ | |
912 return | |
913 fname = args.fname or self.default_file_name | |
914 if args.idx == '*': | |
915 saveme = '\n\n'.join(self.history[:]) | |
916 elif args.idx: | |
917 saveme = self.history[int(args.idx)-1] | |
918 else: | |
919 saveme = self.history[-1] | |
920 try: | |
921 f = open(os.path.expanduser(fname), 'w') | |
922 f.write(saveme) | |
923 f.close() | |
924 print 'Saved to %s' % (fname) | |
925 except Exception, e: | |
926 print 'Error saving %s: %s' % (fname, str(e)) | |
927 | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
928 def read_file_or_url(self, fname): |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
929 if isinstance(fname, file): |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
930 target = open(fname, 'r') |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
931 else: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
932 match = self.urlre.match(fname) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
933 if match: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
934 target = urllib.urlopen(match.group(1)) |
230 | 935 else: |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
936 fname = os.path.expanduser(fname) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
937 try: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
938 result = open(os.path.expanduser(fname), 'r') |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
939 except IOError, e: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
940 result = open('%s.%s' % (os.path.expanduser(fname), |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
941 self.defaultExtension), 'r') |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
942 return result |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
943 |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
944 def do__relative_load(self, arg=None): |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
945 ''' |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
946 Runs commands in script at file or URL; if this is called from within an |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
947 already-running script, the filename will be interpreted relative to the |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
948 already-running script's directory.''' |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
949 if arg: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
950 arg = arg.split(None, 1) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
951 targetname, args = arg[0], (arg[1:] or [''])[0] |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
952 targetname = os.path.join(self.current_script_dir or '', targetname) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
953 self.do__load('%s %s' % (targetname, args)) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
954 |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
955 urlre = re.compile('(https?://[-\\w\\./]+)') |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
956 def do_load(self, arg=None): |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
957 """Runs script of command(s) from a file or URL.""" |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
958 if arg is None: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
959 targetname = self.default_file_name |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
960 else: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
961 arg = arg.split(None, 1) |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
962 targetname, args = arg[0], (arg[1:] or [''])[0].strip() |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
963 try: |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
964 target = self.read_file_or_url(targetname) |
230 | 965 except IOError, e: |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
966 print 'Problem accessing script from %s: \n%s' % (targetname, e) |
230 | 967 return |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
968 keepstate = Statekeeper(self, ('stdin','use_rawinput','prompt', |
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
969 'continuation_prompt','current_script_dir')) |
230 | 970 self.stdin = target |
971 self.use_rawinput = False | |
972 self.prompt = self.continuation_prompt = '' | |
244
e0c60ea7ad5d
midway through py script change
catherine@Elli.myhome.westell.com
parents:
243
diff
changeset
|
973 self.current_script_dir = os.path.split(targetname)[0] |
230 | 974 stop = self.cmdloop() |
975 self.stdin.close() | |
976 keepstate.restore() | |
977 self.lastcmd = '' | |
978 return (stop == self._STOP_AND_EXIT) and self._STOP_AND_EXIT | |
979 do__load = do_load # avoid an unfortunate legacy use of do_load from sqlpython | |
980 | |
981 def do_run(self, arg): | |
982 """run [arg]: re-runs an earlier command | |
983 | |
984 no arg -> run most recent command | |
985 arg is integer -> run one history item, by index | |
986 arg is string -> run most recent command by string search | |
987 arg is /enclosed in forward-slashes/ -> run most recent by regex | |
988 """ | |
989 'run [N]: runs the SQL that was run N commands ago' | |
990 runme = self.last_matching(arg) | |
991 print runme | |
992 if runme: | |
993 runme = self.precmd(runme) | |
994 stop = self.onecmd(runme) | |
995 stop = self.postcmd(stop, runme) | |
996 do_r = do_run | |
997 | |
998 def fileimport(self, statement, source): | |
999 try: | |
1000 f = open(os.path.expanduser(source)) | |
1001 except IOError: | |
1002 self.stdout.write("Couldn't read from file %s\n" % source) | |
1003 return '' | |
1004 data = f.read() | |
1005 f.close() | |
1006 return data | |
1007 | |
1008 class HistoryItem(str): | |
1009 def __init__(self, instr): | |
1010 str.__init__(self) | |
1011 self.lowercase = self.lower() | |
1012 self.idx = None | |
1013 def pr(self): | |
1014 return '-------------------------[%d]\n%s\n' % (self.idx, str(self)) | |
1015 | |
1016 class History(list): | |
1017 rangeFrom = re.compile(r'^([\d])+\s*\-$') | |
1018 def append(self, new): | |
1019 new = HistoryItem(new) | |
1020 list.append(self, new) | |
1021 new.idx = len(self) | |
1022 def extend(self, new): | |
1023 for n in new: | |
1024 self.append(n) | |
1025 def get(self, getme): | |
1026 try: | |
1027 getme = int(getme) | |
1028 if getme < 0: | |
1029 return self[:(-1 * getme)] | |
1030 else: | |
1031 return [self[getme-1]] | |
1032 except IndexError: | |
1033 return [] | |
1034 except (ValueError, TypeError): | |
1035 getme = getme.strip() | |
1036 mtch = self.rangeFrom.search(getme) | |
1037 if mtch: | |
1038 return self[(int(mtch.group(1))-1):] | |
1039 if getme.startswith(r'/') and getme.endswith(r'/'): | |
1040 finder = re.compile(getme[1:-1], re.DOTALL | re.MULTILINE | re.IGNORECASE) | |
1041 def isin(hi): | |
1042 return finder.search(hi) | |
1043 else: | |
1044 def isin(hi): | |
1045 return (getme.lower() in hi.lowercase) | |
1046 return [itm for itm in self if isin(itm)] | |
1047 | |
1048 class NotSettableError(Exception): | |
1049 pass | |
1050 | |
1051 def cast(current, new): | |
1052 """Tries to force a new value into the same type as the current.""" | |
1053 typ = type(current) | |
1054 if typ == bool: | |
1055 try: | |
1056 return bool(int(new)) | |
1057 except ValueError, TypeError: | |
1058 pass | |
1059 try: | |
1060 new = new.lower() | |
1061 except: | |
1062 pass | |
1063 if (new=='on') or (new[0] in ('y','t')): | |
1064 return True | |
1065 if (new=='off') or (new[0] in ('n','f')): | |
1066 return False | |
1067 else: | |
1068 try: | |
1069 return typ(new) | |
1070 except: | |
1071 pass | |
1072 print "Problem setting parameter (now %s) to %s; incorrect type?" % (current, new) | |
1073 return current | |
1074 | |
1075 class Statekeeper(object): | |
1076 def __init__(self, obj, attribs): | |
1077 self.obj = obj | |
1078 self.attribs = attribs | |
1079 self.save() | |
1080 def save(self): | |
1081 for attrib in self.attribs: | |
1082 setattr(self, attrib, getattr(self.obj, attrib)) | |
1083 def restore(self): | |
1084 for attrib in self.attribs: | |
1085 setattr(self.obj, attrib, getattr(self, attrib)) | |
1086 | |
1087 class Borg(object): | |
1088 '''All instances of any Borg subclass will share state. | |
1089 from Python Cookbook, 2nd Ed., recipe 6.16''' | |
1090 _shared_state = {} | |
1091 def __new__(cls, *a, **k): | |
1092 obj = object.__new__(cls, *a, **k) | |
1093 obj.__dict__ = cls._shared_state | |
1094 return obj | |
1095 | |
1096 class OutputTrap(Borg): | |
1097 '''Instantiate an OutputTrap to divert/capture ALL stdout output. For use in unit testing. | |
1098 Call `tearDown()` to return to normal output.''' | |
1099 def __init__(self): | |
1100 self.old_stdout = sys.stdout | |
1101 self.trap = tempfile.TemporaryFile() | |
1102 sys.stdout = self.trap | |
1103 def read(self): | |
1104 self.trap.seek(0) | |
1105 result = self.trap.read() | |
1106 self.trap.truncate(0) | |
1107 return result.strip('\x00') | |
1108 def tearDown(self): | |
1109 sys.stdout = self.old_stdout | |
1110 | |
1111 class Cmd2TestCase(unittest.TestCase): | |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1112 '''Subclass this, setting CmdApp, to make a unittest.TestCase class |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1113 that will execute the commands in a transcript file and expect the results shown. |
230 | 1114 See example.py''' |
1115 CmdApp = None | |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1116 def fetchTranscripts(self): |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1117 self.transcripts = {} |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1118 for fname in glob.glob(self.CmdApp.testfile): |
254
07dff0ba981e
multiple test files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
253
diff
changeset
|
1119 tfile = open(fname) |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1120 self.transcripts[fname] = iter(tfile.readlines()) |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1121 tfile.close() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1122 |
230 | 1123 def setUp(self): |
1124 if self.CmdApp: | |
1125 self.outputTrap = OutputTrap() | |
1126 self.cmdapp = self.CmdApp() | |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1127 self.fetchTranscripts() |
230 | 1128 def assertEqualEnough(self, got, expected, message): |
1129 got = got.strip().splitlines() | |
1130 expected = expected.strip().splitlines() | |
1131 self.assertEqual(len(got), len(expected), message) | |
1132 for (linegot, lineexpected) in zip(got, expected): | |
1133 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ | |
1134 replace('\\ ', ' ') | |
1135 self.assert_(re.match(matchme, linegot.strip()), message) | |
1136 def testall(self): | |
1137 if self.CmdApp: | |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1138 its = sorted(self.transcripts.items()) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1139 for (fname, transcript) in its: |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1140 self._test_transcript(fname, transcript) |
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1141 def _test_transcript(self, fname, transcript): |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1142 lineNum = 0 |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1143 try: |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1144 line = transcript.next() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1145 while True: |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1146 while not line.startswith(self.cmdapp.prompt): |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1147 line = transcript.next() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1148 command = [line[len(self.cmdapp.prompt):]] |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1149 line = transcript.next() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1150 while line.startswith(self.cmdapp.continuation_prompt): |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1151 command.append(line[len(self.cmdapp.continuation_prompt):]) |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1152 line = transcript.next() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1153 command = ''.join(command) |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1154 self.cmdapp.onecmd(command) |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1155 result = self.outputTrap.read() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1156 if line.startswith(self.cmdapp.prompt): |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1157 self.assertEqualEnough(result.strip(), '', |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1158 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1159 (fname, lineNum, command, result)) |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1160 continue |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1161 expected = [] |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1162 while not line.startswith(self.cmdapp.prompt): |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1163 expected.append(line) |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1164 line = transcript.next() |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1165 expected = ''.join(expected) |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1166 self.assertEqualEnough(expected.strip(), result.strip(), |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1167 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % |
257
d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
Catherine Devlin <catherine.devlin@gmail.com>
parents:
255
diff
changeset
|
1168 (fname, lineNum, command, expected, result)) |
253
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1169 # this needs to account for a line-by-line strip()ping |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1170 except StopIteration: |
24289178b367
midway through allowing multiple testing files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
251
diff
changeset
|
1171 pass |
230 | 1172 def tearDown(self): |
1173 if self.CmdApp: | |
1174 self.outputTrap.tearDown() | |
1175 | |
1176 if __name__ == '__main__': | |
254
07dff0ba981e
multiple test files
Catherine Devlin <catherine.devlin@gmail.com>
parents:
253
diff
changeset
|
1177 doctest.testmod(optionflags = doctest.NORMALIZE_WHITESPACE) |