comparison cmd2.py @ 330:3aca8af5971f

default_to_shell
author cat@eee
date Fri, 12 Feb 2010 16:28:15 -0500
parents c69ad8418d39
children 6306edc46a6e
comparison
equal deleted inserted replaced
329:c69ad8418d39 330:3aca8af5971f
391 timing = False # Prints elapsed time for each command 391 timing = False # Prints elapsed time for each command
392 # make sure your terminators are not in legalChars! 392 # make sure your terminators are not in legalChars!
393 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit 393 legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit
394 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'} 394 shortcuts = {'?': 'help', '!': 'shell', '@': 'load', '@@': '_relative_load'}
395 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() 395 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split()
396 default_to_shell = True
396 noSpecialParse = 'set ed edit exit'.split() 397 noSpecialParse = 'set ed edit exit'.split()
397 defaultExtension = 'txt' # For ``save``, ``load``, etc. 398 defaultExtension = 'txt' # For ``save``, ``load``, etc.
398 default_file_name = 'command.txt' # For ``save``, ``load``, etc. 399 default_file_name = 'command.txt' # For ``save``, ``load``, etc.
399 abbrev = True # Abbreviated commands recognized 400 abbrev = True # Abbreviated commands recognized
400 current_script_dir = None 401 current_script_dir = None
826 try: 827 try:
827 # "heart" of the command, replaces cmd's onecmd() 828 # "heart" of the command, replaces cmd's onecmd()
828 self.lastcmd = statement.parsed.raw 829 self.lastcmd = statement.parsed.raw
829 funcname = self.func_named(statement.parsed.command) 830 funcname = self.func_named(statement.parsed.command)
830 if not funcname: 831 if not funcname:
831 return self.postparsing_postcmd(self.default(statement.full_parsed_statement())) 832 return self._default(statement)
832 try: 833 try:
833 func = getattr(self, funcname) 834 func = getattr(self, funcname)
834 except AttributeError: 835 except AttributeError:
835 return self.postparsing_postcmd(self.default(statement.full_parsed_statement())) 836 return self._default(statement)
836 timestart = datetime.datetime.now() 837 timestart = datetime.datetime.now()
837 stop = func(statement) 838 stop = func(statement)
838 if self.timing: 839 if self.timing:
839 self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart)) 840 self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart))
840 except Exception, e: 841 except Exception, e:
853 self.stdout.close() 854 self.stdout.close()
854 statekeeper.restore() 855 statekeeper.restore()
855 856
856 return self.postparsing_postcmd(stop) 857 return self.postparsing_postcmd(stop)
857 858
859 def _default(self, statement):
860 arg = statement.full_parsed_statement()
861 if self.default_to_shell:
862 result = os.system(arg)
863 if not result:
864 return self.postparsing_postcmd(None)
865 return self.postparsing_postcmd(self.default(arg))
866
858 def pseudo_raw_input(self, prompt): 867 def pseudo_raw_input(self, prompt):
859 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout""" 868 """copied from cmd's cmdloop; like raw_input, but accounts for changed stdin, stdout"""
860 869
861 if self.use_rawinput: 870 if self.use_rawinput:
862 try: 871 try: