2Backend to the console plugin.
5@organization: IBM Corporation
6@copyright: Copyright (c) 2007 IBM Corporation
9All rights reserved. This program and the accompanying materials are made
10available under the terms of the BSD which accompanies this distribution, and
11is available at U{http://www.opensource.org/licenses/bsd-license.php}
16from __future__
import print_function
21from gi.repository
import Pango
22from StringIO
import StringIO
25from pkg_resources
import parse_version
75 def __init__(self,argv=None,user_ns=None,user_global_ns=None,
76 cin=None, cout=None,cerr=None, input_func=None):
79 @param self: this object
80 @param argv: Command line options
for IPython
81 @param user_ns: User namespace.
82 @param user_global_ns: User
global namespace.
83 @param cin: Console standard input.
84 @param cout: Console standard output.
85 @param cerr: Console standard error.
86 @param input_func: Replacement
for builtin raw_input()
90 if parse_version(IPython.release.version) >= parse_version(
"1.2.1"):
91 IPython.terminal.interactiveshell.raw_input_original = input_func
93 IPython.frontend.terminal.interactiveshell.raw_input_original = input_func
95 io.stdin = io.IOStream(cin)
97 io.stdout = io.IOStream(cout)
99 io.stderr = io.IOStream(cerr)
104 io.raw_input =
lambda x:
None
106 os.environ[
'TERM'] =
'dumb'
107 excepthook = sys.excepthook
109 from IPython.config.loader
import Config
111 cfg.InteractiveShell.colors =
"Linux"
116 old_stdout, old_stderr = sys.stdout, sys.stderr
117 sys.stdout, sys.stderr = io.stdout.stream, io.stderr.stream
121 if parse_version(IPython.release.version) >= parse_version(
"1.2.1"):
122 self.
IP = IPython.terminal.embed.InteractiveShellEmbed.instance(\
123 config=cfg, user_ns=user_ns)
125 self.
IP = IPython.frontend.terminal.embed.InteractiveShellEmbed.instance(\
126 config=cfg, user_ns=user_ns)
128 sys.stdout, sys.stderr = old_stdout, old_stderr
130 self.
IP.system =
lambda cmd: self.
shell(self.
IP.var_expand(cmd),
131 header=
'IPython system call: ')
136 self.
IP.raw_input = input_func
137 sys.excepthook = excepthook
143 self.
IP.readline_startup_hook(self.
IP.pre_readline)
150 Update self.IP namespace for autocompletion
with sys.modules
153 for k, v
in list(sys.modules.items()):
155 self.
IP.user_ns.update({k:v})
159 Executes the current line provided by the shell object.
163 orig_stdout = sys.stdout
164 sys.stdout = IPython.utils.io.stdout
166 orig_stdin = sys.stdin
167 sys.stdin = IPython.utils.io.stdin;
170 self.IP.hooks.pre_prompt_hook()
175 self.
IP.showtraceback()
176 if self.
IP.autoindent:
177 self.
IP.rl_do_indent =
True
180 line = self.
IP.raw_input(self.
prompt)
181 except KeyboardInterrupt:
182 self.
IP.write(
'\nKeyboardInterrupt\n')
183 self.
IP.input_splitter.reset()
185 self.
IP.showtraceback()
187 self.
IP.input_splitter.push(line)
188 self.
iter_more = self.
IP.input_splitter.push_accepts_more()
190 if (self.
IP.SyntaxTB.last_syntax_error
and
191 self.
IP.autoedit_syntax):
192 self.
IP.edit_syntax_error()
194 if parse_version(IPython.release.version) >= parse_version(
"2.0.0-dev"):
195 source_raw = self.
IP.input_splitter.raw_reset()
197 source_raw = self.
IP.input_splitter.source_raw_reset()[1]
198 self.
IP.run_cell(source_raw, store_history=
True)
199 self.
IP.rl_do_indent =
False
203 self.
IP.rl_do_indent =
True
206 sys.stdout = orig_stdout
207 sys.stdin = orig_stdin
211 Generate prompt depending on is_continuation value
213 @param is_continuation
214 @return: The prompt string representation
220 ver = IPython.__version__
222 prompt = self.
IP.hooks.generate_prompt(is_continuation)
225 prompt = self.
IP.prompt_manager.render(
'in2')
227 prompt = self.
IP.prompt_manager.render(
'in')
234 Provides one history command back.
236 @param self this object
237 @return: The command string.
246 Provides one history command forward.
248 @param self this object
249 @return: The command string.
257 Gets the command string of the current history level.
259 @param self this object
260 @return: Historic command string.
270 Add the current dictionary to the shell namespace.
272 @param ns_dict: A dictionary of symbol-values.
275 self.IP.user_ns.update(ns_dict)
279 Returns an auto completed line and/
or possibilities
for completion.
281 @param line: Given line so far.
282 @return: Line completed
as for as possible,
and possible further completions.
286 possibilities = self.
IP.
complete(split_line[-1])
289 possibilities = [
'', []]
291 def _commonPrefix(str1, str2):
293 Reduction function. returns common prefix of two given strings.
295 @param str1: First string.
296 @param str2: Second string
297 @return: Common prefix to both strings.
299 for i
in range(len(str1)):
300 if not str2.startswith(str1[:i+1]):
304 common_prefix = reduce(_commonPrefix, possibilities[1])
or line[-1]
305 completed = line[:-len(split_line[-1])]+common_prefix
310 return completed, possibilities[1]
313 def shell(self, cmd,verbose=0,debug=0,header=''):
315 Replacement method to allow shell commands without them blocking.
317 @param cmd: Shell command to execute.
318 @param verbose: Verbosity
319 @param debug: Debug level
320 @param header: Header to be printed before output
324 if verbose
or debug: print(header+cmd)
327 input, output = os.popen4(cmd)
345 Specialized text view for console-like workflow.
347 @cvar ANSI_COLORS: Mapping of terminal colors to X11 names.
348 @type ANSI_COLORS: dictionary
350 @ivar text_buffer: Widget
's text buffer. @type text_buffer: Gtk.TextBuffer
351 @ivar color_pat: Regex of terminal color pattern
352 @type color_pat: _sre.SRE_Pattern
353 @ivar mark: Scroll mark
for automatic scrolling on input.
354 @type mark: Gtk.TextMark
355 @ivar line_start: Start of command line mark.
356 @type line_start: Gtk.TextMark
358 ANSI_COLORS = {'0;30':
'Black',
'0;31':
'Red',
359 '0;32':
'Green',
'0;33':
'Brown',
360 '0;34':
'Blue',
'0;35':
'Purple',
361 '0;36':
'Cyan',
'0;37':
'LightGray',
362 '1;30':
'DarkGray',
'1;31':
'DarkRed',
363 '1;32':
'SeaGreen',
'1;33':
'Yellow',
364 '1;34':
'LightBlue',
'1;35':
'MediumPurple',
365 '1;36':
'LightCyan',
'1;37':
'White'}
369 Initialize console view.
371 GObject.GObject.__init__(self)
372 self.modify_font(Pango.FontDescription('Mono'))
373 self.set_cursor_visible(
True)
383 self.
text_buffer.create_tag(
'notouch', editable=
False)
384 self.
color_pat = re.compile(
'\x01?\x1b\[(.*?)m\x02?')
388 self.connect(
'key-press-event', self.
onKeyPress)
390 def write(self, text, editable=False):
392 Write given text to buffer.
394 @param text: Text to append.
395 @param editable: If true, added text
is editable.
398 GObject.idle_add(self._write, text, editable)
400 def _write(self, text, editable=False):
402 Write given text to buffer.
404 @param text: Text to append.
405 @param editable: If true, added text
is editable.
409 segment = segments.pop(0)
417 for tag
in ansi_tags:
418 i = segments.index(tag)
420 segments[i+1], str(tag))
427 self.scroll_mark_onscreen(self.
mark)
431 Prints prompt at start of line.
433 @param prompt: Prompt to
print.
440 Prints prompt at start of line.
442 @param prompt: Prompt to
print.
451 Replace currently entered command line with given text.
453 @param text: Text to use
as replacement.
460 Replace currently entered command line with given text.
462 @param text: Text to use
as replacement.
466 iter.forward_to_line_end()
472 Get text in current command line.
474 @return Text of current command line.
483 Show returned text from last command
and print new prompt.
485 @param text: Text to show.
492 Show returned text from last command
and print new prompt.
494 @param text: Text to show.
498 iter.forward_to_line_end()
510 if self.IP.rl_do_indent:
511 indentation = self.IP.input_splitter.indent_spaces *
' '
516 Key press callback used for correcting behavior
for console-like
517 interfaces. For example
'home' should go to prompt,
not to beginning of
520 @param widget: Widget that key press accored
in.
521 @param event: Event object
522 @return Return
True if event should
not trickle.
525 insert_iter = self.text_buffer.get_iter_at_mark(insert_mark)
526 selection_mark = self.text_buffer.get_selection_bound()
527 selection_iter = self.text_buffer.get_iter_at_mark(selection_mark)
529 if event.keyval == Gdk.KEY_Home:
530 if event.get_state() & Gdk.ModifierType.CONTROL_MASK
or event.get_state() & Gdk.ModifierType.MOD1_MASK:
532 elif event.get_state() & Gdk.ModifierType.SHIFT_MASK:
533 self.
text_buffer.move_mark(insert_mark, start_iter)
538 elif event.keyval == Gdk.KEY_Left:
539 insert_iter.backward_cursor_position()
540 if not insert_iter.editable(
True):
542 elif not event.string:
544 elif start_iter.compare(insert_iter) <= 0
and \
545 start_iter.compare(selection_iter) <= 0:
547 elif start_iter.compare(insert_iter) > 0
and \
548 start_iter.compare(selection_iter) > 0:
550 elif insert_iter.compare(selection_iter) < 0:
551 self.
text_buffer.move_mark(insert_mark, start_iter)
552 elif insert_iter.compare(selection_iter) > 0:
553 self.
text_buffer.move_mark(selection_mark, start_iter)
559 For some reason we can't extend onKeyPress directly (bug #500900).
560 @param event key press
582 Sub-class of both modified
IPython shell and L{ConsoleView} this makes
583 a GTK+ IPython console.
587 Initialize. Redirect I/O to console.
589 ConsoleView.__init__(self)
590 self.cout = StringIO()
591 IterableIPShell.__init__(self, cout=self.cout,cerr=self.cout,
601 Custom raw_input() replacement. Gets current line from console buffer.
603 @param prompt: Prompt to
print. Here
for compatibility
as replacement.
604 @return The current command line text.
608 raise KeyboardInterrupt
613 Key press callback with plenty of shell goodness, like history,
614 autocompletions, etc.
616 @param event: Event object.
617 @return True if event should
not trickle.
620 if event.get_state() & Gdk.ModifierType.CONTROL_MASK
and event.keyval == 99:
624 elif event.keyval == Gdk.KEY_Return:
627 elif event.keyval == Gdk.KEY_Up:
630 elif event.keyval == Gdk.KEY_Down:
633 elif event.keyval == Gdk.KEY_Tab:
637 if len(possibilities) > 1:
640 for symbol
in possibilities:
641 self.
write(symbol+
'\n')
648 Process current command line.
653 rv = self.cout.getvalue()
654 if rv: rv = rv.strip(
'\n')
656 self.
cout.truncate(0)
659if __name__ ==
"__main__":
660 window = Gtk.Window()
661 window.set_default_size(640, 320)
662 window.connect(
'delete-event',
lambda x, y: Gtk.main_quit())
def write(self, text, editable=False)
Write given text to buffer.
def changeLine(self, text)
Replace currently entered command line with given text.
dictionary ANSI_COLORS
color list
def _showPrompt(self, prompt)
Prints prompt at start of line.
def _showReturned(self, text)
Show returned text from last command and print new prompt.
def getCurrentLine(self)
Get text in current command line.
def onKeyPressExtend(self, event)
For some reason we can't extend onKeyPress directly (bug #500900).
def _write(self, text, editable=False)
Write given text to buffer.
def _changeLine(self, text)
Replace currently entered command line with given text.
def showPrompt(self, prompt)
Prints prompt at start of line.
def showReturned(self, text)
Show returned text from last command and print new prompt.
def onKeyPress(self, widget, event)
Key press callback used for correcting behavior for console-like interfaces.
def onKeyPressExtend(self, event)
Key press callback with plenty of shell goodness, like history, autocompletions, etc.
def raw_input(self, prompt='')
Custom raw_input() replacement.
def _processLine(self)
Process current command line.
def updateNamespace(self, ns_dict)
Add the current dictionary to the shell namespace.
def __init__(self, argv=None, user_ns=None, user_global_ns=None, cin=None, cout=None, cerr=None, input_func=None)
Initializer.
def _getHistory(self)
Gets the command string of the current history level.
def __update_namespace(self)
Update self.IP namespace for autocompletion with sys.modules.
def historyBack(self)
Provides one history command back.
history_level
history level
def generatePrompt(self, is_continuation)
Generate prompt depending on is_continuation value.
def historyForward(self)
Provides one history command forward.
def shell(self, cmd, verbose=0, debug=0, header='')
Replacement method to allow shell commands without them blocking.
def execute(self)
Executes the current line provided by the shell object.
def complete(self, line)
Returns an auto completed line and/or possibilities for completion.