Completed
Push — main ( 4c245a...a71144 )
by Jochen
05:11
created

byceps.config.get_app_mode()   A

Complexity

Conditions 1

Size

Total Lines 3
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 3
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-2020 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE 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
AppMode = Enum('AppMode', ['admin', 'site'])
23 1
AppMode.is_admin = lambda self: self == AppMode.admin
24 1
AppMode.is_site = lambda self: self == AppMode.site
25
26
27 1
class ConfigurationError(Exception):
28 1
    pass
29
30
31 1
def init_app(app: Flask) -> None:
32 1
    app.extensions[EXTENSION_KEY] = {}
33
34 1
    app_mode = _determine_app_mode(app)
35 1
    set_extension_value(KEY_APP_MODE, app_mode, app)
36
37 1
    if app_mode.is_site():
38 1
        site_id = _determine_site_id(app)
39 1
        set_extension_value(KEY_SITE_ID, site_id, app)
40
41
42 1
def get_extension_value(key: str, app: Optional[Flask]=None) -> Any:
43
    """Return the value for the key in this application's own extension
44
    namespace.
45
46
    It is expected that the value has already been set. An exception is
47
    raised if that is not the case.
48
    """
49 1
    if app is None:
50 1
        app = current_app
51
52 1
    extension = app.extensions[EXTENSION_KEY]
53 1
    return extension[key]
54
55
56 1
def set_extension_value(key: str, value: Any, app: Flask) -> None:
57
    """Set/replace the value for the key in this application's own
58
    extension namespace.
59
    """
60 1
    extension = app.extensions[EXTENSION_KEY]
61 1
    extension[key] = value
62
63
64
# -------------------------------------------------------------------- #
65
# app mode
66
67
68 1
def _determine_app_mode(app: Flask) -> AppMode:
69 1
    value = app.config.get('APP_MODE')
70 1
    if value is None:
71
        raise ConfigurationError('No app mode configured.')
72
73 1
    try:
74 1
        return AppMode[value]
75
    except KeyError:
76
        raise ConfigurationError(f'Invalid app mode "{value}" configured.')
77
78
79 1
def get_app_mode(app: Optional[Flask]=None) -> AppMode:
80
    """Return the mode the site should run in."""
81 1
    return get_extension_value(KEY_APP_MODE, app)
82
83
84
# -------------------------------------------------------------------- #
85
# site ID
86
87
88 1
def _determine_site_id(app: Flask) -> SiteID:
89 1
    site_id = app.config.get('SITE_ID')
90 1
    if site_id is None:
91
        raise ConfigurationError('No site ID configured.')
92
93 1
    return site_id
94
95
96 1
def get_current_site_id(app: Optional[Flask]=None) -> SiteID:
97
    """Return the id of the current site."""
98
    return get_extension_value(KEY_SITE_ID, app)
99