lib.PackageFilter: Derive from PathFilter

This commit moves package-based filtering from the package level to the
path level, using the new path filter framework. Derive PackageFilter from
a new FileContextFilter base, which binds a PathFilter to the FileContext
it guards, and implements _access().

The filter's space is the file list of the packages it selects, taken from
the backing distro's pkg_files(). An address is allowed only if it names
one of those files.

The backing distro is taken from the file context, if the context carries
one, and is otherwise autodetected from the host the context executes on,
so the filter also works on a bare exec context. A context that is not an
exec context and carries no distro rejects path queries with an error.

The filter string now follows the loader syntax, pkg:url=~ regex, and
a bare url=~regex like the old url=~jw.dev form is rejected; update
JW_DEFAULT_PKG_FILTER values accordingly.

PackageFilter's match() is renamed match_pkg(), to make clear that it
matches packages, and Distro._select() calls the new name.

ExecApp.path_filter builds the filter through PathFilter.create() and
binds it to the exec context. CmdSelect passes its argument to the new
constructor, CmdCopy hands the app's filter to the copy, and the App distro
picks it up as the default package filter.

Extend the unit test to cover the space membership and the distro lookup.

Signed-off-by: Jan Lindemann <jan@janware.com>
Assisted-by: unsloth/Qwen3.8-27B-GGUF:Q4_K_M with pi.dev v1.5.0
This commit is contained in:
Jan Lindemann 2026-11-07 11:17:34 +01:00
commit f74dca5a45
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
7 changed files with 198 additions and 41 deletions

View file

@ -389,7 +389,7 @@ class App(Base):
self.__distro = await Distro.instantiate(
ec = self.exec_context,
id = self.args.distro_id,
default_pkg_filter = self.pkg_filter,
default_pkg_filter = self.path_filter,
)
@override

View file

@ -20,6 +20,8 @@ class CmdSelect(Cmd): # export
@override
async def _run(self, args: Namespace) -> None:
filter = PackageFilterString(args.filter) if args.filter else None
filter = PackageFilterString(
file_context = self.app.exec_context, spec = args.filter
) if args.filter else None
for p in await self.distro.select(filter = filter):
print(p.name)

View file

@ -67,4 +67,5 @@ class CmdCopy(Cmd): # export
mode = int(args.mode, 0) if args.mode is not None else None,
recursive = args.recursive,
parents = args.parents,
default_path_filter = self.app.path_filter,
)

View file

@ -381,7 +381,7 @@ class Distro(abc.ABC):
async def _select(self, names: Collection[str],
filter: PackageFilter) -> Iterable[Package]:
return [p for p in await self._select_by_name(names) if filter.match(p)]
return [p for p in await self._select_by_name(names) if filter.match_pkg(p)]
async def select(
self,

View file

@ -6,13 +6,13 @@ import sys
from typing import TYPE_CHECKING, Any, Literal, override
from .App import App as Base
from .PathFilter import PathFilter
if TYPE_CHECKING:
from argparse import ArgumentParser
from .ExecContext import ExecContext
from .PackageFilter import PackageFilter
class ExecApp(Base): # export
@ -21,7 +21,7 @@ class ExecApp(Base): # export
self.__opt_interactive: bool | None = None
self.__opt_verbose: bool | None = None
self.__exec_context: ExecContext | None = None
self.__pkg_filter: PackageFilter | None | Literal[False] = None
self.__path_filter: PathFilter | None | Literal[False] = None
@override
async def __aexit__(self, exc_type: Any, exc: Any, tb: Any) -> None:
@ -82,20 +82,20 @@ class ExecApp(Base): # export
return self.__opt_verbose
@property
def pkg_filter(self) -> PackageFilter | None:
if self.__pkg_filter is None:
def path_filter(self) -> PathFilter | None:
if self.__path_filter is None:
spec = self.args.pkg_filter
if spec is None:
spec = os.getenv('JW_DEFAULT_PKG_FILTER')
if spec is None:
self.__pkg_filter = False
self.__path_filter = False
else:
from .PackageFilter import PackageFilterString
self.__pkg_filter = PackageFilterString(spec)
if not self.__pkg_filter:
self.__path_filter = PathFilter.create(
spec = spec, file_context = self.exec_context
)
if not self.__path_filter:
return None
return self.__pkg_filter
return self.__path_filter
@property
def exec_context(self) -> ExecContext:

View file

@ -1,34 +1,81 @@
from __future__ import annotations
import abc
import os
import re
from typing import TYPE_CHECKING, override
from .PathFilter import PathFilter
if TYPE_CHECKING:
from typing import Any
from .Distro import Distro
from .FileContext import FileContext
from .Package import Package
class PackageFilter(abc.ABC):
class FileContextFilter(PathFilter):
def __init__(self, *, spec: str, file_context: FileContext, **kwargs: Any) -> None:
super().__init__(spec = spec, **kwargs)
self.__file_context = file_context
@property
def file_context(self) -> FileContext:
return self.__file_context
class PackageFilter(FileContextFilter):
def __init__(self, *, spec: str, file_context: FileContext, **kwargs: Any) -> None:
super().__init__(spec = spec, file_context = file_context, **kwargs)
self.__space: set[str] | None = None
self.___distro: Distro | None = None
async def distro(self) -> Distro:
if self.___distro is None:
from .Distro import Distro
from .ExecContext import ExecContext
if not isinstance(self.file_context, ExecContext):
raise Exception(
'Package-based filters need to be backed by '
f'distributions, but "{self}" isn\'t'
)
self.___distro = await Distro.instantiate(ec = self.file_context)
return self.___distro
@abc.abstractmethod
def _match(self, package: Package) -> bool:
def _match_pkg(self, package: Package) -> bool:
raise NotImplementedError()
def match(self, package: Package) -> bool:
return self._match(package)
def match_pkg(self, package: Package) -> bool:
return self._match_pkg(package)
@override
async def _access(self, path: str, mode: int = os.F_OK) -> bool:
if self.__space is None:
distro = await self.distro()
names = [p.name for p in await distro.select([], filter = self)]
self.__space = set(await distro.pkg_files(names))
return path in self.__space
class PackageFilterString(PackageFilter):
def __init__(self, definition: str) -> None:
def __init__(self, *, spec: str, file_context: FileContext) -> None:
super().__init__(spec = spec, file_context = file_context)
# -- Poor man's parsing for now
url_rx_str = re.sub(r'^\s*url\s*=~\s*', '', definition)
if url_rx_str == definition:
raise Exception(f'Unsupported filter string "{definition}"')
self.__definition = url_rx_str
m = re.match(r'^\s*pkg\s*:\s*(\S+?)\s*=~\s*(.+?)\s*$', spec)
if m is None:
raise Exception(f'Unsupported filter string "{spec}"')
self.__criterion = m.group(1)
self.__rx = re.compile(m.group(2))
@override
def _match(self, package: Package) -> bool:
def _match_pkg(self, package: Package) -> bool:
if self.__criterion != 'url':
return False
url = package.url
if url is None:
return False
return re.search(self.__definition, url) is not None
return self.__rx.search(url) is not None

View file

@ -72,7 +72,8 @@ class KvFile(Base):
self.__distro = await Distro.instantiate(
ec = self.__inner,
default_pkg_filter = (
PackageFilterString(pkg_filter_str) if pkg_filter_str else None
PackageFilterString(file_context = self, spec = pkg_filter_str)
if pkg_filter_str else None
),
)
ret: set[str] = set()