changeset 369:bc4b956d57b6

added run to py
author cat@eee
date Thu, 18 Feb 2010 16:13:10 -0500
parents 0816cdb5a7db
children cd25e114fbc1
files cmd2.py docs/pycon2010/fileutil.py docs/pycon2010/fileutil.script
diffstat 3 files changed, 27 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/cmd2.py	Thu Feb 18 15:35:36 2010 -0500
+++ b/cmd2.py	Thu Feb 18 16:13:10 2010 -0500
@@ -1016,24 +1016,33 @@
         '''
         py <command>: Executes a Python command.
         py: Enters interactive Python mode.
-        End with `Ctrl-D` (Unix) / `Ctrl-Z` (Windows), `quit()`, 'exit()`.
-        Non-python commands can be issued with `cmd("your command")`.
+        End with ``Ctrl-D`` (Unix) / ``Ctrl-Z`` (Windows), ``quit()``, '`exit()``.
+        Non-python commands can be issued with ``cmd("your command")``.
+        Run python code from external files with ``run("filename.py")``
         '''
         self.pystate['self'] = self
         arg = arg.parsed.raw[2:].strip()
+        localvars = (self.locals_in_py and self.pystate) or {}
+        interp = InteractiveConsole(locals=localvars)
+        interp.runcode('import sys, os;sys.path.insert(0, os.getcwd())')
         if arg.strip():
-            interp = InteractiveInterpreter(locals=self.pystate)
             interp.runcode(arg)
         else:
-            localvars = (self.locals_in_py and self.pystate) or {}
-            interp = InteractiveConsole(locals=localvars)
             def quit():
                 raise EmbeddedConsoleExit
             def onecmd_plus_hooks(arg):
                 return self.onecmd_plus_hooks(arg + '\n')
+            def run(arg):
+                try:
+                    file = open(arg)
+                    interp.runcode(file.read())
+                    file.close()
+                except IOError, e:
+                    self.perror(e)
             self.pystate['quit'] = quit
             self.pystate['exit'] = quit
             self.pystate['cmd'] = onecmd_plus_hooks
+            self.pystate['run'] = run
             try:
                 cprt = 'Type "help", "copyright", "credits" or "license" for more information.'        
                 keepstate = Statekeeper(sys, ('stdin','stdout'))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/docs/pycon2010/fileutil.py	Thu Feb 18 16:13:10 2010 -0500
@@ -0,0 +1,13 @@
+import os
+import os.path
+
+for (dirpath, dirnames, filenames) in os.walk('/home/cat/proj/sqlpython/sqlpython'):
+    for fname in filenames:
+        fullfilename = os.path.join(dirpath, fname)
+        stats = os.stat(fullfilename)
+        binds['path'] = dirpath
+        binds['name'] = fname
+        binds['bytes'] = stats.st_size
+        cmd("""INSERT INTO cat.files (path, name, bytes)
+               VALUES (%(path)s, %(name)s, %(bytes)s)""")
+    quit()
--- a/docs/pycon2010/fileutil.script	Thu Feb 18 15:35:36 2010 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-set echo on
-py
-import os
-import os.path
-
-for (dirpath, dirnames, filenames) in os.walk('/home/cat/proj/sqlpython/sqlpython'):
-    for fname in filenames:
-        fullfilename = os.path.join(dirpath, fname)
-        stats = os.stat(fullfilename)
-        binds['path'] = dirpath
-        binds['name'] = fname
-        binds['bytes'] = stats.st_size
-        cmd("""INSERT INTO cat.files (path, name, bytes)
-               VALUES (%(path)s, %(name)s, %(bytes)s)""")
-    quit()