diff options
author | cat <cat@eee> | 2010-02-18 16:13:10 -0500 |
---|---|---|
committer | cat <cat@eee> | 2010-02-18 16:13:10 -0500 |
commit | bf4876ec681023b7280cb33fe58bf98f13eb7d9f (patch) | |
tree | 9cb50922f74d0e80fc80f0cd691d1d6906aa3073 | |
parent | 8bfd23d772027cee9aca72e61469eea3d09c32f4 (diff) | |
download | cmd2-hg-bf4876ec681023b7280cb33fe58bf98f13eb7d9f.tar.gz |
added run to py
-rwxr-xr-x | cmd2.py | 19 | ||||
-rw-r--r-- | docs/pycon2010/fileutil.py (renamed from docs/pycon2010/fileutil.script) | 2 |
2 files changed, 14 insertions, 7 deletions
@@ -1016,24 +1016,33 @@ class Cmd(cmd.Cmd): ''' 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')) diff --git a/docs/pycon2010/fileutil.script b/docs/pycon2010/fileutil.py index 411f4ba..ef920e9 100644 --- a/docs/pycon2010/fileutil.script +++ b/docs/pycon2010/fileutil.py @@ -1,5 +1,3 @@ -set echo on -py import os import os.path |