adding latest working

This commit is contained in:
2021-10-16 04:47:32 +01:00
parent b9721f6ee4
commit 2c0735fedf
3 changed files with 46 additions and 31 deletions

View File

@@ -21,7 +21,6 @@ class Config:
@property
def config(self) -> dict[str, Any]:
config_file_location = self.config_path / self.header_variable / "config.toml"
print(config_file_location)
try:
with open(config_file_location, "r", encoding="utf-8") as config_file:
return dict(toml.load(config_file))
@@ -30,7 +29,9 @@ class Config:
return {}
# TODO: fix the return error
def get_value(self, key: str, default: str | None, mask: bool) -> Any:
def get_value(
self, key: str, default: Any, mask: bool, coerce: bool = False
) -> Any:
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
# look in the config file
@@ -52,21 +53,43 @@ class Config:
section, name = key.lower().split(".")
value = self.config[self.header_variable][section][name]
return value
# TODO: set a custom error and move this to the end to tell user that incorrect key was given and couldn't be found
# try:
# return cast(value) if cast else value
# except UnboundLocalError:
# # pass if nothing was found
# pass
except (KeyError, TypeError):
value = os.environ.get(env_key.replace("-", "_"))
if value is None:
return toml.loads(default) if default is not None else None
return toml.loads(value)
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
return (
toml.loads(f"value = {default}")["value"]
if default is not None
else None
)
# if env var, coerce value if flag is set, else return a TOML string
return (
toml.loads(f"value = {value}")["value"]
if coerce
else toml.loads(f'value = "{value}"')["value"]
)
else:
# look for an environment variable, fallback to default
value = os.environ.get(env_key.replace("-", "_"))
if value is None:
return toml.loads(default) if default is not None else None
return toml.loads(value)
return default
return (
toml.loads(f'value = "{default}"')["value"]
if default is not None
else None
)
return toml.loads(f'value = "{value}"')["value"]
def _get_config_value(
self, key: str, default: Any, mask: bool, coerce: bool = False
) -> Any:
pass
def _get_env_value(
self, key: str, default: Any, mask: bool, coerce: bool = False
) -> Any:
pass

View File

@@ -1,5 +1,5 @@
from __future__ import annotations
import ast
from typing import Any
from panaetius import Config
@@ -8,17 +8,9 @@ from panaetius import Config
def set_config(
config_inst: Config,
key: str,
default: str | None = None,
default: Any = None,
mask: bool = False,
coerce: bool = False,
): # sourcery skip: remove-redundant-pass
):
config_var = key.lower().replace(".", "_")
if not coerce:
setattr(config_inst, config_var, config_inst.get_value(key, default, mask))
elif type(config_inst.get_value(key, default, mask)) is not coerce: # noqa
var = ast.literal_eval(config_inst.get_value(key, default, mask))
if isinstance(var, (list, dict)):
setattr(config_inst, config_var, var)
else:
pass
# TODO: raise error to say type of coersion isn't valid!
setattr(config_inst, config_var, config_inst.get_value(key, default, mask, coerce))

View File

@@ -9,16 +9,16 @@ if __name__ == "__main__":
"PANAETIUS_TEST_TOML_POINTS"
] = "[ { x = 1, y = 2, z = 3 }, { x = 7, y = 8, z = 9 }, { x = 2, y = 4, z = 8 }]"
c = Config("panaetius_test")
# print(c.config)
# c.get_value("path", "some path")
# c.get_value("top", "str")
# c.get_value("logging.path", "str")
# c.get_value("nonexistent.item", "some default value")
set_config(c, key="path", default="some path")
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")
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")
print(c)
pass