[PR jw/#5] enum_helper.py: Import the name conversion helpers from jw-pkg
All checks were successful
CI / Packaging - Kali Linux (push) Successful in 5m3s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 5m28s
CI / Packaging test (push) Successful in 0s

Merge pull request "enum_helper.py: Import the name conversion helpers from jw-pkg"

Upstream-repo: ssh://git@code.janware.com/janware/jw-python
Source-branch: jan/fix/20261118-enum-helper-py-import-the-name-conversion-helpers-from-jw-pkg
Target-branch: master
Reviewed-on: #5
This commit is contained in:
Jan Lindemann 2026-11-18 19:47:04 +01:00 • committed by janware Code
commit c983a77ef0
Signed by: Code Bot
GPG key ID: 66A5D2B295EE1224

View file

@ -1,22 +1,20 @@
from __future__ import annotations
import re
from typing import TYPE_CHECKING
from jw.pkg.lib.util import kebab_to_pascal, pascal_to_kebab
if TYPE_CHECKING:
from enum import Enum
from typing import TypeVar
EnumType = TypeVar("EnumType", bound = Enum)
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()
__all__ = [
"kebab_to_pascal",
"pascal_to_kebab",
"enum_from_kebab",
"enum_to_kebab",
]
def enum_from_kebab(enum_cls: type[EnumType], value: str) -> EnumType:
return enum_cls[kebab_to_pascal(value)]