~homeworkprod/byceps

ref: 4144a29fdeb34e20d44e5cd78a324d9f9f6a2b0d byceps/byceps/config.py -rw-r--r-- 2.4 KiB
4144a29f — Jochen Kupperschmidt Dim archived brands in admin brand navigation dropdown 1 year, 6 days ago
                                                                                
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
byceps.config
~~~~~~~~~~~~~

:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

from enum import Enum
from typing import Any, Optional

from flask import current_app, Flask

from .services.site.transfer.models import SiteID


EXTENSION_KEY = 'byceps_config'
KEY_APP_MODE = 'app_mode'
KEY_SITE_ID = 'site_id'


AppMode = Enum('AppMode', ['admin', 'site'])
AppMode.is_admin = lambda self: self == AppMode.admin
AppMode.is_site = lambda self: self == AppMode.site


class ConfigurationError(Exception):
    pass


def init_app(app: Flask) -> None:
    app.extensions[EXTENSION_KEY] = {}

    app_mode = _determine_app_mode(app)
    set_extension_value(KEY_APP_MODE, app_mode, app)

    if app_mode.is_site():
        site_id = _determine_site_id(app)
        set_extension_value(KEY_SITE_ID, site_id, app)


def get_extension_value(key: str, app: Optional[Flask] = None) -> Any:
    """Return the value for the key in this application's own extension
    namespace.

    It is expected that the value has already been set. An exception is
    raised if that is not the case.
    """
    if app is None:
        app = current_app

    extension = app.extensions[EXTENSION_KEY]
    return extension[key]


def set_extension_value(key: str, value: Any, app: Flask) -> None:
    """Set/replace the value for the key in this application's own
    extension namespace.
    """
    extension = app.extensions[EXTENSION_KEY]
    extension[key] = value


# -------------------------------------------------------------------- #
# app mode


def _determine_app_mode(app: Flask) -> AppMode:
    value = app.config.get('APP_MODE')
    if value is None:
        raise ConfigurationError('No app mode configured.')

    try:
        return AppMode[value]
    except KeyError:
        raise ConfigurationError(f'Invalid app mode "{value}" configured.')


def get_app_mode(app: Optional[Flask] = None) -> AppMode:
    """Return the mode the site should run in."""
    return get_extension_value(KEY_APP_MODE, app)


# -------------------------------------------------------------------- #
# site ID


def _determine_site_id(app: Flask) -> SiteID:
    site_id = app.config.get('SITE_ID')
    if site_id is None:
        raise ConfigurationError('No site ID configured.')

    return site_id


def get_current_site_id(app: Optional[Flask] = None) -> SiteID:
    """Return the id of the current site."""
    return get_extension_value(KEY_SITE_ID, app)