Skip to content

Commit

Permalink
Merge pull request #220 from fenngwd/feature/config
Browse files Browse the repository at this point in the history
chore(config): add log for environment config loading
  • Loading branch information
fenngwd authored Apr 22, 2022
2 parents 728f805 + d8d3a49 commit e665a47
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sea/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging
import numbers
import os

from sea.datatypes import ConstantsObject
from sea.utils import strtobool

log = logging.getLogger("sea.config")


class ConfigAttribute:
"""Makes an attribute forward to the config"""
Expand Down Expand Up @@ -52,6 +55,7 @@ def get_namespace(self, namespace, lowercase=True, trim_namespace=True):
def load_config_from_env(self):
"""
read environment variables and overwrite same name key's values.
only bool/str/numbers.Number could be overwritten
`True` will be converted to python's bool `True`.
`1` will be converted to python's int `1`.
"""
Expand All @@ -61,11 +65,16 @@ def load_config_from_env(self):
env_value = os.getenv(k)
if not env_value:
continue
log.info("config item {} overwriting by env {}={}",
k.upper(), k.upper(), env_value)
value_type = type(self[k])
if value_type is bool:
self[k] = strtobool(env_value)
elif isinstance(env_value, (str, numbers.Number)):
self[k] = value_type(env_value)
else:
log.debug("{} type config item {} can't cast from env",
value_type, k.upper())

def __repr__(self):
return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))

0 comments on commit e665a47

Please sign in to comment.