File: base.py

package info (click to toggle)
python-django-constance 4.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 796 kB
  • sloc: python: 2,080; makefile: 25; javascript: 23; sh: 6
file content (31 lines) | stat: -rw-r--r-- 898 bytes parent folder | download
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
from . import settings
from . import utils


class Config:
    """The global config wrapper that handles the backend."""

    def __init__(self):
        super().__setattr__('_backend', utils.import_module_attr(settings.BACKEND)())

    def __getattr__(self, key):
        try:
            if len(settings.CONFIG[key]) not in (2, 3):
                raise AttributeError(key)
            default = settings.CONFIG[key][0]
        except KeyError as e:
            raise AttributeError(key) from e
        result = self._backend.get(key)
        if result is None:
            result = default
            setattr(self, key, default)
            return result
        return result

    def __setattr__(self, key, value):
        if key not in settings.CONFIG:
            raise AttributeError(key)
        self._backend.set(key, value)

    def __dir__(self):
        return settings.CONFIG.keys()