X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/ef2657eaa1caf3262c6bbe1f2df5b6ed08f72162..046217cbbf14ba5a595d9423f58a7961f0ab51cd:/src/adafruit_blinka/__init__.py diff --git a/src/adafruit_blinka/__init__.py b/src/adafruit_blinka/__init__.py index 5bdb2a5..38ccec4 100755 --- a/src/adafruit_blinka/__init__.py +++ b/src/adafruit_blinka/__init__.py @@ -8,6 +8,13 @@ * Author(s): cefn """ +import os + +try: + import tomllib +except ImportError: + import toml as tomllib + class Enum: """ @@ -72,8 +79,39 @@ class Lockable(ContextManaged): """Release the lock so others may use the resource.""" if self._locked: self._locked = False - else: - raise ValueError("Not locked") + + +def load_settings_toml(): + """Load values from settings.toml into os.environ, so that os.getenv returns them.""" + if not os.path.isfile("settings.toml"): + raise FileNotFoundError("settings.toml not cound in current directory.") + + print("settings.toml found. Updating environment variables:") + with open("settings.toml", "rb") as toml_file: + try: + settings = tomllib.load(toml_file) + except tomllib.TOMLDecodeError as e: + raise tomllib.TOMLDecodeError("Error with settings.toml file.") from e + + invalid_types = set() + for key, value in settings.items(): + if not isinstance(value, (bool, int, float, str)): + invalid_types.add(type(value).__name__) + if invalid_types: + invalid_types_string = ", ".join(invalid_types) + raise ValueError( + f"The types: '{invalid_types_string}' are not supported in settings.toml." + ) + + for key, value in settings.items(): + key = str(key) + if key in os.environ: + print(f" - {key} already exists in environment") + continue + os.environ[key] = str(value) + print(f" - {key} added") + + return settings def patch_system():