| """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) |
|
|