diff --git a/src/python/jw/pkg/lib/PathFilter.py b/src/python/jw/pkg/lib/PathFilter.py index dd1148aa..6f0b43c4 100644 --- a/src/python/jw/pkg/lib/PathFilter.py +++ b/src/python/jw/pkg/lib/PathFilter.py @@ -54,7 +54,9 @@ class PathFilter(abc.ABC): f'Unsupported filter spec "{spec}": expected a "name:spec" ' 'form, where the name picks the filter class' ) - class_name = name.capitalize() + from .util import kebab_to_pascal + + class_name = kebab_to_pascal(name) module_name = 'jw.pkg.lib.pf.' + class_name filter_classes: LoadTypes[PathFilter] = LoadTypes( [module_name], diff --git a/src/python/jw/pkg/lib/util.py b/src/python/jw/pkg/lib/util.py index ab06270c..f8eb78bd 100644 --- a/src/python/jw/pkg/lib/util.py +++ b/src/python/jw/pkg/lib/util.py @@ -2,6 +2,7 @@ from __future__ import annotations import json import os +import re import sys from enum import Enum, auto @@ -38,6 +39,15 @@ def pretty_cmd(cmd: list[str] | None = None, wd: str | None = None) -> str: ret += f' in {wd}' return ret +def kebab_to_pascal(value: str) -> str: + return ''.join(part.capitalize() for part in value.split('-')) + +def pascal_to_kebab(value: str) -> str: + # Handles normal CamelCase / PascalCase, including many acronym cases + s1 = re.sub(r'(.)([A-Z][a-z]+)', r'\1-\2', value) + s2 = re.sub(r'([a-z0-9])([A-Z])', r'\1-\2', s1) + return s2.lower() + # See ExecContext.run() for what this function does async def run_cmd( *args: Any, diff --git a/test/unit/python/jw/pkg/lib/pf/Pkg/test.py b/test/unit/python/jw/pkg/lib/pf/Pkg/test.py index 6444e260..9260a7a7 100644 --- a/test/unit/python/jw/pkg/lib/pf/Pkg/test.py +++ b/test/unit/python/jw/pkg/lib/pf/Pkg/test.py @@ -6,6 +6,7 @@ from typing import TYPE_CHECKING, Any, override from jw.pkg.lib.FileContext import FileContext from jw.pkg.lib.Package import Package +from jw.pkg.lib.PathFilter import PathFilter from jw.pkg.lib.pf.Pkg import Pkg if TYPE_CHECKING: @@ -65,6 +66,13 @@ for definition in ('', f = Pkg(file_context = fc, spec = 'pkg:url=~example\\.com') assert repr(f) == 'filter:pkg:url=~example\\.com' +# -- PathFilter.create() resolves the name kebab-to-pascal -- + +f3 = PathFilter.create(spec = 'pkg:url=~example\\.com', file_context = fc) +assert isinstance(f3, Pkg) +assert f3.match_pkg(pkg) +assert not f3.match_pkg(pkg_other) + class _FakeDistro: def __init__(self, pkgs: list[tuple[str, str | None, list[str]]]) -> None: diff --git a/test/unit/python/jw/pkg/lib/util/test.py b/test/unit/python/jw/pkg/lib/util/test.py index 8771a905..aaf65cbc 100644 --- a/test/unit/python/jw/pkg/lib/util/test.py +++ b/test/unit/python/jw/pkg/lib/util/test.py @@ -4,12 +4,19 @@ import stat import tempfile from jw.pkg.lib.ec.Local import Local -from jw.pkg.lib.util import copy +from jw.pkg.lib.util import copy, kebab_to_pascal, pascal_to_kebab def uri(path: str) -> str: return 'local://localhost' + path async def run_tests() -> None: + # -- kebab <-> pascal: the name conversion behind the dynamic + # loading of filters and, later, file contexts + assert kebab_to_pascal('pkg') == 'Pkg' + assert kebab_to_pascal('pkg-secrets') == 'PkgSecrets' + assert pascal_to_kebab('Pkg') == 'pkg' + assert pascal_to_kebab('PkgSecrets') == 'pkg-secrets' + with tempfile.TemporaryDirectory() as tmp: # -- Source tree #