def test_system_datadir_unsupportedos(self, monkeypatch, tmpdir, fake_args): """Test that system-wide path is not used on non-Linux OS.""" fake_args.basedir = str(tmpdir) standarddir.init(fake_args) monkeypatch.setattr('sys.platform', "potato") assert standarddir.system_data() == standarddir.data()
def run_async(tab, cmd, *args, win_id, env, verbose=False): """Run a userscript after dumping page html/source. Raises: UnsupportedError if userscripts are not supported on the current platform. Args: tab: The WebKitTab/WebEngineTab to get the source from. cmd: The userscript binary to run. *args: The arguments to pass to the userscript. win_id: The window id the userscript is executed in. env: A dictionary of variables to add to the process environment. verbose: Show notifications when the command started/exited. """ tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) commandrunner = runners.CommandRunner(win_id, parent=tabbed_browser) if os.name == 'posix': runner = _POSIXUserscriptRunner(tabbed_browser) elif os.name == 'nt': # pragma: no cover runner = _WindowsUserscriptRunner(tabbed_browser) else: # pragma: no cover raise UnsupportedError runner.got_cmd.connect(lambda cmd: log.commands.debug( "Got userscript command: {}".format(cmd))) runner.got_cmd.connect(commandrunner.run_safely) user_agent = config.get('network', 'user-agent') if user_agent is not None: env['QUTE_USER_AGENT'] = user_agent config_dir = standarddir.config() if config_dir is not None: env['QUTE_CONFIG_DIR'] = config_dir data_dir = standarddir.data() if data_dir is not None: env['QUTE_DATA_DIR'] = data_dir download_dir = downloads.download_dir() if download_dir is not None: env['QUTE_DOWNLOAD_DIR'] = download_dir cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) if not os.path.exists(cmd_path): cmd_path = os.path.join(standarddir.system_data(), "userscripts", cmd) log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(runner.deleteLater) runner.prepare_run(cmd_path, *args, env=env, verbose=verbose) tab.dump_async(runner.store_html) tab.dump_async(runner.store_text, plain=True)
def run(cmd, *args, win_id, env, verbose=False): """Convenience method to run a userscript. Args: cmd: The userscript binary to run. *args: The arguments to pass to the userscript. win_id: The window id the userscript is executed in. env: A dictionary of variables to add to the process environment. verbose: Show notifications when the command started/exited. """ tabbed_browser = objreg.get("tabbed-browser", scope="window", window=win_id) commandrunner = runners.CommandRunner(win_id, tabbed_browser) runner = UserscriptRunner(win_id, tabbed_browser) runner.got_cmd.connect(lambda cmd: log.commands.debug("Got userscript command: {}".format(cmd))) runner.got_cmd.connect(commandrunner.run_safely) user_agent = config.get("network", "user-agent") if user_agent is not None: env["QUTE_USER_AGENT"] = user_agent cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) if not os.path.exists(cmd_path): cmd_path = os.path.join(standarddir.system_data(), "userscripts", cmd) log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.run(cmd_path, *args, env=env, verbose=verbose) runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(runner.deleteLater)
def run_async(tab, cmd, *args, win_id, env, verbose=False): """Run a userscript after dumping page html/source. Raises: UnsupportedError if userscripts are not supported on the current platform. Args: tab: The WebKitTab/WebEngineTab to get the source from. cmd: The userscript binary to run. *args: The arguments to pass to the userscript. win_id: The window id the userscript is executed in. env: A dictionary of variables to add to the process environment. verbose: Show notifications when the command started/exited. """ tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) commandrunner = runners.CommandRunner(win_id, parent=tabbed_browser) if os.name == 'posix': runner = _POSIXUserscriptRunner(win_id, tabbed_browser) elif os.name == 'nt': # pragma: no cover runner = _WindowsUserscriptRunner(win_id, tabbed_browser) else: # pragma: no cover raise UnsupportedError runner.got_cmd.connect( lambda cmd: log.commands.debug("Got userscript command: {}".format(cmd))) runner.got_cmd.connect(commandrunner.run_safely) user_agent = config.get('network', 'user-agent') if user_agent is not None: env['QUTE_USER_AGENT'] = user_agent config_dir = standarddir.config() if config_dir is not None: env['QUTE_CONFIG_DIR'] = config_dir data_dir = standarddir.data() if data_dir is not None: env['QUTE_DATA_DIR'] = data_dir download_dir = downloads.download_dir() if download_dir is not None: env['QUTE_DOWNLOAD_DIR'] = download_dir cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) if not os.path.exists(cmd_path): cmd_path = os.path.join(standarddir.system_data(), "userscripts", cmd) log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(runner.deleteLater) runner.prepare_run(cmd_path, *args, env=env, verbose=verbose) tab.dump_async(runner.store_html) tab.dump_async(runner.store_text, plain=True)
def test_system_datadir_not_exist_linux(self, monkeypatch, tmpdir, fake_args): """Test that system-wide path isn't used on linux if path not exist.""" fake_args.basedir = str(tmpdir) standarddir.init(fake_args) monkeypatch.setattr(os.path, 'exists', lambda path: False) assert standarddir.system_data() == standarddir.data()
def _path_info(): """Get info about important path names. Return: A dictionary of descriptive to actual path names. """ return { 'config': standarddir.config(), 'data': standarddir.data(), 'system_data': standarddir.system_data(), 'cache': standarddir.cache(), 'download': standarddir.download(), 'runtime': standarddir.runtime(), }
def _lookup_path(cmd): """Search userscript directories for given command. Raises: NotFoundError if the command could not be found. Args: cmd: The command to look for. Returns: A path to the userscript. """ directories = [ os.path.join(standarddir.data(), "userscripts"), os.path.join(standarddir.system_data(), "userscripts"), ] for directory in directories: cmd_path = os.path.join(directory, cmd) if os.path.exists(cmd_path): return cmd_path raise NotFoundError(cmd, directories)
def run(cmd, *args, win_id, env, verbose=False): """Convenience method to run a userscript. Args: cmd: The userscript binary to run. *args: The arguments to pass to the userscript. win_id: The window id the userscript is executed in. env: A dictionary of variables to add to the process environment. verbose: Show notifications when the command started/exited. """ tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) commandrunner = runners.CommandRunner(win_id, parent=tabbed_browser) runner = UserscriptRunner(win_id, tabbed_browser) runner.got_cmd.connect(lambda cmd: log.commands.debug( "Got userscript command: {}".format(cmd))) runner.got_cmd.connect(commandrunner.run_safely) user_agent = config.get('network', 'user-agent') if user_agent is not None: env['QUTE_USER_AGENT'] = user_agent cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/qutebrowser/userscripts (or $XDG_DATA_DIR) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) cmd_path = os.path.join(standarddir.data(), "userscripts", cmd) if not os.path.exists(cmd_path): cmd_path = os.path.join(standarddir.system_data(), "userscripts", cmd) log.misc.debug("Userscript to run: {}".format(cmd_path)) runner.run(cmd_path, *args, env=env, verbose=verbose) runner.finished.connect(commandrunner.deleteLater) runner.finished.connect(runner.deleteLater)
def test_system_datadir_not_exist_linux(self, monkeypatch): """Test that system-wide path isn't used on linux if path not exist.""" monkeypatch.setattr(os.path, 'exists', lambda path: False) assert standarddir.system_data() == standarddir.data()
def test_system_datadir_exist_linux(self, monkeypatch): """Test that /usr/share/qutebrowser is used if path exists.""" monkeypatch.setattr('sys.platform', "linux") monkeypatch.setattr(os.path, 'exists', lambda path: True) assert standarddir.system_data() == "/usr/share/qutebrowser"
def test_system_datadir_unsupportedos(self, monkeypatch): """Test that system-wide path is not used on non-Linux OS.""" monkeypatch.setattr('sys.platform', "potato") assert standarddir.system_data() == standarddir.data()