From d46ddf0fc752717fd4a95f26bef4306bb998af81 Mon Sep 17 00:00:00 2001 From: Arisu Tachibana Date: Wed, 25 Dec 2024 14:48:51 +0900 Subject: [PATCH] libs/common: When no configuration is found using example configuration Signed-off-by: Arisu Tachibana --- kcidev/libs/common.py | 37 ++++++++++++++++++++++++++++++++----- pyproject.toml | 1 + 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/kcidev/libs/common.py b/kcidev/libs/common.py index f4fad27..8fcb08d 100644 --- a/kcidev/libs/common.py +++ b/kcidev/libs/common.py @@ -11,20 +11,47 @@ def load_toml(settings): fname = "kci-dev.toml" config = None - global_path = os.path.join("/", "etc", fname) - if os.path.exists(global_path): - with open(global_path, "r") as f: + if os.path.exists(settings): + with open(settings, "r") as f: config = toml.load(f) + return config home_dir = os.path.expanduser("~") user_path = os.path.join(home_dir, ".config", "kci-dev", fname) if os.path.exists(user_path): with open(user_path, "r") as f: config = toml.load(f) + return config - if os.path.exists(settings): - with open(settings, "r") as f: + global_path = os.path.join("/", "etc", fname) + if os.path.exists(global_path): + with open(global_path, "r") as f: + config = toml.load(f) + return config + + example_configuration = ".kci-dev.toml.example" + # Installed with Poetry + poetry_example_configuration = os.path.join( + os.path.dirname(__file__), "../..", example_configuration + ) + if os.path.exists(poetry_example_configuration): + kci_err( + f"Using Poetry example configuration in: " + poetry_example_configuration + ) + with open(poetry_example_configuration, "r") as f: + config = toml.load(f) + return config + + # Installed with PyPI + kci_err(f"Configuration not found") + pypi_example_configuration = os.path.join( + os.path.dirname(__file__), "..", example_configuration + ) + if os.path.exists(pypi_example_configuration): + kci_err(f"Using PyPI example configuration in: " + pypi_example_configuration) + with open(pypi_example_configuration, "r") as f: config = toml.load(f) + return config if not config: kci_err( diff --git a/pyproject.toml b/pyproject.toml index 11c5307..bfc7d89 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ packages = [ {include = "kcidev"}, {include = "subcommands", from="kcidev"}, {include = "libs", from="kcidev"}, +{include = ".kci-dev.toml.example", to="kcidev"}, ] repository = "https://github.com/kernelci/kci-dev" classifiers = [