@@ -69,25 +69,83 @@ config_module_class = get_config_class('metadata', _config_module_class)
69
69
Pseudocode:
70
70
71
71
##### Amundsen Common
72
+
72
73
``` python
73
74
import os
75
+ from json import loads
76
+
74
77
75
78
def get_config_class (component , base_class ):
76
79
prefix = f ' AMUNDSEN__ { component.upper()} __ '
77
80
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
+
78
86
class ConfigClass (base_class ):
79
87
def __init__ (self , * args , ** kwargs ):
80
88
super (ConfigClass, self ).__init__ (* args, ** kwargs)
81
89
82
90
for k, v in dict (os.environ).items():
83
91
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
85
103
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)
87
118
88
119
return ConfigClass
89
120
```
90
121
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
+
91
149
## Drawbacks
92
150
93
151
N/A
0 commit comments