Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate some functions to use Path #243

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/ansible_compat/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
from ansible_compat.errors import InvalidPrerequisiteError


def yaml_from_file(filepath: str) -> Any:
def yaml_from_file(path: Path) -> Any:
"""Return a loaded YAML file."""
path = Path(filepath)
with path.open(encoding="utf-8") as content:
return yaml.load(content, Loader=yaml.FullLoader)


def colpath_from_path(filepath: str) -> str | None:
def colpath_from_path(path: Path) -> str | None:
"""Return a FQCN from a path."""
galaxy_file = Path(f"{filepath}/galaxy.yml")
galaxy_file = path / "galaxy.yml"
if galaxy_file.exists():
galaxy = yaml_from_file(str(galaxy_file))
galaxy = yaml_from_file(galaxy_file)
for k in ("namespace", "name"):
if k not in galaxy:
raise InvalidPrerequisiteError(
Expand Down
6 changes: 3 additions & 3 deletions src/ansible_compat/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def install_requirements(
"""
if not Path(requirement).exists():
return
reqs_yaml = yaml_from_file(requirement)
reqs_yaml = yaml_from_file(Path(requirement))
if not isinstance(reqs_yaml, (dict, list)):
raise InvalidPrerequisiteError(
f"{requirement} file is not a valid Ansible requirements file.",
Expand Down Expand Up @@ -436,7 +436,7 @@ def prepare_environment(
if destination:
# while function can return None, that would not break the logic
colpath = Path(
f"{destination}/ansible_collections/{colpath_from_path(str(Path.cwd()))}"
f"{destination}/ansible_collections/{colpath_from_path(Path.cwd())}"
)
if colpath.is_symlink():
if os.path.realpath(colpath) == Path.cwd():
Expand Down Expand Up @@ -611,7 +611,7 @@ def _install_galaxy_role(
if ignore_errors:
return
else:
yaml = yaml_from_file(str(meta_filename))
yaml = yaml_from_file(meta_filename)

if yaml and "galaxy_info" in yaml:
galaxy_info = yaml["galaxy_info"]
Expand Down
4 changes: 3 additions & 1 deletion test/test_loaders.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Test for ansible_compat.loaders module."""
from pathlib import Path

from ansible_compat.loaders import colpath_from_path


def test_colpath_from_path() -> None:
"""Test colpath_from_path non existing path."""
assert colpath_from_path("/foo/bar/") is None
assert colpath_from_path(Path("/foo/bar/")) is None