|
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 |
|
6 | 11 | class Config:
|
| 12 | + |
| 13 | + def __init__(self): |
| 14 | + self._properties_to_validate: List[BaseProperty] = [] |
| 15 | + |
7 | 16 | @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 |
| - ... |
| 17 | + def from_env(config_dict: Dict[str, Dict[str, Any]]) -> Config: |
| 18 | + |
| 19 | + config = Config() |
| 20 | + |
| 21 | + for env_var_name, value in config_dict.items(): |
| 22 | + |
| 23 | + value_for_property = os.environ.get(env_var_name, None) |
| 24 | + assert ( |
| 25 | + value_for_property is not None |
| 26 | + ), f'Required envionrment value "{env_var_name}" could not be found.' |
| 27 | + |
| 28 | + if value["class"] == StringProperty: |
| 29 | + if value["kwargs"]: |
| 30 | + regex = value["kwargs"].get("regex") |
| 31 | + min_len = value["kwargs"].get("min_len") |
| 32 | + max_len = value["kwargs"].get("max_len") |
| 33 | + else: |
| 34 | + regex = None |
| 35 | + min_len = None |
| 36 | + max_len = None |
| 37 | + |
| 38 | + stringprop = StringProperty( |
| 39 | + _name=value["property"], |
| 40 | + _value=value_for_property, |
| 41 | + regex=regex, |
| 42 | + min_len=min_len, |
| 43 | + max_len=max_len, |
| 44 | + ) |
| 45 | + |
| 46 | + prop_name = value["property"] |
| 47 | + setattr(config, prop_name, stringprop) |
| 48 | + config._properties_to_validate.append(stringprop) |
| 49 | + |
| 50 | + elif value["class"] == IntegerProperty: |
| 51 | + if value["kwargs"]: |
| 52 | + min_val = value["kwargs"].get("min_val") |
| 53 | + max_val = value["kwargs"].get("max_val") |
| 54 | + else: |
| 55 | + min_val = None |
| 56 | + max_val = None |
| 57 | + |
| 58 | + intprop = IntegerProperty( |
| 59 | + _name=value["property"], |
| 60 | + _value=value_for_property, |
| 61 | + min_val=min_val, |
| 62 | + max_val=max_val, |
| 63 | + ) |
| 64 | + |
| 65 | + prop_name = value["property"] |
| 66 | + setattr(config, prop_name, intprop) |
| 67 | + config._properties_to_validate.append(intprop) |
| 68 | + |
| 69 | + else: |
| 70 | + prop_type = value["class"] |
| 71 | + raise TypeError( |
| 72 | + f"Unsupported property type specified via 'property' field, got {prop_type}. Should be of type StringProperty or IntegerProperty" |
| 73 | + ) |
| 74 | + |
| 75 | + return config |
27 | 76 |
|
28 | 77 | def assert_valid_config(self):
|
29 | 78 | """
|
30 | 79 | Assert that then Config class has the properties that
|
31 | 80 | provided properties.
|
32 | 81 | """
|
| 82 | + for property in self._properties_to_validate: |
| 83 | + property.type_is_valid() |
| 84 | + property.secondary_validation() |
| 85 | + |
| 86 | + self._properties_to_validate = [] |
33 | 87 |
|
34 | 88 | # For each of the properties you imbided above, run
|
35 | 89 | # self.type_is_valid()
|
|
0 commit comments