config.py 1.7 KB
Newer Older
1
2
3
import configparser
import os

4
5
from typing import Any

6
7
8
# Publicly visible version of aurweb. This is used to display
# aurweb versioning in the footer and must be maintained.
# Todo: Make this dynamic/automated.
Kevin Morris's avatar
Kevin Morris committed
9
AURWEB_VERSION = "v6.0.24"
10

11
12
13
14
15
16
17
_parser = None


def _get_parser():
    global _parser

    if not _parser:
18
19
20
        path = os.environ.get('AUR_CONFIG', '/etc/aurweb/config')
        defaults = os.environ.get('AUR_CONFIG_DEFAULTS', path + '.defaults')

21
        _parser = configparser.RawConfigParser()
22
        _parser.optionxform = lambda option: option
23
24
25
        if os.path.isfile(defaults):
            with open(defaults) as f:
                _parser.read_file(f)
26
27
28
29
30
        _parser.read(path)

    return _parser


Kevin Morris's avatar
Kevin Morris committed
31
32
33
34
35
36
37
def rehash():
    """ Globally rehash the configuration parser. """
    global _parser
    _parser = None
    _get_parser()


38
39
40
41
def get_with_fallback(section, option, fallback):
    return _get_parser().get(section, option, fallback=fallback)


42
43
44
45
46
47
48
49
def get(section, option):
    return _get_parser().get(section, option)


def getboolean(section, option):
    return _get_parser().getboolean(section, option)


50
51
def getint(section, option, fallback=None):
    return _get_parser().getint(section, option, fallback=fallback)
52
53


54
55
56
def get_section(section):
    if section in _get_parser().sections():
        return _get_parser()[section]
57
58


59
60
61
62
63
def unset_option(section: str, option: str) -> None:
    _get_parser().remove_option(section, option)


def set_option(section: str, option: str, value: Any) -> None:
64
    _get_parser().set(section, option, value)
65
    return value
66
67
68
69
70
71


def save() -> None:
    aur_config = os.environ.get("AUR_CONFIG", "/etc/aurweb/config")
    with open(aur_config, "w") as fp:
        _get_parser().write(fp)