19 from StringIO
import StringIO
22 from IPython
import ipapi
24 raise "Error importing IPython (%s)" % str(e)
31 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
32 cin=
None, cout=
None,cerr=
None, input_func=
None):
35 IPython.iplib.raw_input_original = input_func
37 IPython.Shell.Term.cin = cin
39 IPython.Shell.Term.cout = cout
41 IPython.Shell.Term.cerr = cerr
44 IPython.iplib.raw_input =
lambda x:
None
45 self.
term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
46 os.environ[
'TERM'] =
'dumb'
47 excepthook = sys.excepthook
48 self.
IP = IPython.Shell.make_IPython(argv,
50 user_global_ns=user_global_ns,
52 shell_class=IPython.Shell.InteractiveShell)
53 self.IP.system =
lambda cmd: self.
shell(self.IP.var_expand(cmd),
54 header=
'IPython system call: ',
55 verbose=self.IP.rc.system_verbose)
58 self.ip.magic(
'colors LightBG')
59 sys.excepthook = excepthook
65 return self.IP.user_ns
67 def eval(self, console):
69 orig_stdout = sys.stdout
70 sys.stdout = IPython.Shell.Term.cout
72 line = self.IP.raw_input(
None, self.
iter_more)
73 if self.IP.autoindent:
74 self.IP.readline_startup_hook(
None)
75 except KeyboardInterrupt:
76 self.IP.write(
'\nKeyboardInterrupt\n')
78 self.IP.outputcache.prompt_count -= 1
79 if self.IP.autoindent:
80 self.IP.indent_current_nsp = 0
83 self.IP.showtraceback()
86 if (self.IP.SyntaxTB.last_syntax_error
and self.IP.rc.autoedit_syntax):
87 self.IP.edit_syntax_error()
89 self.
prompt = str(self.IP.outputcache.prompt2).strip()
90 if self.IP.autoindent:
91 self.IP.readline_startup_hook(self.IP.pre_readline)
93 self.
prompt = str(self.IP.outputcache.prompt1).strip()
94 sys.stdout = orig_stdout
99 buf = os.read(console.piperead, 256)
104 if len(buf) < 256:
break
107 rv = console.cout.getvalue()
113 console.cout.truncate(0)
116 def complete(self, line):
117 split_line = self.complete_sep.split(line)
118 possibilities = self.IP.complete(split_line[-1])
120 common_prefix = os.path.commonprefix (possibilities)
121 completed = line[:-len(split_line[-1])]+common_prefix
124 return completed, possibilities
126 def shell(self, cmd,verbose=0,debug=0,header=''):
128 if verbose
or debug:
print header+cmd
130 input, output = os.popen4(cmd)