2 This is a modified version of source code from the Accerciser project
3 (http://live.gnome.org/accerciser).
5 Backend to the console plugin.
7 @author: Eitan Isaacson
8 @organization: IBM Corporation
9 @copyright: Copyright (c) 2007 IBM Corporation
12 All rights reserved. This program and the accompanying materials are made
13 available under the terms of the BSD which accompanies this distribution, and
14 is available at U{http://www.opensource.org/licenses/bsd-license.php}
20 from StringIO
import StringIO
26 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
27 cin=
None, cout=
None,cerr=
None, input_func=
None):
29 IPython.iplib.raw_input_original = input_func
31 IPython.Shell.Term.cin = cin
33 IPython.Shell.Term.cout = cout
35 IPython.Shell.Term.cerr = cerr
42 IPython.iplib.raw_input =
lambda x:
None
44 self.
term = IPython.genutils.IOTerm(cin=cin, cout=cout, cerr=cerr)
45 os.environ[
'TERM'] =
'dumb'
46 excepthook = sys.excepthook
47 self.
IP = IPython.Shell.make_IPython(argv,user_ns=user_ns,
48 user_global_ns=user_global_ns,
50 shell_class=IPython.Shell.InteractiveShell)
51 self.IP.system =
lambda cmd: self.
shell(self.IP.var_expand(cmd),
52 header=
'IPython system call: ',
53 verbose=self.IP.rc.system_verbose)
54 sys.excepthook = excepthook
61 orig_stdout = sys.stdout
62 sys.stdout = IPython.Shell.Term.cout
64 line = self.IP.raw_input(
None, self.
iter_more)
65 if self.IP.autoindent:
66 self.IP.readline_startup_hook(
None)
67 except KeyboardInterrupt:
68 self.IP.write(
'\nKeyboardInterrupt\n')
71 self.IP.outputcache.prompt_count -= 1
73 if self.IP.autoindent:
74 self.IP.indent_current_nsp = 0
77 self.IP.showtraceback()
80 if (self.IP.SyntaxTB.last_syntax_error
and
81 self.IP.rc.autoedit_syntax):
82 self.IP.edit_syntax_error()
85 if self.IP.autoindent:
86 self.IP.readline_startup_hook(self.IP.pre_readline)
88 self.
prompt = str(self.IP.outputcache.prompt1).
strip()
89 sys.stdout = orig_stdout
108 self.IP.user_ns.update(ns_dict)
111 split_line = self.complete_sep.split(line)
112 possibilities = self.IP.complete(split_line[-1])
115 completed = line[:-
len(split_line[-1])]+common_prefix
118 return completed, possibilities
121 for i
in range(
len(str1)):
122 if not str2.startswith(str1[:i+1]):
126 def shell(self, cmd,verbose=0,debug=0,header=''):
128 if verbose
or debug:
print header+cmd
131 input, output = os.popen4(cmd)
137 ansi_colors = {
'0;30':
'Black',
150 '1;35':
'MediumPurple',
157 Tkinter.Text.__init__(self,root)
167 self.mark_set(self.
mark,Tkinter.END)
168 self.mark_gravity(self.
mark,Tkinter.RIGHT)
171 for code
in ansi_colors:
172 self.tag_config(code,
173 foreground=ansi_colors[code])
175 self.tag_config(
'notouch')
186 self.mark_gravity(self.
line_start,Tkinter.LEFT)
190 def write(self, text, editable=False):
192 segments = self.color_pat.split(text)
194 segment = segments.pop(0)
199 self.mark_gravity(self.
start_mark,Tkinter.LEFT)
201 self.insert(Tkinter.END, segment)
205 ansi_tags = self.color_pat.findall(text)
207 for tag
in ansi_tags:
208 i = segments.index(tag)
209 self.insert(Tkinter.END,segments[i+1],tag)
214 print "adding notouch between %s : %s" % ( self.index(self.
start_mark),\
215 self.index(Tkinter.INSERT) )
217 self.tag_add(
'notouch',self.
start_mark,
"%s-1c" % Tkinter.INSERT)
224 """Print the supplied banner on starting the shell"""
230 self.see(Tkinter.INSERT)
235 self.
write(text,
True)
242 print >> self.
o,
"getCurrentline: %s" % rv
243 print >> self.
o,
"INSERT: %s" % Tkinter.END
244 print >> self.
o,
"END: %s" % Tkinter.INSERT
245 print >> self.
o,
"line_start: %s" % self.index(self.
line_start)
251 self.
write(
'\n'+text)
258 """ Bind the keys we require.
259 REM: if a bound function returns "break" then no other bindings are called
260 If it returns None, then the other default bindings are called.
270 """ Scan the notouch tag range in pairs and see if the INSERT index falls
273 ranges = self.tag_ranges(
'notouch')
282 print "Comparing %s between %s : %s " % (self.index(Tkinter.INSERT),first,idx)
284 if self.compare( Tkinter.INSERT,
'>=',first )
and \
285 self.compare( Tkinter.INSERT,
'<=',idx ):
293 print >>self.
o,
"processKeyPress got key: %s" % event.char
294 print >>self.
o,
"processKeyPress INSERT: %s" % self.index(Tkinter.INSERT)
295 print >>self.
o,
"processKeyPress END: %s" % self.index(Tkinter.END)
299 self.mark_set(Tkinter.INSERT,self.
mark)
302 self.mark_set(self.
mark,
"%s+1c" % Tkinter.INSERT)
325 if len(possibilities) > 1:
328 for symbol
in possibilities:
329 self.
write(symbol+
'\n')
336 TkConsoleView.__init__(self,root)
338 IterableIPShell.__init__(self, cout=self.
cout,cerr=self.
cout,
344 self.cout.truncate(0)
351 raise KeyboardInterrupt
357 rv = self.cout.getvalue()
359 print >>self.
o,
"_processLine got rv: %s" % rv
360 if rv: rv = rv.strip(
'\n')
362 self.cout.truncate(0)
364 if __name__ ==
"__main__":
Fstring::size_type len(Fstring const &s)
Length.
bool open(utility::io::izstream &db_stream, std::string const &db_file, bool warn)
Open a database file on a provided stream.
std::string & strip(std::string &s, std::string const &chars)
Strip Specified Characters from a string's Tails.