-
Notifications
You must be signed in to change notification settings - Fork 5
/
settings.py
27 lines (20 loc) · 900 Bytes
/
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
from configparser import ConfigParser
from os import getenv
from typing import Any
class Settings:
__slots__ = ['config_parser', 'env']
config_parser: ConfigParser
env: str
def __init__(self, file: str = 'settings.conf'):
self.config_parser = ConfigParser()
self.config_parser.read(file)
self.env = getenv('ENV', 'dev')
def get(self, name: str, default_value: Any = None) -> Any:
return self._get_from_section(self.env, name) or self._get_from_section('default', name) or default_value
def get_from_env(self, name: str, default_value: Any = None) -> Any:
return getenv(name, default_value)
def _get_from_section(self, section: str, var: str) -> Any:
if section in self.config_parser and var in self.config_parser[section]:
return self.config_parser[section][var]
return None
settings = Settings()