def test_config_py_no_arg(self, tmpdir): basedir = tmpdir / 'basedir' basedir.ensure(dir=True) with tmpdir.as_cwd(): args = types.SimpleNamespace(basedir='basedir') standarddir._init_dirs(args) assert standarddir.config_py() == str(basedir / 'config' / 'config.py')
def early_init(args: argparse.Namespace) -> None: """Initialize the part of the config which works without a QApplication.""" configdata.init() yaml_config = configfiles.YamlConfig() config.instance = config.Config(yaml_config=yaml_config) config.val = config.ConfigContainer(config.instance) configapi.val = config.ConfigContainer(config.instance) config.key_instance = config.KeyConfig(config.instance) config.cache = configcache.ConfigCache() yaml_config.setParent(config.instance) for cf in config.change_filters: cf.validate() config_commands = configcommands.ConfigCommands( config.instance, config.key_instance) objreg.register('config-commands', config_commands) config_file = standarddir.config_py() try: if os.path.exists(config_file): configfiles.read_config_py(config_file) else: configfiles.read_autoconfig() except configexc.ConfigFileErrors as e: log.config.exception("Error while loading {}".format(e.basename)) global _init_errors _init_errors = e configfiles.init() for opt, val in args.temp_settings: try: config.instance.set_str(opt, val) except configexc.Error as e: message.error("set: {} - {}".format(e.__class__.__name__, e)) objects.backend = get_backend(args) configtypes.Font.monospace_fonts = config.val.fonts.monospace config.instance.changed.connect(_update_monospace_fonts) _init_envvars()
def config_write_py(self, filename: str = None, force: bool = False, defaults: bool = False) -> None: """Write the current configuration to a config.py file. Args: filename: The file to write to, or None for the default config.py. force: Force overwriting existing files. defaults: Write the defaults instead of values configured via :set. """ if filename is None: filename = standarddir.config_py() else: if not os.path.isabs(filename): filename = os.path.join(standarddir.config(), filename) filename = os.path.expanduser(filename) if os.path.exists(filename) and not force: raise cmdutils.CommandError("{} already exists - use --force to " "overwrite!".format(filename)) options = [] # type: typing.List if defaults: options = [(None, opt, opt.default) for _name, opt in sorted(configdata.DATA.items())] bindings = dict(configdata.DATA['bindings.default'].default) commented = True else: for values in self._config: for scoped in values: options.append((scoped.pattern, values.opt, scoped.value)) bindings = dict(self._config.get_mutable_obj('bindings.commands')) commented = False writer = configfiles.ConfigPyWriter(options, bindings, commented=commented) try: writer.write(filename) except OSError as e: raise cmdutils.CommandError(str(e))
def config_edit(self, no_source: bool = False) -> None: """Open the config.py file in the editor. Args: no_source: Don't re-source the config file after editing. """ def on_file_updated() -> None: """Source the new config when editing finished. This can't use cmdutils.CommandError as it's run async. """ try: configfiles.read_config_py(filename) except configexc.ConfigFileErrors as e: message.error(str(e)) ed = editor.ExternalEditor(watch=True, parent=self._config) if not no_source: ed.file_updated.connect(on_file_updated) filename = standarddir.config_py() ed.edit_file(filename)
def config_source(self, filename: str = None, clear: bool = False) -> None: """Read a config.py file. Args: filename: The file to load. If not given, loads the default config.py. clear: Clear current settings first. """ if filename is None: filename = standarddir.config_py() else: filename = os.path.expanduser(filename) if not os.path.isabs(filename): filename = os.path.join(standarddir.config(), filename) if clear: self.config_clear() try: configfiles.read_config_py(filename) except configexc.ConfigFileErrors as e: raise cmdutils.CommandError(e)
def _config_py_loaded() -> str: if config.instance.config_py_loaded: return "{} has been loaded".format(standarddir.config_py()) else: return "no config.py was loaded"
def test_version_output(params, stubs, monkeypatch, config_stub): """Test version.version().""" class FakeWebEngineSettings: default_user_agent = ('Toaster/4.0.4 Chrome/CHROMIUMVERSION ' 'Teapot/4.1.8') config.instance.config_py_loaded = params.config_py_loaded import_path = os.path.abspath('/IMPORTPATH') patches = { 'glimpsebrowser.__file__': os.path.join(import_path, '__init__.py'), 'glimpsebrowser.__version__': 'VERSION', '_git_str': lambda: ('GIT COMMIT' if params.git_commit else None), 'platform.python_implementation': lambda: 'PYTHON IMPLEMENTATION', 'platform.python_version': lambda: 'PYTHON VERSION', 'sys.executable': 'EXECUTABLE PATH', 'PYQT_VERSION_STR': 'PYQT VERSION', 'earlyinit.qt_version': lambda: 'QT VERSION', '_module_versions': lambda: ['MODULE VERSION 1', 'MODULE VERSION 2'], '_pdfjs_version': lambda: 'PDFJS VERSION', 'QSslSocket': FakeQSslSocket('SSL VERSION', params.ssl_support), 'platform.platform': lambda: 'PLATFORM', 'platform.architecture': lambda: ('ARCHITECTURE', ''), '_os_info': lambda: ['OS INFO 1', 'OS INFO 2'], '_path_info': lambda: { 'PATH DESC': 'PATH NAME' }, 'QApplication': (stubs.FakeQApplication(style='STYLE') if params.style else stubs.FakeQApplication(instance=None)), 'QLibraryInfo.location': (lambda _loc: 'QT PATH'), 'sql.version': lambda: 'SQLITE VERSION', '_uptime': lambda: datetime.timedelta(hours=1, minutes=23, seconds=45), '_autoconfig_loaded': lambda: ("yes" if params.autoconfig_loaded else "no"), } substitutions = { 'git_commit': '\nGit commit: GIT COMMIT' if params.git_commit else '', 'style': '\nStyle: STYLE' if params.style else '', 'qt': 'QT VERSION', 'frozen': str(params.frozen), 'import_path': import_path, 'python_path': 'EXECUTABLE PATH', 'uptime': "1:23:45", 'autoconfig_loaded': "yes" if params.autoconfig_loaded else "no", } if params.config_py_loaded: substitutions["config_py_loaded"] = "{} has been loaded".format( standarddir.config_py()) else: substitutions["config_py_loaded"] = "no config.py was loaded" if params.with_webkit: patches['qWebKitVersion'] = lambda: 'WEBKIT VERSION' patches['objects.backend'] = usertypes.Backend.QtWebKit patches['webenginesettings'] = None substitutions['backend'] = 'new QtWebKit (WebKit WEBKIT VERSION)' else: monkeypatch.delattr(version, 'qtutils.qWebKitVersion', raising=False) patches['objects.backend'] = usertypes.Backend.QtWebEngine substitutions['backend'] = 'QtWebEngine (Chromium CHROMIUMVERSION)' patches['webenginesettings'] = FakeWebEngineSettings patches['QWebEngineProfile'] = True if params.known_distribution: patches['distribution'] = lambda: version.DistributionInfo( parsed=version.Distribution.arch, version=None, pretty='LINUX DISTRIBUTION', id='arch') substitutions['linuxdist'] = ('\nLinux distribution: ' 'LINUX DISTRIBUTION (arch)') substitutions['osinfo'] = '' else: patches['distribution'] = lambda: None substitutions['linuxdist'] = '' substitutions['osinfo'] = 'OS INFO 1\nOS INFO 2\n' substitutions['ssl'] = 'SSL VERSION' if params.ssl_support else 'no' for name, val in patches.items(): monkeypatch.setattr('glimpsebrowser.utils.version.' + name, val) if params.frozen: monkeypatch.setattr(sys, 'frozen', True, raising=False) else: monkeypatch.delattr(sys, 'frozen', raising=False) template = textwrap.dedent(""" glimpsebrowser vVERSION{git_commit} Backend: {backend} PYTHON IMPLEMENTATION: PYTHON VERSION Qt: {qt} PyQt: PYQT VERSION MODULE VERSION 1 MODULE VERSION 2 pdf.js: PDFJS VERSION sqlite: SQLITE VERSION QtNetwork SSL: {ssl} {style} Platform: PLATFORM, ARCHITECTURE{linuxdist} Frozen: {frozen} Imported from {import_path} Using Python from {python_path} Qt library executable path: QT PATH, data path: QT PATH {osinfo} Paths: PATH DESC: PATH NAME Autoconfig loaded: {autoconfig_loaded} Config.py: {config_py_loaded} Uptime: {uptime} """.lstrip('\n')) expected = template.rstrip('\n').format(**substitutions) assert version.version() == expected