Implement config cache system
This commit is contained in:
parent
f028759125
commit
067d76616b
45
qutebrowser/config/configcache.py
Normal file
45
qutebrowser/config/configcache.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||||
|
|
||||||
|
# Copyright 2018 Jay Kamat <jaygkamat@gmail.com>
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
"""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]
|
35
tests/unit/config/test_configcache.py
Normal file
35
tests/unit/config/test_configcache.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
||||||
|
|
||||||
|
# Copyright 2018 Jay Kamat <jaygkamat@gmail.com>:
|
||||||
|
#
|
||||||
|
# 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 <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
"""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']
|
Loading…
Reference in New Issue
Block a user