<s> """<STR_LIT>""" <EOL> import os <EOL> import signal <EOL> import sys <EOL> from IPython . external . qt import QtGui <EOL> from pygments . styles import get_all_styles <EOL> from IPython . config . application import boolean_flag <EOL> from IPython . core . application import BaseIPythonApplication <EOL> from IPython . core . profiledir import ProfileDir <EOL> from IPython . frontend . qt . console . frontend_widget import FrontendWidget <EOL> from IPython . frontend . qt . console . ipython_widget import IPythonWidget <EOL> from IPython . frontend . qt . console . rich_ipython_widget import RichIPythonWidget <EOL> from IPython . frontend . qt . console import styles <EOL> from IPython . frontend . qt . kernelmanager import QtKernelManager <EOL> from IPython . utils . traitlets import ( <EOL> Dict , List , Unicode , Int , CaselessStrEnum , CBool , Any <EOL> ) <EOL> from IPython . zmq . ipkernel import ( <EOL> flags as ipkernel_flags , <EOL> aliases as ipkernel_aliases , <EOL> IPKernelApp <EOL> ) <EOL> from IPython . zmq . session import Session <EOL> from IPython . zmq . zmqshell import ZMQInteractiveShell <EOL> from IPython . utils . localinterfaces import LOCALHOST , LOCAL_IPS <EOL> _examples = """<STR_LIT>""" <EOL> class MainWindow ( QtGui . QMainWindow ) : <EOL> def __init__ ( self , app , frontend , existing = False , may_close = True , <EOL> confirm_exit = True ) : <EOL> """<STR_LIT>""" <EOL> super ( MainWindow , self ) . __init__ ( ) <EOL> self . _app = app <EOL> self . _frontend = frontend <EOL> self . _existing = existing <EOL> if existing : <EOL> self . _may_close = may_close <EOL> else : <EOL> self . _may_close = True <EOL> self . _frontend . exit_requested . connect ( self . close ) <EOL> self . _confirm_exit = confirm_exit <EOL> self . setCentralWidget ( frontend ) <EOL> def closeEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> keepkernel = None <EOL> if hasattr ( self . _frontend , '<STR_LIT>' ) : <EOL> keepkernel = self . _frontend . _keep_kernel_on_exit <EOL> kernel_manager = self . _frontend . kernel_manager <EOL> if keepkernel is None and not self . _confirm_exit : <EOL> keepkernel = not self . _existing <EOL> if keepkernel is None : <EOL> if kernel_manager and kernel_manager . channels_running : <EOL> title = self . window ( ) . windowTitle ( ) <EOL> cancel = QtGui . QMessageBox . Cancel <EOL> okay = QtGui . QMessageBox . Ok <EOL> if self . _may_close : <EOL> msg = "<STR_LIT>" <EOL> info = "<STR_LIT>" <EOL> justthis = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> justthis . setShortcut ( '<STR_LIT:N>' ) <EOL> closeall = QtGui . QPushButton ( "<STR_LIT>" , self ) <EOL> closeall . setShortcut ( '<STR_LIT:Y>' ) <EOL> box = QtGui . QMessageBox ( QtGui . QMessageBox . Question , <EOL> title , msg ) <EOL> box . setInformativeText ( info ) <EOL> box . addButton ( cancel ) <EOL> box . addButton ( justthis , QtGui . QMessageBox . NoRole ) <EOL> box . addButton ( closeall , QtGui . QMessageBox . YesRole ) <EOL> box . setDefaultButton ( closeall ) <EOL> box . setEscapeButton ( cancel ) <EOL> reply = box . exec_ ( ) <EOL> if reply == <NUM_LIT:1> : <EOL> kernel_manager . shutdown_kernel ( ) <EOL> event . accept ( ) <EOL> elif reply == <NUM_LIT:0> : <EOL> if not self . _existing : <EOL> self . _app . setQuitOnLastWindowClosed ( False ) <EOL> self . deleteLater ( ) <EOL> event . accept ( ) <EOL> else : <EOL> event . ignore ( ) <EOL> else : <EOL> reply = QtGui . QMessageBox . question ( self , title , <EOL> "<STR_LIT>" + <EOL> "<STR_LIT>" , <EOL> okay | cancel , <EOL> defaultButton = okay <EOL> ) <EOL> if reply == okay : <EOL> event . accept ( ) <EOL> else : <EOL> event . ignore ( ) <EOL> elif keepkernel : <EOL> if kernel_manager and kernel_manager . channels_running : <EOL> if not self . _existing : <EOL> self . _app . setQuitOnLastWindowClosed ( False ) <EOL> event . accept ( ) <EOL> else : <EOL> if kernel_manager and kernel_manager . channels_running : <EOL> kernel_manager . shutdown_kernel ( ) <EOL> event . accept ( ) <EOL> flags = dict ( ipkernel_flags ) <EOL> qt_flags = { <EOL> '<STR_LIT>' : ( { '<STR_LIT>' : { '<STR_LIT>' : True } } , <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( { '<STR_LIT>' : { '<STR_LIT>' : True } } , <EOL> "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( { '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } } , <EOL> "<STR_LIT>" ) , <EOL> } <EOL> qt_flags . update ( boolean_flag ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ) ) <EOL> qt_flags . update ( boolean_flag ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> """<STR_LIT>""" , <EOL> """<STR_LIT>""" <EOL> ) ) <EOL> flags . update ( qt_flags ) <EOL> qt_flags = list ( qt_flags . keys ( ) ) <EOL> aliases = dict ( ipkernel_aliases ) <EOL> qt_aliases = dict ( <EOL> hb = '<STR_LIT>' , <EOL> shell = '<STR_LIT>' , <EOL> iopub = '<STR_LIT>' , <EOL> stdin = '<STR_LIT>' , <EOL> ip = '<STR_LIT>' , <EOL> style = '<STR_LIT>' , <EOL> stylesheet = '<STR_LIT>' , <EOL> colors = '<STR_LIT>' , <EOL> editor = '<STR_LIT>' , <EOL> paging = '<STR_LIT>' , <EOL> ) <EOL> aliases . update ( qt_aliases ) <EOL> qt_flags . extend ( list ( qt_aliases . keys ( ) ) ) <EOL> class IPythonQtConsoleApp ( BaseIPythonApplication ) : <EOL> name = '<STR_LIT>' <EOL> default_config_file_name = '<STR_LIT>' <EOL> description = """<STR_LIT>""" <EOL> examples = _examples <EOL> classes = [ IPKernelApp , IPythonWidget , ZMQInteractiveShell , ProfileDir , Session ] <EOL> flags = Dict ( flags ) <EOL> aliases = Dict ( aliases ) <EOL> kernel_argv = List ( Unicode ) <EOL> auto_create = CBool ( True ) <EOL> ip = Unicode ( LOCALHOST , config = True , <EOL> help = """<STR_LIT>""" <EOL> ) <EOL> hb_port = Int ( <NUM_LIT:0> , config = True , <EOL> help = "<STR_LIT>" ) <EOL> shell_port = Int ( <NUM_LIT:0> , config = True , <EOL> help = "<STR_LIT>" ) <EOL> iopub_port = Int ( <NUM_LIT:0> , config = True , <EOL> help = "<STR_LIT>" ) <EOL> stdin_port = Int ( <NUM_LIT:0> , config = True , <EOL> help = "<STR_LIT>" ) <EOL> existing = CBool ( False , config = True , <EOL> help = "<STR_LIT>" ) <EOL> stylesheet = Unicode ( '<STR_LIT>' , config = True , <EOL> help = "<STR_LIT>" ) <EOL> pure = CBool ( False , config = True , <EOL> help = "<STR_LIT>" ) <EOL> plain = CBool ( False , config = True , <EOL> help = "<STR_LIT>" ) <EOL> def _pure_changed ( self , name , old , new ) : <EOL> kind = '<STR_LIT>' if self . plain else '<STR_LIT>' <EOL> self . config . ConsoleWidget . kind = kind <EOL> if self . pure : <EOL> self . widget_factory = FrontendWidget <EOL> elif self . plain : <EOL> self . widget_factory = IPythonWidget <EOL> else : <EOL> self . widget_factory = RichIPythonWidget <EOL> _plain_changed = _pure_changed <EOL> confirm_exit = CBool ( True , config = True , <EOL> help = """<STR_LIT>""" , <EOL> ) <EOL> widget_factory = Any ( RichIPythonWidget ) <EOL> def parse_command_line ( self , argv = None ) : <EOL> super ( IPythonQtConsoleApp , self ) . parse_command_line ( argv ) <EOL> if argv is None : <EOL> argv = sys . argv [ <NUM_LIT:1> : ] <EOL> self . kernel_argv = list ( argv ) <EOL> self . kernel_argv . append ( "<STR_LIT>" % self . name ) <EOL> for a in argv : <EOL> if a . startswith ( '<STR_LIT:->' ) : <EOL> key = a . lstrip ( '<STR_LIT:->' ) . split ( '<STR_LIT:=>' ) [ <NUM_LIT:0> ] <EOL> if key in qt_flags : <EOL> self . kernel_argv . remove ( a ) <EOL> def init_kernel_manager ( self ) : <EOL> signal . signal ( signal . SIGINT , signal . SIG_DFL ) <EOL> self . kernel_manager = QtKernelManager ( <EOL> shell_address = ( self . ip , self . shell_port ) , <EOL> sub_address = ( self . ip , self . iopub_port ) , <EOL> stdin_address = ( self . ip , self . stdin_port ) , <EOL> hb_address = ( self . ip , self . hb_port ) , <EOL> config = self . config <EOL> ) <EOL> if not self . existing : <EOL> kwargs = dict ( ip = self . ip , ipython = not self . pure ) <EOL> kwargs [ '<STR_LIT>' ] = self . kernel_argv <EOL> self . kernel_manager . start_kernel ( ** kwargs ) <EOL> self . kernel_manager . start_channels ( ) <EOL> def init_qt_elements ( self ) : <EOL> self . app = QtGui . QApplication ( [ ] ) <EOL> local_kernel = ( not self . existing ) or self . ip in LOCAL_IPS <EOL> self . widget = self . widget_factory ( config = self . config , <EOL> local_kernel = local_kernel ) <EOL> self . widget . kernel_manager = self . kernel_manager <EOL> self . window = MainWindow ( self . app , self . widget , self . existing , <EOL> may_close = local_kernel , <EOL> confirm_exit = self . confirm_exit ) <EOL> self . window . setWindowTitle ( '<STR_LIT>' if self . pure else '<STR_LIT>' ) <EOL> def init_colors ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . pure : <EOL> return <EOL> try : <EOL> colors = self . config . ZMQInteractiveShell . colors <EOL> except AttributeError : <EOL> colors = None <EOL> try : <EOL> style = self . config . IPythonWidget . colors <EOL> except AttributeError : <EOL> style = None <EOL> if colors : <EOL> colors = colors . lower ( ) <EOL> if colors in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> colors = '<STR_LIT>' <EOL> elif colors in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> colors = '<STR_LIT>' <EOL> else : <EOL> colors = '<STR_LIT>' <EOL> elif style : <EOL> if style == '<STR_LIT>' : <EOL> colors = '<STR_LIT>' <EOL> elif styles . dark_style ( style ) : <EOL> colors = '<STR_LIT>' <EOL> else : <EOL> colors = '<STR_LIT>' <EOL> else : <EOL> colors = None <EOL> widget = self . widget <EOL> if style : <EOL> widget . style_sheet = styles . sheet_from_template ( style , colors ) <EOL> widget . syntax_style = style <EOL> widget . _syntax_style_changed ( ) <EOL> widget . _style_sheet_changed ( ) <EOL> elif colors : <EOL> widget . set_default_style ( colors = colors ) <EOL> else : <EOL> widget . set_default_style ( ) <EOL> if self . stylesheet : <EOL> if os . path . isfile ( self . stylesheet ) : <EOL> with open ( self . stylesheet ) as f : <EOL> sheet = f . read ( ) <EOL> widget . style_sheet = sheet <EOL> widget . _style_sheet_changed ( ) <EOL> else : <EOL> raise IOError ( "<STR_LIT>" % self . stylesheet ) <EOL> def initialize ( self , argv = None ) : <EOL> super ( IPythonQtConsoleApp , self ) . initialize ( argv ) <EOL> self . init_kernel_manager ( ) <EOL> self . init_qt_elements ( ) <EOL> self . init_colors ( ) <EOL> def start ( self ) : <EOL> self . window . show ( ) <EOL> self . app . exec_ ( ) <EOL> def main ( ) : <EOL> app = IPythonQtConsoleApp ( ) <EOL> app . initialize ( ) <EOL> app . start ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
