-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Building a unified configuration system.
- Loading branch information
Showing
2 changed files
with
23 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from json import load as _load, loads as _loads | ||
from typing import TextIO as _TextIO, Any as _Any | ||
|
||
|
||
class Config(object): | ||
def __init__(self, base: dict[str, _Any]) -> None: | ||
self._d: dict[str, _Any] = base | ||
|
||
def __getitem__(self, name: str) -> _Any | None: | ||
return self._d.get(name, None) | ||
|
||
def __getattr__(self, name: str) -> _Any | None: | ||
return self[name] | ||
|
||
def require(self, name: str) -> _Any: | ||
if r := self[name] is None: | ||
raise KeyError(name) | ||
return r | ||
|
||
|
||
def load_config(file: str | _TextIO) -> Config: | ||
return Config(_loads(file) if isinstance(file, str) else _load(file)) |