<s> from collections import namedtuple <EOL> import sys <EOL> import time <EOL> from pygments . lexers import PythonLexer <EOL> from IPython . external . qt import QtCore , QtGui <EOL> from IPython . core . inputsplitter import InputSplitter , transform_classic_prompt <EOL> from IPython . core . oinspect import call_tip <EOL> from IPython . frontend . qt . base_frontend_mixin import BaseFrontendMixin <EOL> from IPython . utils . traitlets import Bool , Instance <EOL> from . bracket_matcher import BracketMatcher <EOL> from . call_tip_widget import CallTipWidget <EOL> from . completion_lexer import CompletionLexer <EOL> from . history_console_widget import HistoryConsoleWidget <EOL> from . pygments_highlighter import PygmentsHighlighter <EOL> class FrontendHighlighter ( PygmentsHighlighter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , frontend ) : <EOL> super ( FrontendHighlighter , self ) . __init__ ( frontend . _control . document ( ) ) <EOL> self . _current_offset = <NUM_LIT:0> <EOL> self . _frontend = frontend <EOL> self . highlighting_on = False <EOL> def highlightBlock ( self , string ) : <EOL> """<STR_LIT>""" <EOL> if not self . highlighting_on : <EOL> return <EOL> current_block = self . currentBlock ( ) <EOL> string = self . _frontend . _get_block_plain_text ( current_block ) <EOL> if current_block . contains ( self . _frontend . _prompt_pos ) : <EOL> prompt = self . _frontend . _prompt <EOL> else : <EOL> prompt = self . _frontend . _continuation_prompt <EOL> if string . startswith ( prompt ) : <EOL> self . _current_offset = len ( prompt ) <EOL> string = string [ len ( prompt ) : ] <EOL> super ( FrontendHighlighter , self ) . highlightBlock ( string ) <EOL> def rehighlightBlock ( self , block ) : <EOL> """<STR_LIT>""" <EOL> old = self . highlighting_on <EOL> self . highlighting_on = True <EOL> super ( FrontendHighlighter , self ) . rehighlightBlock ( block ) <EOL> self . highlighting_on = old <EOL> def setFormat ( self , start , count , format ) : <EOL> """<STR_LIT>""" <EOL> start += self . _current_offset <EOL> super ( FrontendHighlighter , self ) . setFormat ( start , count , format ) <EOL> class FrontendWidget ( HistoryConsoleWidget , BaseFrontendMixin ) : <EOL> """<STR_LIT>""" <EOL> custom_interrupt = Bool ( False ) <EOL> custom_interrupt_requested = QtCore . Signal ( ) <EOL> custom_restart = Bool ( False ) <EOL> custom_restart_kernel_died = QtCore . Signal ( float ) <EOL> custom_restart_requested = QtCore . Signal ( ) <EOL> executing = QtCore . Signal ( object ) <EOL> executed = QtCore . Signal ( object ) <EOL> exit_requested = QtCore . Signal ( ) <EOL> _CallTipRequest = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> _CompletionRequest = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> _ExecutionRequest = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:id>' , '<STR_LIT>' ] ) <EOL> _input_splitter_class = InputSplitter <EOL> _local_kernel = False <EOL> _highlighter = Instance ( FrontendHighlighter ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( FrontendWidget , self ) . __init__ ( * args , ** kw ) <EOL> self . _bracket_matcher = BracketMatcher ( self . _control ) <EOL> self . _call_tip_widget = CallTipWidget ( self . _control ) <EOL> self . _completion_lexer = CompletionLexer ( PythonLexer ( ) ) <EOL> self . _copy_raw_action = QtGui . QAction ( '<STR_LIT>' , None ) <EOL> self . _hidden = False <EOL> self . _highlighter = FrontendHighlighter ( self ) <EOL> self . _input_splitter = self . _input_splitter_class ( input_mode = '<STR_LIT>' ) <EOL> self . _kernel_manager = None <EOL> self . _request_info = { } <EOL> self . tab_width = <NUM_LIT:4> <EOL> self . _set_continuation_prompt ( '<STR_LIT>' ) <EOL> self . _call_tip_widget . setFont ( self . font ) <EOL> self . font_changed . connect ( self . _call_tip_widget . setFont ) <EOL> action = self . _copy_raw_action <EOL> key = QtCore . Qt . CTRL | QtCore . Qt . SHIFT | QtCore . Qt . Key_C <EOL> action . setEnabled ( False ) <EOL> action . setShortcut ( QtGui . QKeySequence ( key ) ) <EOL> action . setShortcutContext ( QtCore . Qt . WidgetWithChildrenShortcut ) <EOL> action . triggered . connect ( self . copy_raw ) <EOL> self . copy_available . connect ( action . setEnabled ) <EOL> self . addAction ( action ) <EOL> document = self . _control . document ( ) <EOL> document . contentsChange . connect ( self . _document_contents_change ) <EOL> self . _local_kernel = kw . get ( '<STR_LIT>' , <EOL> FrontendWidget . _local_kernel ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> text = self . _control . textCursor ( ) . selection ( ) . toPlainText ( ) <EOL> if text : <EOL> lines = list ( map ( transform_classic_prompt , text . splitlines ( ) ) ) <EOL> text = '<STR_LIT:\n>' . join ( lines ) <EOL> QtGui . QApplication . clipboard ( ) . setText ( text ) <EOL> def _is_complete ( self , source , interactive ) : <EOL> """<STR_LIT>""" <EOL> complete = self . _input_splitter . push ( source ) <EOL> if interactive : <EOL> complete = not self . _input_splitter . push_accepts_more ( ) <EOL> return complete <EOL> def _execute ( self , source , hidden ) : <EOL> """<STR_LIT>""" <EOL> msg_id = self . kernel_manager . shell_channel . execute ( source , hidden ) <EOL> self . _request_info [ '<STR_LIT>' ] = self . _ExecutionRequest ( msg_id , '<STR_LIT:user>' ) <EOL> self . _hidden = hidden <EOL> if not hidden : <EOL> self . executing . emit ( source ) <EOL> def _prompt_started_hook ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _reading : <EOL> self . _highlighter . highlighting_on = True <EOL> def _prompt_finished_hook ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _input_splitter . reset ( ) <EOL> if not self . _reading : <EOL> self . _highlighter . highlighting_on = False <EOL> def _tab_pressed ( self ) : <EOL> """<STR_LIT>""" <EOL> text = self . _get_input_buffer_cursor_line ( ) <EOL> if text is None : <EOL> return False <EOL> complete = bool ( text [ : self . _get_input_buffer_cursor_column ( ) ] . strip ( ) ) <EOL> if complete : <EOL> self . _complete ( ) <EOL> return not complete <EOL> def _context_menu_make ( self , pos ) : <EOL> """<STR_LIT>""" <EOL> menu = super ( FrontendWidget , self ) . _context_menu_make ( pos ) <EOL> for before_action in menu . actions ( ) : <EOL> if before_action . shortcut ( ) . matches ( QtGui . QKeySequence . Paste ) == QtGui . QKeySequence . ExactMatch : <EOL> menu . insertAction ( before_action , self . _copy_raw_action ) <EOL> break <EOL> return menu <EOL> def _event_filter_console_keypress ( self , event ) : <EOL> """<STR_LIT>""" <EOL> key = event . key ( ) <EOL> if self . _control_key_down ( event . modifiers ( ) , include_command = False ) : <EOL> if key == QtCore . Qt . Key_C and self . _executing : <EOL> self . interrupt_kernel ( ) <EOL> return True <EOL> elif key == QtCore . Qt . Key_Period : <EOL> message = '<STR_LIT>' <EOL> self . restart_kernel ( message , now = False ) <EOL> return True <EOL> elif not event . modifiers ( ) & QtCore . Qt . AltModifier : <EOL> if key == QtCore . Qt . Key_Backspace : <EOL> col = self . _get_input_buffer_cursor_column ( ) <EOL> cursor = self . _control . textCursor ( ) <EOL> if col > <NUM_LIT:3> and not cursor . hasSelection ( ) : <EOL> text = self . _get_input_buffer_cursor_line ( ) [ : col ] <EOL> if text . endswith ( '<STR_LIT:U+0020>' ) and not text . strip ( ) : <EOL> cursor . movePosition ( QtGui . QTextCursor . Left , <EOL> QtGui . QTextCursor . KeepAnchor , <NUM_LIT:4> ) <EOL> cursor . removeSelectedText ( ) <EOL> return True <EOL> return super ( FrontendWidget , self ) . _event_filter_console_keypress ( event ) <EOL> def _insert_continuation_prompt ( self , cursor ) : <EOL> """<STR_LIT>""" <EOL> super ( FrontendWidget , self ) . _insert_continuation_prompt ( cursor ) <EOL> cursor . insertText ( '<STR_LIT:U+0020>' * self . _input_splitter . indent_spaces ) <EOL> def _handle_complete_reply ( self , rep ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _get_cursor ( ) <EOL> info = self . _request_info . get ( '<STR_LIT>' ) <EOL> if info and info . id == rep [ '<STR_LIT>' ] [ '<STR_LIT>' ] and info . pos == cursor . position ( ) : <EOL> text = '<STR_LIT:.>' . join ( self . _get_context ( ) ) <EOL> cursor . movePosition ( QtGui . QTextCursor . Left , n = len ( text ) ) <EOL> self . _complete_with_items ( cursor , rep [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] ) <EOL> def _handle_execute_reply ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> info = self . _request_info . get ( '<STR_LIT>' ) <EOL> if info and info . id == msg [ '<STR_LIT>' ] [ '<STR_LIT>' ] and info . kind == '<STR_LIT:user>' and not self . _hidden : <EOL> self . kernel_manager . sub_channel . flush ( ) <EOL> if self . ansi_codes : <EOL> self . _ansi_processor . reset_sgr ( ) <EOL> content = msg [ '<STR_LIT:content>' ] <EOL> status = content [ '<STR_LIT:status>' ] <EOL> if status == '<STR_LIT>' : <EOL> self . _process_execute_ok ( msg ) <EOL> elif status == '<STR_LIT:error>' : <EOL> self . _process_execute_error ( msg ) <EOL> elif status == '<STR_LIT>' : <EOL> self . _process_execute_abort ( msg ) <EOL> self . _show_interpreter_prompt_for_reply ( msg ) <EOL> self . executed . emit ( msg ) <EOL> def _handle_input_request ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if self . _hidden : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . kernel_manager . sub_channel . flush ( ) <EOL> def callback ( line ) : <EOL> self . kernel_manager . stdin_channel . input ( line ) <EOL> self . _readline ( msg [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] , callback = callback ) <EOL> def _handle_kernel_died ( self , since_last_heartbeat ) : <EOL> """<STR_LIT>""" <EOL> if self . custom_restart : <EOL> self . custom_restart_kernel_died . emit ( since_last_heartbeat ) <EOL> else : <EOL> message = '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' % since_last_heartbeat <EOL> self . restart_kernel ( message , now = True ) <EOL> def _handle_object_info_reply ( self , rep ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _get_cursor ( ) <EOL> info = self . _request_info . get ( '<STR_LIT>' ) <EOL> if info and info . id == rep [ '<STR_LIT>' ] [ '<STR_LIT>' ] and info . pos == cursor . position ( ) : <EOL> content = rep [ '<STR_LIT:content>' ] <EOL> if content . get ( '<STR_LIT>' , False ) : <EOL> call_info , doc = None , None <EOL> else : <EOL> call_info , doc = call_tip ( content , format_call = True ) <EOL> if call_info or doc : <EOL> self . _call_tip_widget . show_call_info ( call_info , doc ) <EOL> def _handle_pyout ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if not self . _hidden and self . _is_from_this_session ( msg ) : <EOL> text = msg [ '<STR_LIT:content>' ] [ '<STR_LIT:data>' ] <EOL> self . _append_plain_text ( text + '<STR_LIT:\n>' , before_prompt = True ) <EOL> def _handle_stream ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if not self . _hidden and self . _is_from_this_session ( msg ) : <EOL> text = msg [ '<STR_LIT:content>' ] [ '<STR_LIT:data>' ] . expandtabs ( <NUM_LIT:8> ) <EOL> self . _append_plain_text ( text , before_prompt = True ) <EOL> self . _control . moveCursor ( QtGui . QTextCursor . End ) <EOL> def _handle_shutdown_reply ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> if not self . _hidden and not self . _is_from_this_session ( msg ) : <EOL> if self . _local_kernel : <EOL> if not msg [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . reset ( ) <EOL> else : <EOL> title = self . window ( ) . windowTitle ( ) <EOL> if not msg [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] : <EOL> reply = QtGui . QMessageBox . question ( self , title , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> QtGui . QMessageBox . Yes , QtGui . QMessageBox . No ) <EOL> if reply == QtGui . QMessageBox . Yes : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> else : <EOL> reply = QtGui . QMessageBox . question ( self , title , <EOL> "<STR_LIT>" , <EOL> QtGui . QMessageBox . Yes , QtGui . QMessageBox . No ) <EOL> if reply == QtGui . QMessageBox . Yes : <EOL> time . sleep ( <NUM_LIT> ) <EOL> self . reset ( ) <EOL> def _started_channels ( self ) : <EOL> """<STR_LIT>""" <EOL> self . reset ( ) <EOL> def copy_raw ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _control . copy ( ) <EOL> def execute_file ( self , path , hidden = False ) : <EOL> """<STR_LIT>""" <EOL> self . execute ( '<STR_LIT>' % path , hidden = hidden ) <EOL> def interrupt_kernel ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . custom_interrupt : <EOL> self . custom_interrupt_requested . emit ( ) <EOL> elif self . kernel_manager . has_kernel : <EOL> self . kernel_manager . interrupt_kernel ( ) <EOL> else : <EOL> self . _append_plain_text ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _executing : <EOL> self . _executing = False <EOL> self . _request_info [ '<STR_LIT>' ] = None <EOL> self . _reading = False <EOL> self . _highlighter . highlighting_on = False <EOL> self . _control . clear ( ) <EOL> self . _append_plain_text ( self . _get_banner ( ) ) <EOL> self . _show_interpreter_prompt ( ) <EOL> def restart_kernel ( self , message , now = False ) : <EOL> """<STR_LIT>""" <EOL> if self . custom_restart : <EOL> self . custom_restart_requested . emit ( ) <EOL> elif self . kernel_manager . has_kernel : <EOL> self . kernel_manager . hb_channel . pause ( ) <EOL> buttons = QtGui . QMessageBox . Yes | QtGui . QMessageBox . No <EOL> result = QtGui . QMessageBox . question ( self , '<STR_LIT>' , <EOL> message , buttons ) <EOL> if result == QtGui . QMessageBox . Yes : <EOL> try : <EOL> self . kernel_manager . restart_kernel ( now = now ) <EOL> except RuntimeError : <EOL> self . _append_plain_text ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> self . reset ( ) <EOL> else : <EOL> self . kernel_manager . hb_channel . unpause ( ) <EOL> else : <EOL> self . _append_plain_text ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _call_tip ( self ) : <EOL> """<STR_LIT>""" <EOL> cursor = self . _get_cursor ( ) <EOL> cursor . movePosition ( QtGui . QTextCursor . Left ) <EOL> if cursor . document ( ) . characterAt ( cursor . position ( ) ) != '<STR_LIT:(>' : <EOL> return False <EOL> context = self . _get_context ( cursor ) <EOL> if not context : <EOL> return False <EOL> name = '<STR_LIT:.>' . join ( context ) <EOL> msg_id = self . kernel_manager . shell_channel . object_info ( name ) <EOL> pos = self . _get_cursor ( ) . position ( ) <EOL> self . _request_info [ '<STR_LIT>' ] = self . _CallTipRequest ( msg_id , pos ) <EOL> return True <EOL> def _complete ( self ) : <EOL> """<STR_LIT>""" <EOL> context = self . _get_context ( ) <EOL> if context : <EOL> msg_id = self . kernel_manager . shell_channel . complete ( <EOL> '<STR_LIT:.>' . join ( context ) , <EOL> self . _get_input_buffer_cursor_line ( ) , <EOL> self . _get_input_buffer_cursor_column ( ) , <EOL> self . input_buffer ) <EOL> pos = self . _get_cursor ( ) . position ( ) <EOL> info = self . _CompletionRequest ( msg_id , pos ) <EOL> self . _request_info [ '<STR_LIT>' ] = info <EOL> def _get_banner ( self ) : <EOL> """<STR_LIT>""" <EOL> banner = '<STR_LIT>' '<STR_LIT>' <EOL> return banner % ( sys . version , sys . platform ) <EOL> def _get_context ( self , cursor = None ) : <EOL> """<STR_LIT>""" <EOL> if cursor is None : <EOL> cursor = self . _get_cursor ( ) <EOL> cursor . movePosition ( QtGui . QTextCursor . StartOfBlock , <EOL> QtGui . QTextCursor . KeepAnchor ) <EOL> text = cursor . selection ( ) . toPlainText ( ) <EOL> return self . _completion_lexer . get_context ( text ) <EOL> def _process_execute_abort ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . _append_plain_text ( "<STR_LIT>" ) <EOL> def _process_execute_error ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> content = msg [ '<STR_LIT:content>' ] <EOL> if content [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> keepkernel = content [ '<STR_LIT>' ] == '<STR_LIT>' or content [ '<STR_LIT>' ] == '<STR_LIT:True>' <EOL> self . _keep_kernel_on_exit = keepkernel <EOL> self . exit_requested . emit ( ) <EOL> else : <EOL> traceback = '<STR_LIT>' . join ( content [ '<STR_LIT>' ] ) <EOL> self . _append_plain_text ( traceback ) <EOL> def _process_execute_ok ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> payload = msg [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] <EOL> for item in payload : <EOL> if not self . _process_execute_payload ( item ) : <EOL> warning = '<STR_LIT>' <EOL> print ( warning % repr ( item [ '<STR_LIT:source>' ] ) ) <EOL> def _process_execute_payload ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def _show_interpreter_prompt ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _show_prompt ( '<STR_LIT>' ) <EOL> def _show_interpreter_prompt_for_reply ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . _show_interpreter_prompt ( ) <EOL> def _document_contents_change ( self , position , removed , added ) : <EOL> """<STR_LIT>""" <EOL> position += added <EOL> document = self . _control . document ( ) <EOL> if position == self . _get_cursor ( ) . position ( ) : <EOL> self . _call_tip ( ) </s>
