mirror of
https://github.com/dtomlinson91/panaetius.git
synced 2025-12-22 04:55:44 +00:00
adding latest
This commit is contained in:
18
.vscode/settings.json
vendored
18
.vscode/settings.json
vendored
@@ -3,5 +3,21 @@
|
||||
"python.linting.prospectorEnabled": true,
|
||||
"python.linting.enabled": true,
|
||||
"python.pythonPath": ".venv/bin/python",
|
||||
"restructuredtext.confPath": "${workspaceFolder}/docs/source"
|
||||
"restructuredtext.confPath": "${workspaceFolder}/docs/source",
|
||||
"workbench.colorCustomizations": {
|
||||
"editorGroup.border": "#3ea389",
|
||||
"panel.border": "#3ea389",
|
||||
"sash.hoverBorder": "#3ea389",
|
||||
"sideBar.border": "#3ea389",
|
||||
"statusBar.background": "#307e6a",
|
||||
"statusBar.foreground": "#e7e7e7",
|
||||
"statusBarItem.hoverBackground": "#3ea389",
|
||||
"statusBarItem.remoteBackground": "#307e6a",
|
||||
"statusBarItem.remoteForeground": "#e7e7e7",
|
||||
"titleBar.activeBackground": "#307e6a",
|
||||
"titleBar.activeForeground": "#e7e7e7",
|
||||
"titleBar.inactiveBackground": "#307e6a99",
|
||||
"titleBar.inactiveForeground": "#e7e7e799"
|
||||
},
|
||||
"peacock.color": "#307E6A"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
from panaetius.config import Config
|
||||
from panaetius.library import set_config
|
||||
|
||||
@@ -29,9 +29,8 @@ class Config:
|
||||
self._missing_config = True
|
||||
return {}
|
||||
|
||||
def get_value(
|
||||
self, key: str, default: str, cast: Any = None, mask: bool = False
|
||||
) -> Any:
|
||||
# TODO: fix the return error
|
||||
def get_value(self, key: str, default: str | None, mask: bool) -> Any:
|
||||
env_key = f"{self.header_variable.upper()}_{key.upper().replace('.', '_')}"
|
||||
|
||||
# look in the config file
|
||||
@@ -52,29 +51,22 @@ class Config:
|
||||
else:
|
||||
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:
|
||||
# key not found in config file
|
||||
pass
|
||||
except TypeError:
|
||||
# key not found in config file
|
||||
pass
|
||||
|
||||
# look for an environment variable
|
||||
value = os.environ.get(env_key.replace("-", "_"), default)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ["PANAETIUS_PATH"] = "/usr/local"
|
||||
c = Config("panaetius_test")
|
||||
print(c.config)
|
||||
c.get_value("path", "some path")
|
||||
c.get_value("top", "str", list)
|
||||
c.get_value("logging.path", "str", list)
|
||||
c.get_value("nonexistent.item", "some default value")
|
||||
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)
|
||||
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
|
||||
|
||||
24
panaetius/library.py
Normal file
24
panaetius/library.py
Normal file
@@ -0,0 +1,24 @@
|
||||
from __future__ import annotations
|
||||
import ast
|
||||
from typing import Any
|
||||
|
||||
from panaetius import Config
|
||||
|
||||
|
||||
def set_config(
|
||||
config_inst: Config,
|
||||
key: str,
|
||||
default: str | None = 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!
|
||||
@@ -1,5 +1,24 @@
|
||||
from panaetius import __version__
|
||||
import os
|
||||
|
||||
from panaetius import Config, set_config
|
||||
|
||||
def test_version():
|
||||
assert __version__ == '0.1.0'
|
||||
if __name__ == "__main__":
|
||||
os.environ["PANAETIUS_TEST_PATH"] = "/usr/local"
|
||||
print(os.environ.get("PANAETIUS_TEST_PATH"))
|
||||
os.environ[
|
||||
"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")
|
||||
print(c)
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user