From 067d76616b09c6f84583096026455e867ab1b86f Mon Sep 17 00:00:00 2001 From: Jay Kamat Date: Sun, 2 Sep 2018 13:02:30 -0700 Subject: [PATCH] Implement config cache system --- qutebrowser/config/configcache.py | 45 +++++++++++++++++++++++++++ tests/unit/config/test_configcache.py | 35 +++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 qutebrowser/config/configcache.py create mode 100644 tests/unit/config/test_configcache.py diff --git a/qutebrowser/config/configcache.py b/qutebrowser/config/configcache.py new file mode 100644 index 000000000..ebb112503 --- /dev/null +++ b/qutebrowser/config/configcache.py @@ -0,0 +1,45 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Jay Kamat +# +# 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 . + +"""A 'high-performance' cache for the config system. + +Useful for areas which call out to the config system very frequently, DO NOT +modify the value returned, and DO NOT require per-url settings. + +If any of these requirements are broken, you will get incorrect behavior. +""" + +from qutebrowser.config import config + + +class ConfigCache(): + + def __init__(self) -> None: + self.cache = {} + config.instance.changed.connect(self.config_changed) + + def config_changed(self, attr: str) -> None: + if attr in self.cache: + self.cache[attr] = config.instance.get(attr) + + def __getattr__(self, attr: str): + if attr not in self.cache: + assert not config.instance.get_opt(attr).supports_pattern + self.cache[attr] = config.instance.get(attr) + return self.cache[attr] diff --git a/tests/unit/config/test_configcache.py b/tests/unit/config/test_configcache.py new file mode 100644 index 000000000..1236cf8a7 --- /dev/null +++ b/tests/unit/config/test_configcache.py @@ -0,0 +1,35 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2018 Jay Kamat : +# +# 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 . + +"""Tests for qutebrowser.misc.autoupdate.""" + +import pytest + +from qutebrowser.config import configcache, config + + +class TestConfigCache: + + @pytest.fixture + def ccache(self, config_stub): + return configcache.ConfigCache() + + def test_configcache_except_pattern(self, ccache): + with pytest.raises(AssertionError): + ccache['content.javascript.enabled']