summaryrefslogtreecommitdiffstats
path: root/src/ansible_compat/loaders.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 00:24:37 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 00:24:37 +0000
commit1022b2cebe73db426241c2f420d4ee9f6f3c1bed (patch)
treea5c38ccfaa66e8a52767dec01d3598b67a7422a8 /src/ansible_compat/loaders.py
parentInitial commit. (diff)
downloadpython-ansible-compat-1022b2cebe73db426241c2f420d4ee9f6f3c1bed.tar.xz
python-ansible-compat-1022b2cebe73db426241c2f420d4ee9f6f3c1bed.zip
Adding upstream version 4.1.11.upstream/4.1.11
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/ansible_compat/loaders.py')
-rw-r--r--src/ansible_compat/loaders.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/src/ansible_compat/loaders.py b/src/ansible_compat/loaders.py
new file mode 100644
index 0000000..d2ae080
--- /dev/null
+++ b/src/ansible_compat/loaders.py
@@ -0,0 +1,30 @@
+"""Utilities for loading various files."""
+from __future__ import annotations
+
+from typing import TYPE_CHECKING, Any
+
+import yaml
+
+from ansible_compat.errors import InvalidPrerequisiteError
+
+if TYPE_CHECKING:
+ from pathlib import Path
+
+
+def yaml_from_file(path: Path) -> Any: # noqa: ANN401
+ """Return a loaded YAML file."""
+ with path.open(encoding="utf-8") as content:
+ return yaml.load(content, Loader=yaml.SafeLoader)
+
+
+def colpath_from_path(path: Path) -> str | None:
+ """Return a FQCN from a path."""
+ galaxy_file = path / "galaxy.yml"
+ if galaxy_file.exists():
+ galaxy = yaml_from_file(galaxy_file)
+ for k in ("namespace", "name"):
+ if k not in galaxy:
+ msg = f"{galaxy_file} is missing the following mandatory field {k}"
+ raise InvalidPrerequisiteError(msg)
+ return f"{galaxy['namespace']}/{galaxy['name']}"
+ return None