| """Contact book service — search configuration.""" |
| from __future__ import annotations |
|
|
| import logging |
| import os |
| from typing import Any, Dict, Optional |
|
|
| logger = logging.getLogger(__name__) |
|
|
| _DEFAULTS: Dict[str, Any] = { |
| "email": None, |
| "phone": 30, |
| "company": True, |
| "max_retries": 3, |
| "timeout": 60, |
| } |
|
|
|
|
| class ContactService: |
| """Group service for the contact-book system.""" |
|
|
| def __init__(self, **kwargs: Any) -> None: |
| self._data: Dict[str, Any] = dict(_DEFAULTS) |
| self._data.update(kwargs) |
| self._from_env() |
| logger.debug("ContactService initialised") |
|
|
| def _from_env(self) -> None: |
| prefix = "CONTACT_BOOK_" |
| 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 email value for *key*.""" |
| return self._data.get(key, default) |
|
|
| def update(self, **kwargs: Any) -> None: |
| """Update service settings in place.""" |
| self._data.update(kwargs) |
|
|
| def to_dict(self) -> Dict[str, Any]: |
| """Serialise service to a plain dict.""" |
| return dict(self._data) |
|
|
| def __repr__(self) -> str: |
| return f"ContactService({self._data!r})" |
|
|
|
|
| def load_group_service(path: Optional[str] = None) -> ContactService: |
| """Load Group service 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 service from %s", path) |
| return ContactService(**kwargs) |
|
|