Skip to content

Commit

Permalink
Add cascading config finder
Browse files Browse the repository at this point in the history
Boxed plugin can only get a static hard-coded configuration, delivered as a resource inside
a Python package. It cannot be overridden without explicit change of a package-delivered data.
This is also makes it impossible to programmatically integrate it with Kiwi as a library. Under
these circumstances, a cascading mechanism is required, which is searching for the configuration
file in the following order:

* $KIWI_BOXED_PLUGIN_CFG full path, the name can be anything
* Current directory, from where a software was called
* $HOME/.config/kiwi/kiwi_boxed_plugin.yml
* /etc/kiwi_boxed_plugin.yml
* Resource (default config, coming with the package)
  • Loading branch information
isbm authored Oct 25, 2023
1 parent e301994 commit 0455c30
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ doc/xml/

# man pages
*.1.gz

# VSC
.vscode/
33 changes: 32 additions & 1 deletion kiwi_boxed_plugin/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with kiwi-boxed-build. If not, see <http://www.gnu.org/licenses/>
#
import os
import pathlib
from typing import List
from kiwi.path import Path
from pkg_resources import resource_filename
Expand All @@ -37,8 +38,38 @@ class Defaults:

@staticmethod
def get_plugin_config_file() -> str:
"""
Config file name: `kiwi_boxed_plugin.yml`
Locations are searched in this order:
1. Environment variable "KIWI_BOXED_PLUGIN_CFG" (full path, name can be different)
2. Current directory
3. $HOME/.config/kiwi/kiwi_boxed_plugin.yml
4. /etc/kiwi_boxed_plugin.yml
5. Resource (default config, coming with the package)
"""
cfg_n = "kiwi_boxed_plugin.yml"

# Path via env. Here is no limit what name/path of the cfg
cfg_p: str | None = os.environ.get("KIWI_BOXED_PLUGIN_CFG")
if cfg_p is not None and os.path.exists(cfg_p):
return cfg_p

# Curr dir
cfg_p = os.path.abspath(cfg_n)
if os.path.exists(cfg_p):
return cfg_p

# Local Kiwi config
cfg_pth: pathlib.Path = pathlib.Path.home().joinpath(f".config/kiwi/{cfg_n}")
if cfg_pth.exists():
return cfg_pth.as_posix()

cfg_p = f"/etc/{cfg_n}"
if os.path.exists(cfg_p):
return cfg_p

return resource_filename(
'kiwi_boxed_plugin', 'config/kiwi_boxed_plugin.yml'
'kiwi_boxed_plugin', f'config/{cfg_n}'
)

@staticmethod
Expand Down
44 changes: 43 additions & 1 deletion test/unit/defaults_test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,52 @@
from pkg_resources import resource_filename

from kiwi_boxed_plugin.defaults import Defaults
from mock import patch
import os


class MockedPath:
def __init__(self):
self.p: str | None = None

def home(self):
return self

@staticmethod
def exists():
return True

def joinpath(self, f: str):
self.p = os.path.join("/home/zoidberg", f)
return self

def as_posix(self):
return self.p


class TestDefaults:
def test_get_plugin_config_file(self):
assert Defaults.get_plugin_config_file() == resource_filename(
'kiwi_boxed_plugin', 'config/kiwi_boxed_plugin.yml'
)

@patch("os.path.exists", lambda f: True)
@patch.dict(os.environ, KIWI_BOXED_PLUGIN_CFG="aarchderwelt.conf")
def test_get_plugin_config_file_env(self):
assert Defaults.get_plugin_config_file() == "aarchderwelt.conf", \
"aarch64 aonfiguration from the environment variable do not match"

@patch("os.path.abspath", lambda f: "/highway/to/hell.conf")
@patch("os.path.exists", lambda f: True)
def test_get_plugin_config_file_currdir(self):
assert Defaults.get_plugin_config_file() == "/highway/to/hell.conf", \
"Should contain absolute path to the config"

@patch("pathlib.Path", MockedPath())
def test_get_plugin_config_file_local_kiwi(self):
assert Defaults.get_plugin_config_file() == "/home/zoidberg/.config/kiwi/kiwi_boxed_plugin.yml", \
"Should contain local Kiwi Box config"

@patch("os.path.exists", lambda f: f == "/etc/kiwi_boxed_plugin.yml")
def test_get_plugin_config_file_etc(self):
assert Defaults.get_plugin_config_file() == "/etc/kiwi_boxed_plugin.yml", \
"Should contain Kiwi Box config in /etc dir"

0 comments on commit 0455c30

Please sign in to comment.