From 9b1729c77e735487125b0588cd3e920cba515544 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 1 Feb 2015 23:47:18 +0100 Subject: [PATCH] Always write config files to disk on first start. --- qutebrowser/browser/quickmarks.py | 5 ++++- qutebrowser/config/config.py | 8 ++++++-- qutebrowser/misc/savemanager.py | 22 +++++++++++++++++----- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/quickmarks.py b/qutebrowser/browser/quickmarks.py index c08e4ca6a..52387456d 100644 --- a/qutebrowser/browser/quickmarks.py +++ b/qutebrowser/browser/quickmarks.py @@ -24,6 +24,7 @@ OrderedDict. This is because we read them from a file at start and write them to a file on shutdown, so it makes sense to keep them as strings here. """ +import os.path import functools import collections @@ -61,8 +62,10 @@ class QuickmarkManager(QObject): message.error(0, "Invalid quickmark '{}'".format(line)) else: self.marks[key] = url + filename = os.path.join(confdir, 'quickmarks') objreg.get('save-manager').add_saveable('quickmark-manager', self.save, - self.changed) + self.changed, + filename=filename) def save(self): """Save the quickmarks to disk.""" diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index e55ca950f..2aa7734f2 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -140,9 +140,11 @@ def init(args): sys.exit(1) else: objreg.register('config', config_obj) + filename = os.path.join(confdir, 'qutebrowser.conf') save_manager.add_saveable('config', config_obj.save, config_obj.changed, - config_opt=('general', 'auto-save-config')) + config_opt=('general', 'auto-save-config'), + filename=filename) try: key_config = keyconf.KeyConfigParser(confdir, 'keys.conf') except (keyconf.KeyConfigError, UnicodeDecodeError) as e: @@ -158,9 +160,11 @@ def init(args): sys.exit(1) else: objreg.register('key-config', key_config) + filename = os.path.join(confdir, 'keys.conf') save_manager.add_saveable('key-config', key_config.save, key_config.changed, - config_opt=('general', 'auto-save-config')) + config_opt=('general', 'auto-save-config'), + filename=filename) datadir = standarddir.get(QStandardPaths.DataLocation, args) state_config = ini.ReadWriteConfigParser(datadir, 'state') diff --git a/qutebrowser/misc/savemanager.py b/qutebrowser/misc/savemanager.py index 5a1852b66..baf6aa37e 100644 --- a/qutebrowser/misc/savemanager.py +++ b/qutebrowser/misc/savemanager.py @@ -19,6 +19,7 @@ """Saving things to disk periodically.""" +import os.path import collections from PyQt5.QtCore import pyqtSlot, QObject, QTimer @@ -39,9 +40,11 @@ class Saveable: _save_on_exit: Whether to always save this saveable on exit. _config_opt: A (section, option) tuple of a config option which decides whether to autosave or not. None if no such option exists. + _filename: The filename of the underlying file. """ - def __init__(self, name, save_handler, changed=None, config_opt=None): + def __init__(self, name, save_handler, changed=None, config_opt=None, + filename=None): self._name = name self._dirty = False self._save_handler = save_handler @@ -51,12 +54,17 @@ class Saveable: self._save_on_exit = False else: self._save_on_exit = True + self._filename = filename + if filename is not None and not os.path.exists(filename): + self._dirty = True + self.save() def __repr__(self): return utils.get_repr(self, name=self._name, dirty=self._dirty, save_handler=self._save_handler, config_opt=self._config_opt, - save_on_exit=self._save_on_exit) + save_on_exit=self._save_on_exit, + filename=self._filename) @pyqtSlot() def mark_dirty(self): @@ -128,7 +136,8 @@ class SaveManager(QObject): self._save_timer.setInterval(interval) self._save_timer.start() - def add_saveable(self, name, save, changed=None, config_opt=None): + def add_saveable(self, name, save, changed=None, config_opt=None, + filename=None): """Add a new saveable. Args: @@ -136,11 +145,14 @@ class SaveManager(QObject): save: The function to call to save this saveable. changed: The signal emitted when this saveable changed. config_opt: A (section, option) tuple deciding whether to autosave - or not. + or not. + filename: The filename of the underlying file, so we can force + saving if it doesn't exist. """ if name in self.saveables: raise ValueError("Saveable {} already registered!".format(name)) - self.saveables[name] = Saveable(name, save, changed, config_opt) + self.saveables[name] = Saveable(name, save, changed, config_opt, + filename) def save(self, name, is_exit=False, explicit=False, silent=False): """Save a saveable by name.