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) monkeypatch.setattr(os.path, 'exists', lambda path: False) standarddir._init_dirs(fake_args) assert standarddir.data(system=True) == standarddir.data()
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) monkeypatch.setattr('sys.platform', "potato") standarddir._init_dirs(fake_args) assert standarddir.data(system=True) == standarddir.data()
def dictionary_dir(old=False): """Return the path (str) to the QtWebEngine's dictionaries directory.""" if can_use_data_path() and not old: datapath = standarddir.data() else: datapath = QLibraryInfo.location(QLibraryInfo.DataPath) return os.path.join(datapath, 'qtwebengine_dictionaries')
def __init__(self) -> None: super().__init__() self._filename = os.path.join(standarddir.data(), 'state') self.read(self._filename, encoding='utf-8') qt_version = qVersion() # We handle this here, so we can avoid setting qt_version_changed if # the config is brand new, but can still set it when qt_version wasn't # there before... if 'general' in self: old_qt_version = self['general'].get('qt_version', None) self.qt_version_changed = old_qt_version != qt_version else: self.qt_version_changed = False for sect in ['general', 'geometry']: try: self.add_section(sect) except configparser.DuplicateSectionError: pass deleted_keys = ['fooled', 'backend-warning-shown'] for key in deleted_keys: self['general'].pop(key, None) self['general']['qt_version'] = qt_version self['general']['version'] = glimpsebrowser.__version__
def generate_pdfjs_page(filename, url): """Return the html content of a page that displays a file with pdfjs. Returns a string. Args: filename: The filename of the PDF to open. url: The URL being opened. """ if not is_available(): pdfjs_dir = os.path.join(standarddir.data(), 'pdfjs') return jinja.render('no_pdfjs.html', url=url.toDisplayString(), title="PDF.js not found", pdfjs_dir=pdfjs_dir) html = get_pdfjs_res('web/viewer.html').decode('utf-8') script = _generate_pdfjs_script(filename) html = html.replace('</body>', '</body><script>{}</script>'.format(script)) # WORKAROUND for the fact that PDF.js tries to use the Fetch API even with # glimpse:// URLs. pdfjs_script = '<script src="../build/pdf.js"></script>' html = html.replace(pdfjs_script, '<script>window.Response = undefined;</script>\n' + pdfjs_script) return html
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. NotFoundError if the command could not be found. 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 utils.is_posix: runner = _POSIXUserscriptRunner(tabbed_browser) elif utils.is_windows: # 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.val.content.headers.user_agent if user_agent is not None: env['QUTE_USER_AGENT'] = user_agent env['QUTE_CONFIG_DIR'] = standarddir.config() env['QUTE_DATA_DIR'] = standarddir.data() env['QUTE_DOWNLOAD_DIR'] = downloads.download_dir() env['QUTE_COMMANDLINE_TEXT'] = objreg.get('status-command', scope='window', window=win_id).text() cmd_path = os.path.expanduser(cmd) # if cmd is not given as an absolute path, look it up # ~/.local/share/glimpsebrowser/userscripts (or $XDG_DATA_HOME) if not os.path.isabs(cmd_path): log.misc.debug("{} is no absolute path".format(cmd_path)) cmd_path = _lookup_path(cmd) elif not os.path.exists(cmd_path): raise NotFoundError(cmd_path) 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) return runner
def _path_info(): """Get info about important path names. Return: A dictionary of descriptive to actual path names. """ info = { 'config': standarddir.config(), 'data': standarddir.data(), 'cache': standarddir.cache(), 'runtime': standarddir.runtime(), } if standarddir.config() != standarddir.config(auto=True): info['auto config'] = standarddir.config(auto=True) if standarddir.data() != standarddir.data(system=True): info['system data'] = standarddir.data(system=True) return info
def _init_crashlogfile(self): """Start a new logfile and redirect faulthandler to it.""" logname = os.path.join(standarddir.data(), 'crash.log') try: self._crash_log_file = open(logname, 'w', encoding='ascii') except OSError: log.init.exception("Error while opening crash log file!") else: earlyinit.init_faulthandler(self._crash_log_file)
def init(): """Initialize the LimitLineParser storing the history.""" save_manager = objreg.get('save-manager') command_history = lineparser.LimitLineParser( standarddir.data(), 'cmd-history', limit='completion.cmd_history_max_items') objreg.register('command-history', command_history) save_manager.add_saveable('command-history', command_history.save, command_history.changed)
def test_fake_haiku(tmpdir, monkeypatch): """Test getting data dir on HaikuOS.""" locations = { QStandardPaths.DataLocation: '', QStandardPaths.ConfigLocation: str(tmpdir / 'config' / APPNAME), } monkeypatch.setattr(standarddir.QStandardPaths, 'writableLocation', locations.get) monkeypatch.setattr(standarddir.sys, 'platform', 'haiku1') standarddir._init_data(args=None) assert standarddir.data() == str(tmpdir / 'config' / APPNAME / 'data')
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.data(system=True), "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 __init__(self, parent=None, *, line_parser=None): super().__init__(parent) if line_parser: self._lineparser = line_parser else: self._lineparser = lineparser.LineParser( standarddir.data(), 'cookies', binary=True, parent=self) self.parse_cookies() config.instance.changed.connect(self._on_cookies_store_changed) objreg.get('save-manager').add_saveable( 'cookies', self.save, self.changed, config_opt='content.cookies.store')
def get_pdfjs_res_and_path(path): """Get a pdf.js resource in binary format. Returns a (content, path) tuple, where content is the file content and path is the path where the file was found. If path is None, the bundled version was used. Args: path: The path inside the pdfjs directory. """ path = path.lstrip('/') content = None file_path = None system_paths = [ # Debian pdf.js-common # Arch Linux pdfjs (AUR) '/usr/share/pdf.js/', # Flatpak (Flathub) '/app/share/pdf.js/', # Arch Linux pdf.js (AUR) '/usr/share/javascript/pdf.js/', # Debian libjs-pdf '/usr/share/javascript/pdf/', # fallback os.path.join(standarddir.data(), 'pdfjs'), # hardcoded fallback for --temp-basedir os.path.expanduser('~/.local/share/glimpsebrowser/pdfjs/'), ] # First try a system wide installation # System installations might strip off the 'build/' or 'web/' prefixes. # glimpse expects them, so we need to adjust for it. names_to_try = [path, _remove_prefix(path)] for system_path in system_paths: content, file_path = _read_from_system(system_path, names_to_try) if content is not None: break # Fallback to bundled pdf.js if content is None: res_path = '3rdparty/pdfjs/{}'.format(path) try: content = utils.read_file(res_path, binary=True) except FileNotFoundError: raise PDFJSNotFound(path) from None except OSError as e: log.misc.warning("OSError while reading PDF.js file: {}".format(e)) raise PDFJSNotFound(path) from None return content, file_path
def init(parent=None): """Initialize sessions. Args: parent: The parent to use for the SessionManager. """ base_path = os.path.join(standarddir.data(), 'sessions') try: os.mkdir(base_path) except FileExistsError: pass session_manager = SessionManager(base_path, parent) objreg.register('session-manager', session_manager)
def init_faulthandler(self): """Handle a segfault from a previous run and set up faulthandler.""" logname = os.path.join(standarddir.data(), 'crash.log') try: # First check if an old logfile exists. if os.path.exists(logname): with open(logname, 'r', encoding='ascii') as f: self._crash_log_data = f.read() os.remove(logname) self._init_crashlogfile() else: # There's no log file, so we can use this to display crashes to # the user on the next start. self._init_crashlogfile() except OSError: log.init.exception("Error while handling crash log file!") self._init_crashlogfile()
def _init_profiles(): """Init the two used QWebEngineProfiles.""" global default_profile, private_profile, default_user_agent default_profile = QWebEngineProfile.defaultProfile() default_user_agent = default_profile.httpUserAgent() default_profile.setter = ProfileSetter(default_profile) default_profile.setCachePath(os.path.join(standarddir.cache(), 'webengine')) default_profile.setPersistentStoragePath( os.path.join(standarddir.data(), 'webengine')) default_profile.setter.init_profile() default_profile.setter.set_persistent_cookie_policy() if not qtutils.is_single_process(): private_profile = QWebEngineProfile() private_profile.setter = ProfileSetter(private_profile) assert private_profile.isOffTheRecord() private_profile.setter.init_profile()
def init(_args): """Initialize the global QWebSettings.""" cache_path = standarddir.cache() data_path = standarddir.data() QWebSettings.setIconDatabasePath(standarddir.cache()) QWebSettings.setOfflineWebApplicationCachePath( os.path.join(cache_path, 'application-cache')) QWebSettings.globalSettings().setLocalStoragePath( os.path.join(data_path, 'local-storage')) QWebSettings.setOfflineStoragePath( os.path.join(data_path, 'offline-storage')) settings = QWebSettings.globalSettings() _set_user_stylesheet(settings) _set_cookie_accept_policy(settings) _set_cache_maximum_pages(settings) config.instance.changed.connect(_update_settings) global global_settings global_settings = WebKitSettings(QWebSettings.globalSettings()) global_settings.init_settings()
def test_system_datadir_exist_linux(self, monkeypatch): """Test that /usr/share/glimpse_test is used if path exists.""" monkeypatch.setattr(os.path, 'exists', lambda path: True) standarddir._init_dirs() assert standarddir.data(system=True) == "/usr/share/glimpse_test"
def __init__(self, conf: config.Config, keyconfig: config.KeyConfig): self._config = conf self._keyconfig = keyconfig self.errors = [] # type: typing.List[configexc.ConfigErrorDesc] self.configdir = pathlib.Path(standarddir.config()) self.datadir = pathlib.Path(standarddir.data())
def _scripts_dir(): """Get the directory of the scripts.""" return os.path.join(standarddir.data(), 'greasemonkey')
def _init_modules(args, crash_handler): """Initialize all 'modules' which need to be initialized. Args: args: The argparse namespace. crash_handler: The CrashHandler instance. """ log.init.debug("Initializing save manager...") save_manager = savemanager.SaveManager(q_app) objreg.register('save-manager', save_manager) configinit.late_init(save_manager) log.init.debug("Checking backend requirements...") backendproblem.init() log.init.debug("Initializing prompts...") prompt.init() log.init.debug("Initializing network...") networkmanager.init() log.init.debug("Initializing proxy...") proxy.init() log.init.debug("Initializing downloads...") downloads.init() log.init.debug("Initializing readline-bridge...") readline_bridge = readline.ReadlineBridge() objreg.register('readline-bridge', readline_bridge) try: log.init.debug("Initializing SQL...") sql.init(os.path.join(standarddir.data(), 'history.sqlite')) log.init.debug("Initializing web history...") history.init(q_app) except sql.KnownError as e: error.handle_fatal_exc(e, args, 'Error initializing SQL', pre_text='Error initializing SQL') sys.exit(usertypes.Exit.err_init) log.init.debug("Initializing command history...") cmdhistory.init() log.init.debug("Initializing sessions...") sessions.init(q_app) log.init.debug("Initializing websettings...") websettings.init(args) if not args.no_err_windows: crash_handler.display_faulthandler() log.init.debug("Initializing quickmarks...") quickmark_manager = urlmarks.QuickmarkManager(q_app) objreg.register('quickmark-manager', quickmark_manager) log.init.debug("Initializing bookmarks...") bookmark_manager = urlmarks.BookmarkManager(q_app) objreg.register('bookmark-manager', bookmark_manager) log.init.debug("Initializing cookies...") cookie_jar = cookies.CookieJar(q_app) ram_cookie_jar = cookies.RAMCookieJar(q_app) objreg.register('cookie-jar', cookie_jar) objreg.register('ram-cookie-jar', ram_cookie_jar) log.init.debug("Initializing cache...") diskcache = cache.DiskCache(standarddir.cache(), parent=q_app) objreg.register('cache', diskcache) log.init.debug("Initializing downloads...") download_manager = qtnetworkdownloads.DownloadManager(parent=q_app) objreg.register('qtnetwork-download-manager', download_manager) log.init.debug("Initializing Greasemonkey...") greasemonkey.init() log.init.debug("Misc initialization...") macros.init() # Init backend-specific stuff browsertab.init()
def _get_init_context() -> InitContext: """Get an InitContext object.""" return InitContext(data_dir=pathlib.Path(standarddir.data()), config_dir=pathlib.Path(standarddir.config()), args=objreg.get('args'))