diff options
author | Zearin <zearin@gonk.net> | 2011-11-15 15:03:05 -0500 |
---|---|---|
committer | Zearin <zearin@gonk.net> | 2011-11-15 15:03:05 -0500 |
commit | 69ceb1b3b4c685e628210c8e68620b4ae71689a2 (patch) | |
tree | 79959aa879bc007aa22e5f653ba5fa4afbe9c54b | |
parent | 527ff957fa65262c0e8c7d9a7c69575ccf726684 (diff) | |
download | cmd2-69ceb1b3b4c685e628210c8e68620b4ae71689a2.tar.gz |
Various Python3 edits.
* using `isinstance()` on files checks against 2 classes instead of just 1 (a match for either returns true)
* remove `u` from the beginning of strings (Python3 has no `unicode` or `basestring` types)
* Python3 style exceptions
* Use `.format()` over `%` (not strictly Python3, but still--it’s code modernization :P)
-rwxr-xr-x | cmd2/cmd2.py | 57 |
1 files changed, 11 insertions, 46 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 4656862..8f54d0c 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -161,7 +161,7 @@ class Cmd(cmd.Cmd): feedback_to_output = False # Include nonessentials in >, | output kept_state = None # make sure your terminators are not in legalChars! - legalChars = u'!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit + legalChars = '!#$%.:?@_' + pyparsing.alphanums + pyparsing.alphas8bit locals_in_py = True noSpecialParse = 'set ed edit exit'.split() quiet = False # Do not suppress nonessential output @@ -258,7 +258,7 @@ class Cmd(cmd.Cmd): line = self.cmdqueue.pop(0) else: line = self.pseudo_raw_input(self.prompt) - if (self.echo) and (isinstance(self.stdin, file)): + if (self.echo) and (isinstance(self.stdin, (file,TextIOWrapper))): self.stdout.write(line + '\n') stop = self.onecmd_plus_hooks(line) self.postloop() @@ -664,43 +664,7 @@ class Cmd(cmd.Cmd): if len(funcs) == 1: result = 'do_' + funcs[0] - return result - - - 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, e: - self.perror(str(e), statement) - finally: - return self.postparsing_postcmd(stop) + return result def complete_statement(self, line): @@ -887,7 +851,7 @@ class Cmd(cmd.Cmd): self.poutput(' %2d. %s\n' % (idx+1, text)) while True: - response = raw_input(prompt) + response = input(prompt) try: response = int(response) result = fulloptions[response - 1][0] @@ -915,19 +879,20 @@ class Cmd(cmd.Cmd): # Add docstring description # @TODO : not working on localhost - if isinstance(fname, file): + if isinstance(fname, (file,TextIOWrapper)): result = open(fname, 'r') else: match = self.urlre.match(fname) if match: - result = urllib.urlopen(match.group(1)) + result = urllib.urlopen(match.group(1)) else: - fname = os.path.expanduser(fname) + fname = os.path.expanduser(fname) try: result = open(os.path.expanduser(fname), 'r') except IOError: - result = open('%s.%s' % (os.path.expanduser(fname), - self.defaultExtension), 'r') + result = open('{}.{}'.format(os.path.expanduser(fname) , + self.defaultExtension) , + 'r') return result @@ -937,7 +902,7 @@ class Cmd(cmd.Cmd): try: f = open(os.path.expanduser(source)) except IOError: - self.stdout.write("Couldn't read from file %s\n" % source) + self.stdout.write("Couldn't read from file {}\n".format(source)) return '' data = f.read() f.close() |