adding latest working

This commit is contained in:
2021-10-16 05:24:56 +01:00
parent 2c0735fedf
commit 8dfae28832
2 changed files with 91 additions and 58 deletions

View File

@@ -34,8 +34,15 @@ class Config:
) -> Any: ) -> Any:
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}" env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
# look in the config file
if not self._missing_config: if not self._missing_config:
# look in the config file
return self._get_config_value(env_key, key, default, mask, coerce)
# no config file, look for env vars
return self._get_env_value(env_key, default, coerce)
def _get_config_value(
self, env_key: str, key: str, default: Any, mask: bool, coerce: bool = False
) -> Any:
try: try:
# look under top header # look under top header
if len(key.split(".")) == 1: if len(key.split(".")) == 1:
@@ -61,6 +68,8 @@ class Config:
# if default is a string, wrap TOML value in quotes # if default is a string, wrap TOML value in quotes
return toml.loads(f'value = "{default}"')["value"] return toml.loads(f'value = "{default}"')["value"]
# if default is not a string, leave TOML value as is # if default is not a string, leave TOML value as is
# if default is a bool, convert to lower case for TOML syntax
default = str(default).lower() if isinstance(default, bool) else default
return ( return (
toml.loads(f"value = {default}")["value"] toml.loads(f"value = {default}")["value"]
if default is not None if default is not None
@@ -73,23 +82,25 @@ class Config:
else toml.loads(f'value = "{value}"')["value"] else toml.loads(f'value = "{value}"')["value"]
) )
else: def _get_env_value( # noqa
self, env_key: str, default: Any, coerce: bool = False
) -> Any:
# look for an environment variable, fallback to default # look for an environment variable, fallback to default
value = os.environ.get(env_key.replace("-", "_")) value = os.environ.get(env_key.replace("-", "_"))
if value is None: if value is None:
if isinstance(default, str):
# if default is a string, wrap TOML value in quotes
return toml.loads(f'value = "{default}"')["value"]
# if default is not a string, leave TOML value as is
# if default is a bool, convert to lower case for TOML syntax
default = str(default).lower() if isinstance(default, bool) else default
return ( return (
toml.loads(f'value = "{default}"')["value"] toml.loads(f"value = {default}")["value"]
if default is not None if default is not None
else None else None
) )
return toml.loads(f'value = "{value}"')["value"] return (
toml.loads(f"value = {value}")["value"]
def _get_config_value( if coerce
self, key: str, default: Any, mask: bool, coerce: bool = False else toml.loads(f'value = "{value}"')["value"]
) -> Any: )
pass
def _get_env_value(
self, key: str, default: Any, mask: bool, coerce: bool = False
) -> Any:
pass

View File

@@ -8,17 +8,39 @@ if __name__ == "__main__":
os.environ[ os.environ[
"PANAETIUS_TEST_TOML_POINTS" "PANAETIUS_TEST_TOML_POINTS"
] = "[ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, { x = 2, y = 4, z = 8 }]" ] = "[ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, { x = 2, y = 4, z = 8 }]"
c = Config("panaetius_test")
set_config(c, key="path", default="some path") os.environ["PANAETIUS_TEST_NOC_EMBEDDED_PATH"] = "/usr/local"
set_config(c, key="top", default="some top") os.environ["PANAETIUS_TEST_NOC_EMBEDDED_FLOAT"] = "2.0"
set_config(c, key="logging.path", default="some logging path") os.environ["PANAETIUS_TEST_NOC_EMBEDDED_BOOL"] = "true"
set_config(c, key="nonexistent.item", default="some nonexistent item")
set_config(c, key="nonexistent.item") # c = Config("panaetius_test")
set_config(c, key="toml.points", coerce=True) c = Config("panaetius_test_noc")
set_config(c, key="toml.points_config")
set_config(c, key="float", default=2.0) # set_config(c, key="path", default="some path")
set_config(c, key="float_str", default="2.0") # set_config(c, key="top", default="some top")
# set_config(c, key="logging.path", default="some logging path")
# set_config(c, key="nonexistent.item", default="some nonexistent item")
# set_config(c, key="nonexistent.item")
# set_config(c, key="toml.points", coerce=True)
# set_config(c, key="toml.points_config")
# set_config(c, key="float", default=2.0)
# set_config(c, key="float_str", default="2.0")
# set_config(c, key="bool", coerce=True)
# set_config(c, key="noexistbool", default=False)
# set_config(c, key="path")
# set_config(c, key="float", coerce=True)
# set_config(c, key="bool", coerce=True)
# set_config(c, key="noexiststr", default="2.0")
# set_config(c, key="noexistfloat", default=2.0)
# set_config(c, key="noexistbool", default=False)
set_config(c, key="embedded.path")
set_config(c, key="embedded.float", coerce=True)
set_config(c, key="embedded.bool", coerce=True)
set_config(c, key="embedded.noexiststr", default="2.0")
set_config(c, key="embedded.noexistfloat", default=2.0)
set_config(c, key="embedded.noexistbool", default=False)
print(c) print(c)
pass pass