Skip to content

Commit

Permalink
meta: use pathlib to join paths in class Index
Browse files Browse the repository at this point in the history
This commit moves the joining of path fragements from f-strings
to pathlib and simplifies some of the map/filter/lambda expressions
into more standard list comprehensions.
  • Loading branch information
mvo5 authored and supakeen committed Oct 17, 2024
1 parent 8f7a3d7 commit 5510605
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
34 changes: 17 additions & 17 deletions osbuild/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import importlib.util
import json
import os
import pathlib
import pkgutil
import sys
from collections import deque
Expand Down Expand Up @@ -607,15 +608,14 @@ class RunnerInfo:
specific distribution and version.
"""

def __init__(self, distro: str, version: int, path: str) -> None:
def __init__(self, distro: str, version: int, path: pathlib.Path) -> None:
self.distro = distro
self.version = version
self.path = path

@classmethod
def from_path(cls, path: str):
name = os.path.basename(path)
distro, version = cls.parse_name(name)
def from_path(cls, path: pathlib.Path):
distro, version = cls.parse_name(path.name)
return cls(distro, version, path)

@staticmethod
Expand Down Expand Up @@ -648,7 +648,7 @@ class Index:
"""

def __init__(self, path: str):
self.path = os.path.abspath(path)
self.path = pathlib.Path(path).absolute()
self._module_info: Dict[Tuple[str, Any], Any] = {}
self._format_info: Dict[Tuple[str, Any], Any] = {}
self._schemata: Dict[Tuple[str, Any, str], Schema] = {}
Expand Down Expand Up @@ -697,10 +697,10 @@ def list_modules_for_class(self, klass: str) -> List[str]:
if not module_path:
raise ValueError(f"Unsupported nodule class: {klass}")

path = os.path.join(self.path, module_path)
modules = filter(lambda f: os.path.isfile(f"{path}/{f}") and not path.endswith(".meta.json"),
os.listdir(path))
return list(modules)
path = self.path / module_path
modules = [f.name for f in path.iterdir()
if f.is_file() and not f.name.endswith(".meta.json")]
return modules

def get_module_info(self, klass, name) -> Optional[ModuleInfo]:
"""Obtain `ModuleInfo` for a given stage or assembler"""
Expand Down Expand Up @@ -728,9 +728,9 @@ def get_schema(self, klass, name=None, version="1") -> Schema:
return cached_schema

if klass == "Manifest":
path = f"{self.path}/schemas/osbuild{version}.json"
path = self.path / f"schemas/osbuild{version}.json"
with contextlib.suppress(FileNotFoundError):
with open(path, "r", encoding="utf8") as f:
with path.open("r", encoding="utf8") as f:
schema = json.load(f)
elif klass in ModuleInfo.MODULES:
info = self.get_module_info(klass, name)
Expand All @@ -752,12 +752,12 @@ def list_runners(self, distro: Optional[str] = None) -> List[RunnerInfo]:
will be returned.
"""
if not self._runners:
path = os.path.join(self.path, "runners")
names = filter(lambda f: os.path.isfile(f"{path}/{f}"),
os.listdir(path))
paths = map(lambda n: os.path.join(path, n), names)
mapped = map(RunnerInfo.from_path, paths)
self._runners = sorted(mapped, key=lambda r: (r.distro, r.version))
path = self.path / "runners"
paths = (p for p in path.iterdir()
if p.is_file())
runners = [RunnerInfo.from_path(p)
for p in paths]
self._runners = sorted(runners, key=lambda r: (r.distro, r.version))

runners = self._runners[:]
if distro:
Expand Down
5 changes: 5 additions & 0 deletions test/mod/test_osbuild.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,14 @@ def check_moduleinfo(self, version):
modules += [(klass, module) for module in mods]

self.assertTrue(modules)
assert "org.osbuild.noop" in [m[1] for m in modules]

for module in modules:
klass, name = module
assert isinstance(name, str)
assert not name.endswith(".meta.json")
assert os.path.dirname(name) == ""

try:
info = osbuild.meta.ModuleInfo.load(os.curdir, klass, name)
schema = osbuild.meta.Schema(info.get_schema(version), name)
Expand Down

0 comments on commit 5510605

Please sign in to comment.