Completed
Push — master ( cd0c61...ee130d )
by Jochen
05:47
created

byceps.config   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 12

7 Functions

Rating   Name   Duplication   Size   Complexity  
A _determine_site_mode() 0 9 3
A _determine_site_id() 0 6 2
A set_extension_value() 0 6 1
A init_app() 0 9 2
A get_extension_value() 0 12 2
A get_site_mode() 0 3 1
A get_current_site_id() 0 3 1
1
"""
2
byceps.config
3
~~~~~~~~~~~~~
4
5
:Copyright: 2006-2019 Jochen Kupperschmidt
6
:License: Modified BSD, see LICENSE for details.
7
"""
8
9
from enum import Enum
10
from typing import Any, Optional
11
12
from flask import current_app, Flask
13
14
from .services.site.transfer.models import SiteID
15
16
17
EXTENSION_KEY = 'byceps_config'
18
KEY_SITE_MODE = 'site_mode'
19
KEY_SITE_ID = 'site_id'
20
21
22
SiteMode = Enum('SiteMode', ['public', 'admin'])
23
SiteMode.is_admin = lambda self: self == SiteMode.admin
24
SiteMode.is_public = lambda self: self == SiteMode.public
25
26
27
class ConfigurationError(Exception):
28
    pass
29
30
31
def init_app(app: Flask) -> None:
32
    app.extensions[EXTENSION_KEY] = {}
33
34
    site_mode = _determine_site_mode(app)
35
    set_extension_value(KEY_SITE_MODE, site_mode, app)
36
37
    if site_mode.is_public():
38
        site_id = _determine_site_id(app)
39
        set_extension_value(KEY_SITE_ID, site_id, app)
40
41
42
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
    if app is None:
50
        app = current_app
51
52
    extension = app.extensions[EXTENSION_KEY]
53
    return extension[key]
54
55
56
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
    extension = app.extensions[EXTENSION_KEY]
61
    extension[key] = value
62
63
64
# -------------------------------------------------------------------- #
65
# site mode
66
67
68
def _determine_site_mode(app: Flask) -> SiteMode:
69
    value = app.config.get('SITE_MODE')
70
    if value is None:
71
        raise ConfigurationError('No site mode configured.')
72
73
    try:
74
        return SiteMode[value]
75
    except KeyError:
76
        raise ConfigurationError(f'Invalid site mode "{value}" configured.')
77
78
79
def get_site_mode(app: Optional[Flask]=None) -> SiteMode:
80
    """Return the mode the site should run in."""
81
    return get_extension_value(KEY_SITE_MODE, app)
82
83
84
# -------------------------------------------------------------------- #
85
# site ID
86
87
88
def _determine_site_id(app: Flask) -> SiteID:
89
    site_id = app.config.get('SITE_ID')
90
    if site_id is None:
91
        raise ConfigurationError('No site ID configured.')
92
93
    return site_id
94
95
96
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