lib.util: Add kebab_to_pascal() and pascal_to_kebab()
PathFilter.create() currently instantiates individual filters in a match - case over the filter URI's protocol. The plan is to instantiate filters without match / case based on a protocol naming scheme only, so we better introduce one at this point. Adopt jw-python's kebab_to_pascal() and pascal_to_kebab() functions as a foundation for a future naming convention. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
5ac36ec271
commit
372356a8df
4 changed files with 29 additions and 2 deletions
|
|
@ -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],
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
#
|
||||
|
|
|
|||
Loading…
Reference in a new issue