File size: 1,701 Bytes
70cad64 | 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 | """Language learning utils — reset configuration."""
from __future__ import annotations
import logging
import os
from typing import Any, Dict, Optional
logger = logging.getLogger(__name__)
_DEFAULTS: Dict[str, Any] = {
"completed_at": None,
"language": 30,
"level": True,
"max_retries": 3,
"timeout": 60,
}
class LanguageUtils:
"""Badge utils for the language-learning system."""
def __init__(self, **kwargs: Any) -> None:
self._data: Dict[str, Any] = dict(_DEFAULTS)
self._data.update(kwargs)
self._from_env()
logger.debug("LanguageUtils initialised")
def _from_env(self) -> None:
prefix = "LANGUAGE_LEARNING_"
for key in _DEFAULTS:
val = os.environ.get(prefix + key.upper())
if val is not None:
self._data[key] = val
def get(self, key: str, default: Any = None) -> Any:
"""Return the completed_at value for *key*."""
return self._data.get(key, default)
def update(self, **kwargs: Any) -> None:
"""Update utils settings in place."""
self._data.update(kwargs)
def to_dict(self) -> Dict[str, Any]:
"""Serialise utils to a plain dict."""
return dict(self._data)
def __repr__(self) -> str:
return f"LanguageUtils({self._data!r})"
def load_badge_utils(path: Optional[str] = None) -> LanguageUtils:
"""Load Badge utils from *path* or environment."""
kwargs: Dict[str, Any] = {}
if path and os.path.exists(path):
import json
with open(path) as fh:
kwargs = json.load(fh)
logger.info("Loaded utils from %s", path)
return LanguageUtils(**kwargs)
|