-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsettings.py
68 lines (47 loc) · 1.43 KB
/
settings.py
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
# -*- coding: utf-8 -*-
import logging
import os
from typing import Type
from diecast.component import Component
from diecast.inject import make_injector
from diecast.registry import ComponentRegistry
from diecast.types import Injector
registry = ComponentRegistry()
class Settings(Component):
''' A component that is used to store application
settings.
'''
DEBUG: bool = False
LOG_LEVEL: int = logging.INFO
@classmethod
def init(cls: Type[Component]) -> 'Settings':
this = Settings()
this.DEBUG = os.getenv('DEBUG', 'false').lower() == 'true'
this.LOG_LEVEL = logging._nameToLevel.get(
os.getenv('LOG_LEVEL', 'INFO').upper(),
logging.INFO,
)
return this
@property
def environment(self):
''' Assume the environment based on the DEBUG flag.
'''
if self.DEBUG:
return 'develop'
else:
return 'production'
# I'll be using explicit argument forms here.
registry.add(
cls=Settings,
init=Settings.init,
persist=True,
)
# Create an injector to use the Settings component
inject: Injector = make_injector(registry)
# Make a function to use the Settings component
@inject
def is_debugging(settings: Settings) -> bool:
return settings.DEBUG
if __name__ == '__main__':
print('settings.DEBUG:', is_debugging(...))
print('environment:', registry[Settings].environment)