# HG changeset patch # User catherine@dellzilla # Date 1237404728 14400 # Node ID 35be1f8332a95d8904fe56aec3170e02719674af # Parent 5a45cf2e74552009164323384d3aec950a66a0f8 py object manipulation almost done diff -r 5a45cf2e7455 -r 35be1f8332a9 cmd2.py --- a/cmd2.py Wed Mar 18 12:12:39 2009 -0400 +++ b/cmd2.py Wed Mar 18 15:32:08 2009 -0400 @@ -273,7 +273,7 @@ cmd.Cmd.__init__(self, *args, **kwargs) self.history = History() self._init_parser() - self.pyenvironment = {} + self.pystate = {} def do_shortcuts(self, args): """Lists single-key shortcuts available.""" @@ -494,10 +494,14 @@ pyparsing.Optional(fileName) + (pyparsing.stringEnd | '|') self.inputParser.ignore(pyparsing.quotedString).ignore(self.commentGrammars).ignore(self.commentInProgress) + def preparse(self, raw, **kwargs): + return raw + def parsed(self, raw, **kwargs): if isinstance(raw, ParsedString): p = raw else: + raw = self.preparse(raw, **kwargs) s = self.inputParser.transformString(raw.lstrip()) for (shortcut, expansion) in self.shortcuts.items(): if s.lower().startswith(shortcut): @@ -741,7 +745,11 @@ '''Executes a python command''' if arg.strip(): try: - exec(arg, self.pyenvironment) + result = eval(arg, self.pystate) + if result is None: + exec(arg, self.pystate) + else: + print result except Exception, e: print e else: @@ -749,10 +757,18 @@ buffer = [self.pseudo_raw_input('>>> ')] while not buffer[-1].strip().startswith('\\py'): try: - exec('\n'.join(buffer), self.pyenvironment) + buf = '\n'.join(buffer) + result = eval(buf, self.pystate) + if result is None: + exec(buf, self.pystate) + else: + print result buffer = [self.pseudo_raw_input('>>> ')] except SyntaxError: buffer.append(self.pseudo_raw_input('... ')) + except Exception, e: + print e + buffer = [self.pseudo_raw_input('>>> ')] def do_history(self, arg): """history [arg]: lists past commands issued