Implement config cache system

This commit is contained in:
Jay Kamat 2018-09-02 13:02:30 -07:00
parent f028759125
commit 067d76616b
No known key found for this signature in database
GPG Key ID: 5D2E399600F4F7B5
2 changed files with 80 additions and 0 deletions

View 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]

View 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']