def proc(qtbot, caplog): """A fixture providing a GUIProcess and cleaning it up after the test.""" p = guiprocess.GUIProcess('testprocess') yield p if p._proc.state() == QProcess.Running: with caplog.at_level(logging.ERROR): with qtbot.waitSignal(p.finished, timeout=10000, raising=False) as blocker: p._proc.terminate() if not blocker.signal_triggered: p._proc.kill() p._proc.waitForFinished()
def _run_process(self, cmd, *args, env=None, verbose=False): """Start the given command. Args: cmd: The command to be started. *args: The arguments to hand to the command env: A dictionary of environment variables to add. verbose: Show notifications when the command started/exited. """ self._env['QUTE_FIFO'] = self._filepath if env is not None: self._env.update(env) self._proc = guiprocess.GUIProcess('userscript', additional_env=self._env, verbose=verbose, parent=self) self._proc.finished.connect(self.on_proc_finished) self._proc.error.connect(self.on_proc_error) self._proc.start(cmd, args)
def open_file(filename, cmdline=None): """Open the given file. If cmdline is not given, downloads.open_dispatcher is used. If open_dispatcher is unset, the system's default application is used. Args: filename: The filename to open. cmdline: The command to use as string. A `{}` is expanded to the filename. None means to use the system's default application or `downloads.open_dispatcher` if set. If no `{}` is found, the filename is appended to the cmdline. """ # Import late to avoid circular imports: # - usertypes -> utils -> guiprocess -> message -> usertypes # - usertypes -> utils -> config -> configdata -> configtypes -> # cmdutils -> command -> message -> usertypes from glimpsebrowser.config import config from glimpsebrowser.misc import guiprocess # the default program to open downloads with - will be empty string # if we want to use the default override = config.val.downloads.open_dispatcher # precedence order: cmdline > downloads.open_dispatcher > openUrl if cmdline is None and not override: log.misc.debug( "Opening {} with the system application".format(filename)) url = QUrl.fromLocalFile(filename) QDesktopServices.openUrl(url) return if cmdline is None and override: cmdline = override cmd, *args = shlex.split(cmdline) args = [arg.replace('{}', filename) for arg in args] if '{}' not in cmdline: args.append(filename) log.misc.debug("Opening {} with {}".format(filename, [cmd] + args)) proc = guiprocess.GUIProcess(what='open-file') proc.start_detached(cmd, args)
def _start_editor(self, line=1, column=1): """Start the editor with the file opened as self._filename. Args: line: the line number to pass to the editor column: the column number to pass to the editor """ self._proc = guiprocess.GUIProcess(what='editor', parent=self) self._proc.finished.connect(self._on_proc_closed) self._proc.error.connect(self._on_proc_error) editor = config.val.editor.command executable = editor[0] if self._watcher: ok = self._watcher.addPath(self._filename) if not ok: log.procs.error("Failed to watch path: {}".format( self._filename)) self._watcher.fileChanged.connect(self._on_file_changed) args = [self._sub_placeholder(arg, line, column) for arg in editor[1:]] log.procs.debug("Calling \"{}\" with args {}".format(executable, args)) self._proc.start(executable, args)
def test_start_env(monkeypatch, qtbot, py_proc): monkeypatch.setenv('QUTEBROWSER_TEST_1', '1') env = {'QUTEBROWSER_TEST_2': '2'} proc = guiprocess.GUIProcess('testprocess', additional_env=env) argv = py_proc(""" import os import json import sys env = dict(os.environ) print(json.dumps(env)) sys.exit(0) """) with qtbot.waitSignals([proc.started, proc.finished], timeout=10000, order='strict'): proc.start(*argv) data = glimpsescheme.spawn_output assert 'QUTEBROWSER_TEST_1' in data assert 'QUTEBROWSER_TEST_2' in data
def fake_proc(monkeypatch, stubs): """A fixture providing a GUIProcess with a mocked QProcess.""" p = guiprocess.GUIProcess('testprocess') monkeypatch.setattr(p, '_proc', stubs.fake_qprocess()) return p