<s> """<STR_LIT>""" <EOL> import locale <EOL> import logging <EOL> import sys <EOL> from pyqode . core . api . client import PROCESS_ERROR_STRING <EOL> from pyqode . core . managers . decorations import TextDecorationsManager <EOL> from pyqode . core . managers . panels import PanelsManager <EOL> from pyqode . qt . QtCore import Qt , Signal , QProcess , QProcessEnvironment <EOL> from pyqode . qt . QtWidgets import QTextEdit , QAction , QApplication <EOL> from pyqode . qt . QtGui import QColor , QTextCursor , QFont , QKeySequence <EOL> def _logger ( ) : <EOL> return logging . getLogger ( __name__ ) <EOL> class InteractiveConsole ( QTextEdit ) : <EOL> """<STR_LIT>""" <EOL> process_finished = Signal ( int ) <EOL> process_started = Signal ( ) <EOL> def __init__ ( self , parent = None ) : <EOL> super ( InteractiveConsole , self ) . __init__ ( parent ) <EOL> self . panels = PanelsManager ( self ) <EOL> self . decorations = TextDecorationsManager ( self ) <EOL> from pyqode . core . panels import SearchAndReplacePanel <EOL> self . panels . append ( SearchAndReplacePanel ( ) , <EOL> SearchAndReplacePanel . Position . TOP ) <EOL> self . _stdout_col = QColor ( "<STR_LIT>" ) <EOL> self . _app_msg_col = QColor ( "<STR_LIT>" ) <EOL> self . _stdin_col = QColor ( "<STR_LIT>" ) <EOL> self . _stderr_col = QColor ( "<STR_LIT>" ) <EOL> self . _write_app_messages = True <EOL> self . _process_name = '<STR_LIT>' <EOL> self . process = None <EOL> self . _args = None <EOL> self . _usr_buffer = "<STR_LIT>" <EOL> self . _clear_on_start = True <EOL> self . _merge_outputs = False <EOL> self . _running = False <EOL> self . _writer = self . write <EOL> self . _user_stop = False <EOL> font = "<STR_LIT>" <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> font = "<STR_LIT>" <EOL> elif sys . platform == "<STR_LIT>" : <EOL> font = '<STR_LIT>' <EOL> self . _font_family = font <EOL> self . setFont ( QFont ( font , <NUM_LIT:10> ) ) <EOL> self . setReadOnly ( True ) <EOL> self . _mask_user_input = False <EOL> action = QAction ( _ ( '<STR_LIT>' ) , self ) <EOL> action . setShortcut ( QKeySequence . Copy ) <EOL> action . triggered . connect ( self . copy ) <EOL> self . add_action ( action ) <EOL> action = QAction ( _ ( '<STR_LIT>' ) , self ) <EOL> action . setShortcut ( QKeySequence . Paste ) <EOL> action . triggered . connect ( self . paste ) <EOL> self . add_action ( action ) <EOL> def showEvent ( self , event ) : <EOL> super ( InteractiveConsole , self ) . showEvent ( event ) <EOL> self . panels . refresh ( ) <EOL> def resizeEvent ( self , e ) : <EOL> super ( InteractiveConsole , self ) . resizeEvent ( e ) <EOL> self . panels . resize ( ) <EOL> def add_action ( self , action ) : <EOL> self . addAction ( action ) <EOL> action . setShortcutContext ( Qt . WidgetShortcut ) <EOL> def set_writer ( self , writer ) : <EOL> """<STR_LIT>""" <EOL> if self . _writer != writer and self . _writer : <EOL> self . _writer = None <EOL> if writer : <EOL> self . _writer = writer <EOL> def _on_stdout ( self ) : <EOL> raw = bytes ( self . process . readAllStandardOutput ( ) ) <EOL> try : <EOL> txt = raw . decode ( sys . getfilesystemencoding ( ) ) <EOL> except UnicodeDecodeError : <EOL> txt = str ( raw ) . replace ( "<STR_LIT>" , '<STR_LIT>' ) [ : - <NUM_LIT:1> ] . replace ( <EOL> '<STR_LIT>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT>' , '<STR_LIT:\\>' ) <EOL> _logger ( ) . debug ( '<STR_LIT>' , txt ) <EOL> self . _writer ( self , txt , self . stdout_color ) <EOL> def _on_stderr ( self ) : <EOL> raw = bytes ( self . process . readAllStandardError ( ) ) <EOL> try : <EOL> txt = raw . decode ( sys . getfilesystemencoding ( ) ) <EOL> except UnicodeDecodeError : <EOL> txt = str ( raw ) . replace ( "<STR_LIT>" , '<STR_LIT>' ) [ : - <NUM_LIT:1> ] . replace ( <EOL> '<STR_LIT>' , '<STR_LIT:\n>' ) . replace ( '<STR_LIT>' , '<STR_LIT:\\>' ) <EOL> _logger ( ) . debug ( '<STR_LIT>' , txt ) <EOL> self . _writer ( self , txt , self . stderr_color ) <EOL> @ property <EOL> def exit_code ( self ) : <EOL> if self . is_running : <EOL> return None <EOL> exit_status = self . process . exitStatus ( ) <EOL> if exit_status == self . process . Crashed : <EOL> exit_code = <NUM_LIT> <EOL> else : <EOL> exit_code = self . process . exitCode ( ) <EOL> return exit_code <EOL> @ property <EOL> def write_app_messages ( self ) : <EOL> return self . _write_app_messages <EOL> @ write_app_messages . setter <EOL> def write_app_messages ( self , value ) : <EOL> self . _write_app_messages = value <EOL> @ property <EOL> def background_color ( self ) : <EOL> """<STR_LIT>""" <EOL> pal = self . palette ( ) <EOL> return pal . color ( pal . Base ) <EOL> @ background_color . setter <EOL> def background_color ( self , color ) : <EOL> pal = self . palette ( ) <EOL> pal . setColor ( pal . Base , color ) <EOL> pal . setColor ( pal . Text , self . stdout_color ) <EOL> self . setPalette ( pal ) <EOL> @ property <EOL> def stdout_color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stdout_col <EOL> @ stdout_color . setter <EOL> def stdout_color ( self , color ) : <EOL> self . _stdout_col = color <EOL> pal = self . palette ( ) <EOL> pal . setColor ( pal . Text , self . _stdout_col ) <EOL> self . setPalette ( pal ) <EOL> @ property <EOL> def stderr_color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stderr_col <EOL> @ stderr_color . setter <EOL> def stderr_color ( self , color ) : <EOL> self . _stderr_col = color <EOL> @ property <EOL> def stdin_color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stdin_col <EOL> @ stdin_color . setter <EOL> def stdin_color ( self , color ) : <EOL> self . _stdin_col = color <EOL> @ property <EOL> def app_msg_color ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _app_msg_col <EOL> @ app_msg_color . setter <EOL> def app_msg_color ( self , color ) : <EOL> self . _app_msg_col = color <EOL> @ property <EOL> def clear_on_start ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _clear_on_start <EOL> @ clear_on_start . setter <EOL> def clear_on_start ( self , value ) : <EOL> self . _clear_on_start = value <EOL> @ property <EOL> def merge_outputs ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _merge_outputs <EOL> @ merge_outputs . setter <EOL> def merge_outputs ( self , value ) : <EOL> self . _merge_outputs = value <EOL> if value : <EOL> self . process . setProcessChannelMode ( QProcess . MergedChannels ) <EOL> else : <EOL> self . process . setProcessChannelMode ( QProcess . SeparateChannels ) <EOL> @ property <EOL> def is_running ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _running <EOL> @ property <EOL> def mask_user_input ( self ) : <EOL> return self . _mask_user_input <EOL> @ mask_user_input . setter <EOL> def mask_user_input ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _mask_user_input = value <EOL> def closeEvent ( self , * args , ** kwargs ) : <EOL> if self . process and self . process . state ( ) == QProcess . Running : <EOL> self . process . terminate ( ) <EOL> def start_process ( self , process , args = None , cwd = None , env = None ) : <EOL> """<STR_LIT>""" <EOL> self . setReadOnly ( False ) <EOL> if env is None : <EOL> env = { } <EOL> if args is None : <EOL> args = [ ] <EOL> if not self . _running : <EOL> self . process = QProcess ( ) <EOL> self . process . finished . connect ( self . _on_process_finished ) <EOL> self . process . started . connect ( self . process_started . emit ) <EOL> self . process . error . connect ( self . _write_error ) <EOL> self . process . readyReadStandardError . connect ( self . _on_stderr ) <EOL> self . process . readyReadStandardOutput . connect ( self . _on_stdout ) <EOL> if cwd : <EOL> self . process . setWorkingDirectory ( cwd ) <EOL> e = self . process . systemEnvironment ( ) <EOL> ev = QProcessEnvironment ( ) <EOL> for v in e : <EOL> values = v . split ( '<STR_LIT:=>' ) <EOL> ev . insert ( values [ <NUM_LIT:0> ] , '<STR_LIT:=>' . join ( values [ <NUM_LIT:1> : ] ) ) <EOL> for k , v in env . items ( ) : <EOL> ev . insert ( k , v ) <EOL> self . process . setProcessEnvironment ( ev ) <EOL> self . _running = True <EOL> self . _process_name = process <EOL> self . _args = args <EOL> if self . _clear_on_start : <EOL> self . clear ( ) <EOL> self . _user_stop = False <EOL> self . _write_started ( ) <EOL> self . process . start ( process , args ) <EOL> self . process . waitForStarted ( ) <EOL> else : <EOL> _logger ( ) . warning ( '<STR_LIT>' ) <EOL> def stop_process ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . process is not None : <EOL> self . _user_stop = True <EOL> self . process . kill ( ) <EOL> self . setReadOnly ( True ) <EOL> self . _running = False <EOL> def get_user_buffer_as_bytes ( self ) : <EOL> """<STR_LIT>""" <EOL> return bytes ( self . _usr_buffer , locale . getpreferredencoding ( ) ) <EOL> def keyPressEvent ( self , event ) : <EOL> ctrl = event . modifiers ( ) & Qt . ControlModifier != <NUM_LIT:0> <EOL> if not self . is_running or self . textCursor ( ) . hasSelection ( ) : <EOL> if event . key ( ) == Qt . Key_C and ctrl : <EOL> self . copy ( ) <EOL> return <EOL> propagate_to_parent = True <EOL> delete = event . key ( ) in [ Qt . Key_Backspace , Qt . Key_Delete ] <EOL> if delete and not self . _usr_buffer : <EOL> return <EOL> if event . key ( ) == Qt . Key_V and ctrl : <EOL> text = QApplication . clipboard ( ) . text ( ) <EOL> self . _usr_buffer += text <EOL> self . setTextColor ( self . _stdin_col ) <EOL> if self . _mask_user_input : <EOL> text = len ( text ) * '<STR_LIT:*>' <EOL> self . insertPlainText ( text ) <EOL> return <EOL> if event . key ( ) in [ Qt . Key_Return , Qt . Key_Enter ] : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> self . _usr_buffer += "<STR_LIT:\r>" <EOL> self . _usr_buffer += "<STR_LIT:\n>" <EOL> self . process . write ( self . get_user_buffer_as_bytes ( ) ) <EOL> self . _usr_buffer = "<STR_LIT>" <EOL> else : <EOL> if not delete and len ( event . text ( ) ) : <EOL> txt = event . text ( ) <EOL> self . _usr_buffer += txt <EOL> if self . _mask_user_input : <EOL> txt = '<STR_LIT:*>' <EOL> self . setTextColor ( self . _stdin_col ) <EOL> self . insertPlainText ( txt ) <EOL> propagate_to_parent = False <EOL> elif delete : <EOL> self . _usr_buffer = self . _usr_buffer [ : len ( self . _usr_buffer ) - <NUM_LIT:1> ] <EOL> if propagate_to_parent : <EOL> super ( InteractiveConsole , self ) . keyPressEvent ( event ) <EOL> self . setTextColor ( self . _stdout_col ) <EOL> def _on_process_finished ( self , exit_code , exit_status ) : <EOL> if self is None : <EOL> return <EOL> self . _running = False <EOL> if not self . _user_stop : <EOL> if self . _write_app_messages : <EOL> self . _writer ( <EOL> self , "<STR_LIT>" % <EOL> self . exit_code , self . _app_msg_col ) <EOL> _logger ( ) . debug ( '<STR_LIT>' , <EOL> exit_code , exit_status ) <EOL> try : <EOL> self . process_finished . emit ( exit_code ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . setReadOnly ( True ) <EOL> def _write_started ( self ) : <EOL> if not self . _write_app_messages : <EOL> return <EOL> self . _writer ( self , "<STR_LIT>" . format ( <EOL> self . _process_name , "<STR_LIT:U+0020>" . join ( self . _args ) ) , self . _app_msg_col ) <EOL> self . _running = True <EOL> def _write_error ( self , error ) : <EOL> if self is None : <EOL> return <EOL> if self . _user_stop : <EOL> self . _writer ( self , '<STR_LIT>' , <EOL> self . app_msg_color ) <EOL> else : <EOL> err = PROCESS_ERROR_STRING [ error ] <EOL> self . _writer ( self , "<STR_LIT>" % err , self . stderr_color ) <EOL> _logger ( ) . warn ( '<STR_LIT>' , err ) <EOL> self . _running = False <EOL> @ staticmethod <EOL> def write ( text_edit , text , color ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> text_edit . moveCursor ( QTextCursor . End ) <EOL> text_edit . setTextColor ( color ) <EOL> text_edit . insertPlainText ( text ) <EOL> text_edit . moveCursor ( QTextCursor . End ) <EOL> except RuntimeError : <EOL> pass <EOL> def apply_color_scheme ( self , color_scheme ) : <EOL> """<STR_LIT>""" <EOL> self . stdout_color = color_scheme . formats [ '<STR_LIT>' ] . foreground ( ) . color ( ) <EOL> self . stdin_color = color_scheme . formats [ '<STR_LIT>' ] . foreground ( ) . color ( ) <EOL> self . app_msg_color = color_scheme . formats [ <EOL> '<STR_LIT:string>' ] . foreground ( ) . color ( ) <EOL> self . background_color = color_scheme . background <EOL> if self . background_color . lightness ( ) < <NUM_LIT> : <EOL> self . stderr_color = QColor ( '<STR_LIT>' ) <EOL> else : <EOL> self . stderr_color = QColor ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from pyqode . qt import QtWidgets <EOL> app = QtWidgets . QApplication ( [ ] ) <EOL> console = InteractiveConsole ( ) <EOL> console . start_process ( '<STR_LIT>' ) <EOL> console . show ( ) <EOL> app . exec_ ( ) </s>
