def __init__(self, log_textbox, parent, log_file): """Initializes a LogReader with the default display configuration. Args: log_textbox (QtWidgets.QPlainTextEdit): Textbox where logs are displayed. parent (App(QDialog)): Object corresponding to the parent UI element. log_file (str): Path to the logging file on disk. """ super().__init__() self.parent = parent self.processes = {} self.decoder_stdout = QTextCodec.codecForLocale().makeDecoder() self.log_textbox = log_textbox self.cursor_output = self.log_textbox.textCursor() self.log_textbox.setReadOnly(True) self.log_textbox.setMaximumBlockCount(1000) # limit console to 1000 lines self.log_str = "" self.progress = "█" self.process_signal.connect(self.append_output) self.app_name = "" self.gb = None # If log part of a tab widget we can switch to that tab when we have logs to show self.tab_widget = None self.tab_index = -1 self.log_file = log_file os.makedirs(os.path.dirname(self.log_file), exist_ok=True) self.log_f = open(self.log_file, "w")
def run(self, argv, error_message, in_build_dir=False, timeout=30000): """ Execute a command and capture the output. """ if in_build_dir: project = self._project saved_cwd = os.getcwd() build_dir = project.path_from_user(project.build_dir) build_dir = QDir.toNativeSeparators(build_dir) os.chdir(build_dir) self._message_handler.verbose_message( "{0} is now the current directory".format(build_dir)) else: saved_cwd = None self._message_handler.verbose_message("Running '{0}'".format( ' '.join(argv))) QCoreApplication.processEvents() process = QProcess() process.readyReadStandardOutput.connect( lambda: self._message_handler.progress_message( QTextCodec.codecForLocale().toUnicode( process.readAllStandardOutput()).strip())) stderr_output = QByteArray() process.readyReadStandardError.connect( lambda: stderr_output.append(process.readAllStandardError())) process.start(argv[0], argv[1:]) finished = process.waitForFinished(timeout) if saved_cwd is not None: os.chdir(saved_cwd) self._message_handler.verbose_message( "{0} is now the current directory".format(saved_cwd)) if not finished: raise UserException(error_message, process.errorString()) if process.exitStatus() != QProcess.NormalExit or process.exitCode( ) != 0: raise UserException( error_message, QTextCodec.codecForLocale().toUnicode(stderr_output).strip())
def __init__(self, parent=None): super().__init__(parent=parent) self.setProcessChannelMode(QProcess.MergedChannels) codec = QTextCodec.codecForLocale() self._decoder_stdout = codec.makeDecoder() self.readyReadStandardOutput.connect(self._ready_read_standard_output)
def run(self, argv, error_message, in_build_dir=False): """ Execute a command and capture the output. """ if in_build_dir: project = self._project saved_cwd = os.getcwd() build_dir = project.path_from_user(project.build_dir) build_dir = QDir.toNativeSeparators(build_dir) os.chdir(build_dir) self._message_handler.verbose_message( "{0} is now the current directory".format(build_dir)) else: saved_cwd = None self._message_handler.verbose_message( "Running '{0}'".format(' '.join(argv))) QCoreApplication.processEvents() process = QProcess() process.readyReadStandardOutput.connect( lambda: self._message_handler.progress_message( QTextCodec.codecForLocale().toUnicode( process.readAllStandardOutput()).strip())) stderr_output = QByteArray() process.readyReadStandardError.connect( lambda: stderr_output.append(process.readAllStandardError())) process.start(argv[0], argv[1:]) finished = process.waitForFinished() if saved_cwd is not None: os.chdir(saved_cwd) self._message_handler.verbose_message( "{0} is now the current directory".format(saved_cwd)) if not finished: raise UserException(error_message, process.errorString()) if process.exitStatus() != QProcess.NormalExit or process.exitCode() != 0: raise UserException(error_message, QTextCodec.codecForLocale().toUnicode(stderr_output).strip())
def read(self): """ Reads the file and returns the content """ _file = QFile(self.filename) if not _file.open(QIODevice.ReadOnly | QIODevice.Text): raise Exception(_file.errorString()) # Codec codec = QTextCodec.codecForLocale() stream = QTextStream(_file) stream.setCodec(codec) return stream.readAll()
def __init__(self, parent=None): super().__init__(parent) self.profile = None self.started.connect(signals.node_started) self.started.connect(self.node_started) self.finished.connect(self.node_finished) self.finished.connect(signals.node_finished) self.errorOccurred.connect(self.node_error) self.errorOccurred.connect(signals.node_error) self.setProcessChannelMode(QProcess.MergedChannels) self.readyReadStandardOutput.connect(self.on_stdout_ready) self.decoder = QTextCodec.codecForLocale()
def __init__(self, parent=None): super().__init__(parent=parent) # merge stderr channel into stdout channel self.setProcessChannelMode(QProcess.MergedChannels) # prepare decoding process' output to Unicode codec = QTextCodec.codecForLocale() self._decoder_stdout = codec.makeDecoder() # only necessary when stderr channel isn't merged into stdout: # self._decoder_stderr = codec.makeDecoder() self.readyReadStandardOutput.connect(self._ready_read_standard_output)
def save(self, data, path=None): if path: self.filename = path self.is_new = False _file = QFile(self.filename) if not _file.open(QIODevice.WriteOnly | QIODevice.Truncate): raise Exception(_file.errorString()) stream = QTextStream(_file) stream.setCodec(QTextCodec.codecForLocale()) stream << data stream.flush() _file.close() # Emit the signal self.fileSaved.emit(self.filename)