summaryrefslogtreecommitdiffhomepage
path: root/libs/tld/conf.py
blob: 684c2d58498a671bfc2770c7d7ae875e10c5d232 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from typing import Any
from . import defaults

__author__ = 'Artur Barseghyan'
__copyright__ = '2013-2020 Artur Barseghyan'
__license__ = 'MPL-1.1 OR GPL-2.0-only OR LGPL-2.1-or-later'
__all__ = (
    'get_setting',
    'reset_settings',
    'set_setting',
    'settings',
)


class Settings(object):
    """Settings registry."""

    def __init__(self):
        self._settings = {}
        self._settings_get = self._settings.get

    def set(self, name: str, value: Any) -> None:
        """
        Override default settings.

        :param str name:
        :param mixed value:
        """
        self._settings[name] = value

    def get(self, name: str, default: Any = None) -> Any:
        """
        Gets a variable from local settings.

        :param str name:
        :param mixed default: Default value.
        :return mixed:
        """
        if name in self._settings:
            return self._settings_get(name, default)
        elif hasattr(defaults, name):
            return getattr(defaults, name, default)

        return default

    def reset(self) -> None:
        """Reset settings."""
        for name in defaults.__all__:
            self.set(name, getattr(defaults, name))


settings = Settings()

get_setting = settings.get

set_setting = settings.set

reset_settings = settings.reset