[IPython-dev] IPython 0.11.rc1 as a built-in shell for a GUI
Hugo Gagnon
sourceforge.ipython at user.fastmail.fm
Mon Jul 18 12:12:04 EDT 2011
Hello,
In the past I've been using 0.10 as a built-in shell for my small GUI.
When I first implemented it I remember taking most of the code from your
cookbook. However my GUI now fails miserably at the very first line with
0.11.rc1. I don't have the time to go through your doc (and quite
frankly I'm not an expert programmer) and I would appreciate if someone
could help me port my code so that it works with 0.11. Here it is:
import StringIO
import os
import re
import subprocess
import sys
import IPython
class Shell(object):
def __init__(self, input_func, user_ns, user_global_ns):
if '__builtins__' in user_ns: # this fixes a bug in IPython
del user_ns['__builtins__']
self.IP = IPython.Shell.make_IPython(argv=[],
user_ns=user_ns,
user_global_ns=user_global_ns,
embedded=True,
shell_class=IPython.Shell.InteractiveShell)
self.IP.set_hook('shell_hook', self.shell_hook)
self.out = StringIO.StringIO()
IPython.iplib.raw_input_original = input_func
IPython.genutils.Term.cout = self.out
IPython.genutils.Term.cerr = self.out
os.environ['TERM'] = 'dumb'
self.ip = IPython.ipapi.get()
self.ip.magic('colors NoColor')
self.iter_more = 0
self.prompt = str(self.IP.outputcache.prompt1)
self.complete_sep = re.compile('[\s\{\}\[\]\(\)\=]')
def execute(self):
orig_stdout = sys.stdout
sys.stdout = IPython.Shell.Term.cout
line = self.IP.raw_input(continue_prompt=self.iter_more)
self.iter_more = self.IP.push(line)
if self.iter_more:
self.prompt = str(self.IP.outputcache.prompt2)
else:
self.prompt = str(self.IP.outputcache.prompt1)
out = self.out.getvalue().strip()
self.out.truncate(0)
sys.stdout = orig_stdout
return out
def complete(self, line):
split_line = self.complete_sep.split(line)
possibilities = self.IP.complete(split_line[-1])
if possibilities:
common_prefix = os.path.commonprefix(possibilities)
completed = line[:-len(split_line[-1])] + common_prefix
else:
completed = line
return completed, possibilities
def shell_hook(self, IP, cmd):
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out = p.stdout
print out.read()
out.close()
Your time is very much appreciated,
--
Hugo Gagnon
More information about the IPython-dev
mailing list