lib.Distro.paths(): Add method

After the introduction of path-based filters, accompany the existing
pkg_paths() method by a more fine-grained paths() method, taking a
PathFilter instead of a PackageFilter. It additionally takes a "pkg_names"
argument, which is then dispatched to the filter if it's a package filter.
The API isn't glorious, but works for now and is easily changeable in the
future: **kwargs comes to mind, or a PackageFilter which brings its own
package names.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-11-12 16:02:58 +01:00
commit dc9ef58c8b
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61

View file

@ -10,6 +10,8 @@ from typing import TYPE_CHECKING, Any
from .base import InputMode
from .log import ERR, INFO, WARNING, log
from .PathFilter import Verdict
from .pf.Pkg import PackageFilter
if TYPE_CHECKING:
from collections.abc import Collection
@ -20,7 +22,6 @@ if TYPE_CHECKING:
from .FlaggedFile import FlaggedFile
from .Package import Package
from .PathFilter import PathFilter
from .pf.Pkg import PackageFilter
class Distro(abc.ABC):
@ -519,10 +520,50 @@ class Distro(abc.ABC):
# compatibility with the single-package call
if isinstance(names, str):
names = [names]
if names is None and self.default_path_filter is not None:
elif names is None and self.default_path_filter is not None:
names = [p.name for p in await self.select()]
return await self._pkg_paths(names)
# -- paths
async def _paths(
self,
filter: PathFilter | None = None,
pkg_names: Collection[str] = [],
) -> Iterable[str]:
if filter and pkg_names and not isinstance(filter, PackageFilter):
raise Exception(
f'Tried to select files from {self} with non-package-filter {filter} '
f'and package list {", ".join(pkg_names)}'
)
if not filter:
filter = self.__default_path_filter
# -- pkg_names follows the select() convention: an empty
# list selects every package, which is listed in one
# whole-system query instead of by name
if isinstance(filter, PackageFilter):
pkg_names = [p.name for p in await self._select(pkg_names, filter)]
ret: list[str] = [
path for path in await self.pkg_paths(names = list(pkg_names))
]
elif pkg_names:
pkg_names = [p.name for p in await self._select_by_name(pkg_names)]
ret = [path for path in await self.pkg_paths(names = list(pkg_names))]
else:
# -- _pkg_paths() directly: pkg_paths() would re-resolve
# the default filter, which _paths() already did
ret = [path for path in await self._pkg_paths(None)]
if filter and not isinstance(filter, PackageFilter):
ret = [path for path in ret if await filter.run(path) == Verdict.ALLOW]
return ret
async def paths(
self,
filter: PathFilter | None = None,
pkg_names: Collection[str] = [],
) -> Iterable[str]:
return await self._paths(filter, pkg_names)
# -- verify
@abc.abstractmethod