From 493d8a2b7a338a2a34fb1e10a0b89cec4455fbf9 Mon Sep 17 00:00:00 2001 From: Zearin Date: Tue, 15 Nov 2011 14:53:15 -0500 Subject: Non-significant whitespace adjustments (readability). --- cmd2/cmd2.py | 78 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 58 insertions(+), 20 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 423350b..4656862 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -167,9 +167,9 @@ class Cmd(cmd.Cmd): quiet = False # Do not suppress nonessential output redirector = '>' # For redirecting output to a file reserved_words = [] - shortcuts = { '?': 'help', - '!': 'shell', - '@': 'load', + shortcuts = { '?' : 'help', + '!' : 'shell', + '@' : 'load', '@@': '_relative_load' } timing = False # Prints elapsed time for each command @@ -218,10 +218,10 @@ class Cmd(cmd.Cmd): # cmd.Cmd.__init__(self, *args, **kwargs) self.initial_stdout = sys.stdout - self.history = History() - self.pystate = {} - self.shortcuts = sorted(self.shortcuts.items(), reverse=True) - self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) + self.history = History() + self.pystate = {} + self.shortcuts = sorted(self.shortcuts.items(), reverse=True) + self.keywords = self.reserved_words + [fname[3:] for fname in dir(self) if fname.startswith('do_')] self._init_parser() @@ -244,7 +244,7 @@ class Cmd(cmd.Cmd): import readline self.old_completer = readline.get_completer() readline.set_completer(self.complete) - readline.parse_and_bind(self.completekey+": complete") + readline.parse_and_bind(self.completekey + ": complete") except ImportError: pass try: @@ -508,9 +508,9 @@ class Cmd(cmd.Cmd): #self.singleLineParser = self.singleLineParser.setResultsName('singleLineParser') self.blankLineTerminationParser = self.blankLineTerminationParser.setResultsName('statement') self.parser = self.prefixParser + ( - stringEnd | - self.multilineParser | - self.singleLineParser | + stringEnd | + self.multilineParser | + self.singleLineParser | self.blankLineTerminationParser | self.multilineCommand + pyparsing.SkipTo(stringEnd, ignore=doNotParse) ) @@ -795,15 +795,53 @@ class Cmd(cmd.Cmd): stop = func(statement) return stop + + + def onecmd_plus_hooks(self, line): + # @FIXME + # Add docstring description + + # The outermost level of try/finally nesting can be condensed once + # Python 2.4 support can be dropped. + # + # @TODO Do you think we can safely drop Python 2.4 support yet? :-) + stop = 0 + + try: + try: + statement = self.complete_statement(line) + (stop, statement) = self.postparsing_precmd(statement) + if stop: + return self.postparsing_postcmd(stop) + if statement.parsed.command not in self.excludeFromHistory: + self.history.append(statement.parsed.raw) + try: + self.redirect_output(statement) + timestart = datetime.datetime.now() + statement = self.precmd(statement) + stop = self.onecmd(statement) + stop = self.postcmd(stop, statement) + if self.timing: + self.pfeedback('Elapsed: %s' % str(datetime.datetime.now() - timestart)) + finally: + self.restore_output(statement) + except EmptyStatement: + return 0 + except Exception as e: + self.perror(str(e), statement) + finally: + return self.postparsing_postcmd(stop) def pseudo_raw_input(self, prompt): ''' - Copied from cmd's cmdloop. Similar to raw_input, but accounts for changed stdin/stdout. + Copied from cmd's cmdloop. + + Similar to raw_input, but accounts for changed stdin/stdout. ''' if self.use_rawinput: - try: - line = raw_input(prompt) + try: + line = input(prompt) except EOFError: line = 'EOF' else: @@ -915,8 +953,8 @@ class Cmd(cmd.Cmd): self.__class__.testfiles = callargs sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main() testcase = TestMyAppCase() - runner = unittest.TextTestRunner() - result = runner.run(testcase) + runner = unittest.TextTestRunner() + result = runner.run(testcase) result.printErrors() @@ -933,10 +971,10 @@ class Cmd(cmd.Cmd): # Add docstring description parser = optparse.OptionParser() - parser.add_option('-t', '--test', - dest ='test', - action ='store_true', - help ='Test against transcript(s) in FILE (wildcards OK)') + parser.add_option('-t', '--test', + dest ='test', + action ='store_true', + help ='Test against transcript(s) in FILE (wildcards OK)') (callopts, callargs) = parser.parse_args() if callopts.test: -- cgit v1.2.1