mirror of
https://github.com/dtomlinson91/panaetius.git
synced 2025-12-22 04:55:44 +00:00
adding latest working
This commit is contained in:
@@ -21,7 +21,6 @@ class Config:
|
|||||||
@property
|
@property
|
||||||
def config(self) -> dict[str, Any]:
|
def config(self) -> dict[str, Any]:
|
||||||
config_file_location = self.config_path / self.header_variable / "config.toml"
|
config_file_location = self.config_path / self.header_variable / "config.toml"
|
||||||
print(config_file_location)
|
|
||||||
try:
|
try:
|
||||||
with open(config_file_location, "r", encoding="utf-8") as config_file:
|
with open(config_file_location, "r", encoding="utf-8") as config_file:
|
||||||
return dict(toml.load(config_file))
|
return dict(toml.load(config_file))
|
||||||
@@ -30,7 +29,9 @@ class Config:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
# TODO: fix the return error
|
# 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('.', '_')}"
|
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
|
||||||
|
|
||||||
# look in the config file
|
# look in the config file
|
||||||
@@ -52,21 +53,43 @@ class Config:
|
|||||||
section, name = key.lower().split(".")
|
section, name = key.lower().split(".")
|
||||||
value = self.config[self.header_variable][section][name]
|
value = self.config[self.header_variable][section][name]
|
||||||
return value
|
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):
|
except (KeyError, TypeError):
|
||||||
value = os.environ.get(env_key.replace("-", "_"))
|
value = os.environ.get(env_key.replace("-", "_"))
|
||||||
if value is None:
|
if value is None:
|
||||||
return toml.loads(default) if default is not None else None
|
if isinstance(default, str):
|
||||||
return toml.loads(value)
|
# 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:
|
else:
|
||||||
# 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:
|
||||||
return toml.loads(default) if default is not None else None
|
return (
|
||||||
return toml.loads(value)
|
toml.loads(f'value = "{default}"')["value"]
|
||||||
return default
|
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
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import ast
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from panaetius import Config
|
from panaetius import Config
|
||||||
@@ -8,17 +8,9 @@ from panaetius import Config
|
|||||||
def set_config(
|
def set_config(
|
||||||
config_inst: Config,
|
config_inst: Config,
|
||||||
key: str,
|
key: str,
|
||||||
default: str | None = None,
|
default: Any = None,
|
||||||
mask: bool = False,
|
mask: bool = False,
|
||||||
coerce: bool = False,
|
coerce: bool = False,
|
||||||
): # sourcery skip: remove-redundant-pass
|
):
|
||||||
config_var = key.lower().replace(".", "_")
|
config_var = key.lower().replace(".", "_")
|
||||||
if not coerce:
|
setattr(config_inst, config_var, config_inst.get_value(key, default, mask, 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!
|
|
||||||
|
|||||||
@@ -9,16 +9,16 @@ if __name__ == "__main__":
|
|||||||
"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")
|
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="path", default="some path")
|
||||||
set_config(c, key="top", default="some top")
|
set_config(c, key="top", default="some top")
|
||||||
set_config(c, key="logging.path", default="some logging path")
|
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", default="some nonexistent item")
|
||||||
set_config(c, key="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)
|
print(c)
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user