From 70f6d0e3059d46a57635788750431d81c6bdf5fa Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 20 Jun 2017 16:27:54 +0200 Subject: [PATCH] Add qutebrowser.config.configfiles --- qutebrowser/config/config.py | 55 ++------------------- qutebrowser/config/configfiles.py | 80 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 52 deletions(-) create mode 100644 qutebrowser/config/configfiles.py diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 001c2ab86..eb461f31a 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -23,11 +23,10 @@ import copy import os.path import contextlib import functools -import configparser -from PyQt5.QtCore import pyqtSignal, QObject, QUrl, QSettings +from PyQt5.QtCore import pyqtSignal, QObject, QUrl -from qutebrowser.config import configdata, configexc, configtypes +from qutebrowser.config import configdata, configexc, configtypes, configfiles from qutebrowser.utils import (utils, objreg, message, standarddir, log, usertypes) from qutebrowser.commands import cmdexc, cmdutils, runners @@ -458,30 +457,6 @@ class ConfigContainer: return attr -class StateConfig(configparser.ConfigParser): - - """The "state" file saving various application state.""" - - def __init__(self): - super().__init__() - save_manager = objreg.get('save-manager') - self._filename = os.path.join(standarddir.data(), 'state') - self.read(self._filename, encoding='utf-8') - for sect in ['general', 'geometry']: - try: - self.add_section(sect) - except configparser.DuplicateSectionError: - pass - # See commit a98060e020a4ba83b663813a4b9404edb47f28ad. - self['general'].pop('fooled', None) - save_manager.add_saveable('state-config', self._save) - - def _save(self): - """Save the state file to the configured location.""" - with open(self._filename, 'w', encoding='utf-8') as f: - self.write(f) - - def init(parent=None): """Initialize the config. @@ -505,28 +480,4 @@ def init(parent=None): for cf in _change_filters: cf.validate() - state = StateConfig() - objreg.register('state-config', state) - - # We need to import this here because lineparser needs config. - # FIXME:conf add this to the Command widget or something? - from qutebrowser.misc import lineparser - save_manager = objreg.get('save-manager') - command_history = lineparser.LimitLineParser( - standarddir.data(), 'cmd-history', - limit='completion.cmd_history_max_items', - parent=instance) - objreg.register('command-history', command_history) - save_manager.add_saveable('command-history', command_history.save, - command_history.changed) - - # Set the QSettings path to something like - # ~/.config/qutebrowser/qsettings/qutebrowser/qutebrowser.conf so it - # doesn't overwrite our config. - # - # This fixes one of the corruption issues here: - # https://github.com/qutebrowser/qutebrowser/issues/515 - - path = os.path.join(standarddir.config(), 'qsettings') - for fmt in [QSettings.NativeFormat, QSettings.IniFormat]: - QSettings.setPath(fmt, QSettings.UserScope, path) + configfiles.init(instance) diff --git a/qutebrowser/config/configfiles.py b/qutebrowser/config/configfiles.py new file mode 100644 index 000000000..ee6c1f4c1 --- /dev/null +++ b/qutebrowser/config/configfiles.py @@ -0,0 +1,80 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2014-2017 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# qutebrowser is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with qutebrowser. If not, see . + +"""Configuration files residing on disk.""" + +import os.path +import configparser + +from PyQt5.QtCore import QSettings + +from qutebrowser.utils import objreg, standarddir + + +class StateConfig(configparser.ConfigParser): + + """The "state" file saving various application state.""" + + def __init__(self): + super().__init__() + save_manager = objreg.get('save-manager') + self._filename = os.path.join(standarddir.data(), 'state') + self.read(self._filename, encoding='utf-8') + for sect in ['general', 'geometry']: + try: + self.add_section(sect) + except configparser.DuplicateSectionError: + pass + # See commit a98060e020a4ba83b663813a4b9404edb47f28ad. + self['general'].pop('fooled', None) + save_manager.add_saveable('state-config', self._save) + + def _save(self): + """Save the state file to the configured location.""" + with open(self._filename, 'w', encoding='utf-8') as f: + self.write(f) + + +def init(config): + """Initialize config storage not related to the main config.""" + state = StateConfig() + objreg.register('state-config', state) + + # We need to import this here because lineparser needs config. + # FIXME:conf add this to the Command widget or something? + from qutebrowser.misc import lineparser + save_manager = objreg.get('save-manager') + command_history = lineparser.LimitLineParser( + standarddir.data(), 'cmd-history', + limit='completion.cmd_history_max_items', + parent=config) + objreg.register('command-history', command_history) + save_manager.add_saveable('command-history', command_history.save, + command_history.changed) + + # Set the QSettings path to something like + # ~/.config/qutebrowser/qsettings/qutebrowser/qutebrowser.conf so it + # doesn't overwrite our config. + # + # This fixes one of the corruption issues here: + # https://github.com/qutebrowser/qutebrowser/issues/515 + + path = os.path.join(standarddir.config(), 'qsettings') + for fmt in [QSettings.NativeFormat, QSettings.IniFormat]: + QSettings.setPath(fmt, QSettings.UserScope, path)