File: config.py

package info (click to toggle)
python-django-otp 1.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 756 kB
  • sloc: python: 3,221; makefile: 145; sh: 6
file content (34 lines) | stat: -rw-r--r-- 764 bytes parent folder | download | duplicates (2)
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
import os.path

from django.core.exceptions import ImproperlyConfigured

try:
    import tomllib
except ModuleNotFoundError:
    import tomli as tomllib


def _load_config(path: str, required) -> dict:
    if os.path.exists(path):
        with open(path, 'rb') as f:
            config = tomllib.load(f)
    elif required:
        raise ImproperlyConfigured(f"{path} does not exist.")
    else:
        config = {}

    return config


def load() -> dict:
    path = os.getenv('DJANGO_OTP_CONFIG')
    env = os.getenv('HATCH_ENV_ACTIVE', '').split('.', 1)[0]

    if path:
        config = _load_config(path, required=True)
    elif env:
        config = _load_config(f'test/config/env-{env}.toml', required=False)
    else:
        config = {}

    return config