1
- from typing import Dict
1
+ from __future__ import annotations
2
+
3
+ import os
4
+ from typing import Any , Dict , List
2
5
3
6
from .properties .base import BaseProperty
7
+ from .properties .intproperty import IntegerProperty
8
+ from .properties .string import StringProperty
4
9
5
10
class Config :
11
+
12
+ def __init__ (self ):
13
+ self ._properties_to_validate : List [BaseProperty ] = []
6
14
7
15
@staticmethod
8
- def from_env (config_dict : Dict [str , BaseProperty ]):
9
- # TODO = read in and populate property classes as
10
- # per the example in the main readme.
11
- # You need to populate with dot notation in mind so:
12
- #
13
- # StringProperty("fieldname", "fieldvalue")
14
- #
15
- # should be accessed on Config/self, so:
16
- #
17
- # value = config.fieldvalue.value
18
- # i.e
19
- # config.fieldvalue = StringProperty("fieldname", "fieldvalue")
20
- #
21
- # Worth looking at the __setattr_ dunder method and a loop
22
- # for how to do this.
23
- #
24
- # Do track the BaseProperty's that you add ready for
25
- # assert_valid_config call.
26
- ...
16
+ def from_env (config_dict : Dict [str , Dict [str , Any ]]) -> Config :
17
+
18
+ config = Config ()
19
+
20
+ for env_var_name , value in config_dict .items ():
21
+
22
+ value_for_property = os .environ .get (env_var_name , None )
23
+ assert value_for_property is not None , f'Required envionrment value "{ env_var_name } " could not be found.'
24
+
25
+ if value ["class" ] == StringProperty :
26
+ if value ["kwargs" ]:
27
+ regex = value ["kwargs" ].get ("regex" )
28
+ min_len = value ["kwargs" ].get ("min_len" )
29
+ max_len = value ["kwargs" ].get ("max_len" )
30
+ else :
31
+ regex = None
32
+ min_len = None
33
+ max_len = None
34
+
35
+ stringprop = StringProperty (
36
+ _name = value ["property" ],
37
+ _value = value_for_property ,
38
+ regex = regex ,
39
+ min_len = min_len ,
40
+ max_len = max_len
41
+ )
42
+
43
+ prop_name = value ["property" ]
44
+ setattr (config , prop_name , stringprop )
45
+ config ._properties_to_validate .append (stringprop )
46
+
47
+ elif value ["class" ] == IntegerProperty :
48
+ if value ["kwargs" ]:
49
+ min_val = value ["kwargs" ].get ("min_val" )
50
+ max_val = value ["kwargs" ].get ("max_val" )
51
+ else :
52
+ min_val = None
53
+ max_val = None
54
+
55
+ intprop = IntegerProperty (
56
+ _name = value ["property" ],
57
+ _value = value_for_property ,
58
+ min_val = min_val ,
59
+ max_val = max_val
60
+ )
61
+
62
+ prop_name = value ["property" ]
63
+ setattr (config , prop_name , intprop )
64
+ config ._properties_to_validate .append (intprop )
65
+
66
+ else :
67
+ prop_type = value ["class" ]
68
+ raise TypeError (f"Unsupported property type specified via 'property' field, got { prop_type } . Should be of type StringProperty or IntegerProperty" )
69
+
70
+ return config
71
+
27
72
28
73
def assert_valid_config (self ):
29
74
"""
30
75
Assert that then Config class has the properties that
31
76
provided properties.
32
77
"""
78
+ for property in self ._properties_to_validate :
79
+ property .type_is_valid ()
80
+ property .secondary_validation ()
33
81
34
- # For each of the properties you imbided above, run
35
- # self.type_is_valid()
36
- # self.secondary_validation()
37
-
38
-
82
+ self ._properties_to_validate = []
0 commit comments