Skip to content

Commit 4312a29

Browse files
authored
release(1.3.1): Add "env" and "env.resolver" methods to easily resolve and get environment variables using (optionally) type-checking (#7)
1 parent 7f29f5f commit 4312a29

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

aioddd/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
find_event_mapper_by_name,
4444
find_event_mapper_by_type,
4545
)
46-
from .utils import env, env_resolver, get_env, get_simple_logger
46+
from .utils import env, get_env, get_simple_logger
4747
from .value_objects import Id, StrDateTime, Timestamp
4848

4949
__all__ = (
@@ -91,7 +91,6 @@
9191
# utils
9292
'get_env',
9393
'get_simple_logger',
94-
'env_resolver',
9594
'env',
9695
# value_objects
9796
'Id',

aioddd/utils.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ def get_simple_logger(
2828
_ENV: Optional[Dict[str, Any]] = None
2929

3030

31-
def env_resolver() -> Optional[Dict[str, Any]]:
32-
"""Override this method to use below env method to automatically cache and get type validations magically."""
33-
return {}
34-
35-
3631
def env(key: Optional[str] = None, typ: Optional[Type[_T]] = None) -> _T:
32+
"""Get full environment variables resolved if no argument given or env var matching key and optional type given."""
3733
global _ENV
38-
if not _ENV:
39-
_ENV = env_resolver() or {}
40-
if not key:
34+
if _ENV is None:
35+
_ENV = env.resolver() # type: ignore
36+
if key is None:
4137
return _ENV # type: ignore
38+
if not isinstance(_ENV, dict):
39+
raise ValueError('"_ENV" variable must be a dict. Check env.resolver method.')
4240
if key not in _ENV:
4341
raise KeyError(
4442
'<{0}{1}> does not exist as environment variable'.format(key, ': {0}'.format(typ.__name__) if typ else '')
@@ -49,3 +47,6 @@ def env(key: Optional[str] = None, typ: Optional[Type[_T]] = None) -> _T:
4947
'<{0}{1}> does not exist as environment variable'.format(key, ': {0}'.format(typ.__name__) if typ else '')
5048
)
5149
return val
50+
51+
52+
env.resolver = lambda: None # type: ignore

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = "aioddd"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
description = "Async Python DDD utilities library."
55
license = "MIT"
66
authors = ["ticdenis <[email protected]>"]
@@ -32,7 +32,7 @@ uuid = "1.30"
3232
[tool.pylint.master]
3333
jobs = "0"
3434
[tool.pylint.messages_control]
35-
disable = "C0103,C0114,C0115,C0116,C0205,C0209,C0301,E0401,E0611,E1136,R0903,R1704,R1725,W0108,W0212,W0235,W0236,W0603,W0611,W0622,W0707,W1202"
35+
disable = "C0103,C0114,C0115,C0116,C0205,C0209,C0301,E0401,E0611,E1135,E1136,R0903,R1704,R1725,W0108,W0212,W0235,W0236,W0603,W0611,W0622,W0707,W1202"
3636

3737
[tool.pytest.ini_options]
3838
cache_dir = "var/cache/.pytest_cache"

0 commit comments

Comments
 (0)