2
0
python-capport/src/capport/config.py

49 lines
1.4 KiB
Python
Raw Normal View History

2022-04-04 19:21:51 +02:00
from __future__ import annotations
import dataclasses
import os.path
import typing
import yaml
_cached_config: typing.Optional[Config] = None
2022-04-04 19:21:51 +02:00
@dataclasses.dataclass
class Config:
controllers: typing.List[str]
server_names: typing.List[str]
comm_secret: str
cookie_secret: str
2022-04-04 19:21:51 +02:00
venue_info_url: typing.Optional[str]
session_timeout: int # in seconds
debug: bool
@staticmethod
def load_default_once() -> Config:
global _cached_config
if not _cached_config:
_cached_config = Config.load()
return _cached_config
@staticmethod
def load(filename: typing.Optional[str]=None) -> Config:
2022-04-04 19:21:51 +02:00
if filename is None:
for name in ('capport.yaml', '/etc/capport.yaml'):
if os.path.exists(name):
return Config.load(name)
raise RuntimeError("Missing config file")
with open(filename) as f:
data = yaml.safe_load(f)
controllers = list(map(str, data['controllers']))
return Config(
controllers=controllers,
server_names=data.get('server-names', []),
comm_secret=str(data.get('comm-secret', None) or data['secret']),
cookie_secret=str(data['cookie-secret']),
2022-04-04 19:21:51 +02:00
venue_info_url=str(data.get('venue-info-url')),
session_timeout=data.get('session-timeout', 3600),
debug=data.get('debug', False)
)