lib.PathFilter / lib.pf: Add class + module

Add the abstract PathFilter class, designed to be attached to a FileContext
in later commits. This is the first commit of a series that generalizes the
JW_DEFAULT_PKG_FILTER / --pkg-filter framework into loadable path filters
that can be attached to any FileContext.

A filter answers an access check, _access(), with a bool. run() turns the
answer into a verdict: ALLOW when the check passes, otherwise ABORT or
IGNORE according to the filter's deny action, LOG or ABORT. The verdict is
handled by the filter's handle, a pair of per-verdict gates: with the ABORT
gate set, an ABORT verdict raises PermissionError; with the LOG gate set,
an IGNORE verdict is logged and returned. A verdict whose gate is not set
passes through unhandled.

Also add the lib.pf module that hosts the concrete filters. A filter spec
of the form foo:something is loaded by PathFilter.create() as the Foo class
from the jw.pkg.lib.pf.Foo module. A spec without the name prefix, or a
name that no module provides, is rejected with an error that names the
expected class and module.

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-04 18:20:49 +01:00
commit 013636fa38
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
2 changed files with 201 additions and 0 deletions

View file

@ -0,0 +1,95 @@
from __future__ import annotations
import abc
import os
from enum import Enum, Flag, auto
from typing import TYPE_CHECKING, override
from .log import WARNING, get_caller_pos, log
from .Types import LoadTypes
if TYPE_CHECKING:
from typing import Any
class DenyAction(Enum):
LOG = auto()
ABORT = auto()
class Handle(Flag):
LOG = auto()
ABORT = auto()
class Verdict(Enum):
ALLOW = auto()
IGNORE = auto()
ABORT = auto()
class PathFilter(abc.ABC):
def __init__(
self,
*,
spec: str,
deny_action: DenyAction = DenyAction.LOG,
handle: Handle = Handle.LOG | Handle.ABORT,
) -> None:
self.__spec = spec
self.__deny_action = deny_action
self.__handle = handle
@override
def __repr__(self) -> str:
return f'filter:{self.__spec}'
@property
def deny_action(self) -> DenyAction:
return self.__deny_action
@classmethod
def create(cls, *, spec: str, **kwargs: Any) -> PathFilter:
name, sep, _ = spec.partition(':')
if not sep:
raise Exception(
f'Unsupported filter spec "{spec}": expected a "name:spec" '
'form, where the name picks the filter class'
)
class_name = name.capitalize()
module_name = 'jw.pkg.lib.pf.' + class_name
filter_classes: LoadTypes[PathFilter] = LoadTypes(
[module_name],
type_name_filter = class_name,
type_filter = [PathFilter],
)
try:
filter_class = next(iter(filter_classes))
except (StopIteration, ModuleNotFoundError) as e:
raise Exception(
f'No PathFilter class "{class_name}" in module {module_name}'
) from e
return filter_class(spec = spec, **kwargs)
@abc.abstractmethod
async def _access(self, path: str, mode: int = os.F_OK) -> bool:
raise NotImplementedError()
async def run(
self,
path: str,
mode: int = os.F_OK,
msg: str | None = None,
caller: tuple[str, str, int] | None = None,
) -> Verdict:
if await self._access(path, mode):
return Verdict.ALLOW
ret = Verdict.ABORT if self.__deny_action == DenyAction.ABORT \
else Verdict.IGNORE
if not (self.__handle & (Handle.LOG | Handle.ABORT)):
return ret
if msg is None:
msg = f'"{path}" is filtered'
if self.__handle & Handle.ABORT and ret == Verdict.ABORT:
raise PermissionError(msg + ', aborting')
if self.__handle & Handle.LOG and ret == Verdict.IGNORE:
log(WARNING, msg + ', ignoring', caller = caller or get_caller_pos())
return ret

View file

@ -0,0 +1,4 @@
TOPDIR = ../../../../../..
include $(TOPDIR)/make/proj.mk
include $(JWBDIR)/make/py-mod.mk