Mercurial > python-cmd2
comparison cmd2.py @ 192:c0d4c7ba14a9
added settable timings param
author | catherine@Elli.myhome.westell.com |
---|---|
date | Sun, 15 Feb 2009 11:03:38 -0500 |
parents | 9d9e9ea88daf |
children | e96877b59371 |
comparison
equal
deleted
inserted
replaced
191:9d9e9ea88daf | 192:c0d4c7ba14a9 |
---|---|
22 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 | 22 mercurial repository at http://www.assembla.com/wiki/show/python-cmd2 |
23 CHANGES: | 23 CHANGES: |
24 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 24 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
25 flagReader.py options are still supported for backward compatibility | 25 flagReader.py options are still supported for backward compatibility |
26 """ | 26 """ |
27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest, unittest, string | 27 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
28 import unittest, string, datetime | |
28 from optparse import make_option | 29 from optparse import make_option |
29 __version__ = '0.4.5' | 30 __version__ = '0.4.5' |
30 | 31 |
31 class OptionParser(optparse.OptionParser): | 32 class OptionParser(optparse.OptionParser): |
32 def exit(self, status=0, msg=None): | 33 def exit(self, status=0, msg=None): |
159 | 160 |
160 class Cmd(cmd.Cmd): | 161 class Cmd(cmd.Cmd): |
161 echo = False | 162 echo = False |
162 caseInsensitive = True | 163 caseInsensitive = True |
163 continuationPrompt = '> ' | 164 continuationPrompt = '> ' |
165 timing = False | |
164 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit # make sure your terminators are not in here! | 166 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit # make sure your terminators are not in here! |
165 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } | 167 shortcuts = {'?': 'help', '!': 'shell', '@': 'load' } |
166 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() | 168 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() |
167 noSpecialParse = 'set ed edit exit'.split() | 169 noSpecialParse = 'set ed edit exit'.split() |
168 defaultExtension = 'txt' | 170 defaultExtension = 'txt' |
169 defaultFileName = 'command.txt' | 171 defaultFileName = 'command.txt' |
170 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 'echo'] | 172 settable = ['prompt', 'continuationPrompt', 'defaultFileName', 'editor', 'caseInsensitive', 'echo', 'timing'] |
171 | 173 |
172 editor = os.environ.get('EDITOR') | 174 editor = os.environ.get('EDITOR') |
173 _STOP_AND_EXIT = 2 | 175 _STOP_AND_EXIT = 2 |
174 if not editor: | 176 if not editor: |
175 if sys.platform[:3] == 'win': | 177 if sys.platform[:3] == 'win': |
445 """ | 447 """ |
446 if not line: | 448 if not line: |
447 return self.emptyline() | 449 return self.emptyline() |
448 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line): | 450 if not pyparsing.Or(self.commentGrammars).setParseAction(lambda x: '').transformString(line): |
449 return 0 # command was empty except for comments | 451 return 0 # command was empty except for comments |
452 import profile | |
450 try: | 453 try: |
451 statement = self.parsed(line) | 454 statement = self.parsed(line) |
452 while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): | 455 while statement.parsed.multilineCommand and (statement.parsed.terminator == ''): |
453 statement = self.parsed('%s\n%s' % (statement.parsed.raw, | 456 statement = '%s\n%s' % (statement.parsed.raw, |
454 self.pseudo_raw_input(self.continuationPrompt))) | 457 self.pseudo_raw_input(self.continuationPrompt)) |
458 statement = self.parsed(statement) | |
455 except Exception, e: | 459 except Exception, e: |
456 print e | 460 print e |
457 return 0 | 461 return 0 |
458 | 462 |
459 if not statement.parsed.command: | 463 if not statement.parsed.command: |
487 self.lastcmd = statement.parsed.expanded | 491 self.lastcmd = statement.parsed.expanded |
488 try: | 492 try: |
489 func = getattr(self, 'do_' + statement.parsed.command) | 493 func = getattr(self, 'do_' + statement.parsed.command) |
490 except AttributeError: | 494 except AttributeError: |
491 return self.default(statement) | 495 return self.default(statement) |
492 stop = func(statement) | 496 timestart = datetime.datetime.now() |
497 stop = func(statement) | |
498 if self.timing: | |
499 print 'Elapsed: %s' % str(datetime.datetime.now() - timestart) | |
493 except Exception, e: | 500 except Exception, e: |
494 print e | 501 print e |
495 try: | 502 try: |
496 if statement.parsed.command not in self.excludeFromHistory: | 503 if statement.parsed.command not in self.excludeFromHistory: |
497 self.history.append(statement.parsed.raw) | 504 self.history.append(statement.parsed.raw) |