Passed
Push — main ( 5593c7...37a38c )
by Jochen
04:43 queued 18s
created

byceps.config.AppMode.is_admin()   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
"""
2
byceps.config
3
~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from enum import Enum
10 1
from typing import Any, Optional
11
12 1
from flask import current_app, Flask
13
14 1
from .services.site.transfer.models import SiteID
15
16
17 1
EXTENSION_KEY = 'byceps_config'
18 1
KEY_APP_MODE = 'app_mode'
19 1
KEY_SITE_ID = 'site_id'
20
21
22 1
class AppMode(Enum):
23 1
    admin = object()
24 1
    site = object()
25
26 1
    def is_admin(self) -> bool:
27 1
        return self == AppMode.admin
28
29 1
    def is_site(self) -> bool:
30 1
        return self == AppMode.site
31
32
33 1
class ConfigurationError(Exception):
34 1
    pass
35
36
37 1
def init_app(app: Flask) -> None:
38 1
    app.extensions[EXTENSION_KEY] = {}
39
40 1
    app_mode = _determine_app_mode(app)
41 1
    set_extension_value(KEY_APP_MODE, app_mode, app)
42
43 1
    if app_mode.is_site():
44 1
        site_id = _determine_site_id(app)
45 1
        set_extension_value(KEY_SITE_ID, site_id, app)
46
47
48 1
def get_extension_value(key: str, app: Optional[Flask] = None) -> Any:
49
    """Return the value for the key in this application's own extension
50
    namespace.
51
52
    It is expected that the value has already been set. An exception is
53
    raised if that is not the case.
54
    """
55 1
    if app is None:
56 1
        app = current_app
57
58 1
    extension = app.extensions[EXTENSION_KEY]
59 1
    return extension[key]
60
61
62 1
def set_extension_value(key: str, value: Any, app: Flask) -> None:
63
    """Set/replace the value for the key in this application's own
64
    extension namespace.
65
    """
66 1
    extension = app.extensions[EXTENSION_KEY]
67 1
    extension[key] = value
68
69
70
# -------------------------------------------------------------------- #
71
# app mode
72
73
74 1
def _determine_app_mode(app: Flask) -> AppMode:
75 1
    value = app.config.get('APP_MODE')
76 1
    if value is None:
77
        raise ConfigurationError('No app mode configured.')
78
79 1
    try:
80 1
        return AppMode[value]
81
    except KeyError:
82
        raise ConfigurationError(f'Invalid app mode "{value}" configured.')
83
84
85 1
def get_app_mode(app: Optional[Flask] = None) -> AppMode:
86
    """Return the mode the site should run in."""
87 1
    return get_extension_value(KEY_APP_MODE, app)
88
89
90
# -------------------------------------------------------------------- #
91
# site ID
92
93
94 1
def _determine_site_id(app: Flask) -> SiteID:
95 1
    site_id = app.config.get('SITE_ID')
96 1
    if site_id is None:
97
        raise ConfigurationError('No site ID configured.')
98
99 1
    return site_id
100
101
102 1
def get_current_site_id(app: Optional[Flask] = None) -> SiteID:
103
    """Return the id of the current site."""
104
    return get_extension_value(KEY_SITE_ID, app)
105