changeset 219:35be1f8332a9

py object manipulation almost done
author catherine@dellzilla
date Wed, 18 Mar 2009 15:32:08 -0400
parents 5a45cf2e7455
children 8c277a37e2bc
files cmd2.py
diffstat 1 files changed, 19 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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