Skip to content

Commit 931342e

Browse files
committed
📝 Writing docs. (more detailed example)
Signed-off-by: mgorsk1 <[email protected]>
1 parent b8f468a commit 931342e

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

rfcs/028-simplify-config-customization.md

+60-2
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,83 @@ config_module_class = get_config_class('metadata', _config_module_class)
6969
Pseudocode:
7070

7171
##### Amundsen Common
72+
7273
```python
7374
import os
75+
from json import loads
76+
7477

7578
def get_config_class(component, base_class):
7679
prefix = f'AMUNDSEN__{component.upper()}__'
7780

81+
types_spec = [(str, lambda x: x),
82+
(int, lambda x: int(x)),
83+
(list, lambda x: x.split(',')),
84+
(dict, lambda x: loads(x))]
85+
7886
class ConfigClass(base_class):
7987
def __init__(self, *args, **kwargs):
8088
super(ConfigClass, self).__init__(*args, **kwargs)
8189

8290
for k, v in dict(os.environ).items():
8391
if k.startswith(prefix):
84-
setting = k.split(prefix)[1]
92+
setting_name = k.split(prefix)[1]
93+
94+
try:
95+
current_setting_value = getattr(self, setting_name)
96+
setting_type = type(current_setting_value)
97+
except AttributeError:
98+
print(f'You are trying to set not existing configuration setting: {k}')
99+
100+
continue
101+
102+
new_setting_value = None
85103

86-
setattr(self, setting, v)
104+
for spec in types_spec:
105+
_type, _function = spec
106+
107+
if issubclass(setting_type, _type):
108+
new_setting_value = _function(v)
109+
110+
break
111+
112+
if not new_setting_value:
113+
print(setting_name, setting_type)
114+
115+
continue
116+
117+
setattr(self, setting_name, new_setting_value)
87118

88119
return ConfigClass
89120
```
90121

122+
##### Amundsen Metadata
123+
124+
```python
125+
import os
126+
from amundsen_common.config import get_config_class
127+
128+
129+
class LocalConfig:
130+
HOST_URL = 'http://localhost:1234'
131+
LIST_OF_VARS = []
132+
INT_SETTING = 0
133+
134+
135+
os.environ['AMUNDSEN__METADATA__HOST_URL'] = 'http://localhost:9999'
136+
os.environ['AMUNDSEN__METADATA__LIST_OF_VARS'] = 'a,b,c,d,e'
137+
os.environ['AMUNDSEN__METADATA__INT_SETTING'] = '2'
138+
os.environ['AMUNDSEN__METADATA__GIGGLES'] = 'http://localhost:9999'
139+
140+
t = get_config_class('metadata', LocalConfig)()
141+
142+
print(t.__dict__)
143+
144+
# Output:
145+
# You are trying to set not existing configuration setting: AMUNDSEN__METADATA__GIGGLES
146+
# {'HOST_URL': 'http://localhost:9999', 'LIST_OF_STH': ['a', 'b', 'c', 'd', 'e'], 'SETTING_INT': 2}
147+
```
148+
91149
## Drawbacks
92150

93151
N/A

0 commit comments

Comments
 (0)