jw-python/src/python/jw/util/enum_helper.py
Jan Lindemann 2d847b4ccd
jw.util, jwutils: Follow jw-pkg's move to isort
"make format", as defined by jw-pkg, now sorts the order or imports,
too. So do that and commit.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-08-12 23:10:13 +00:00

25 lines
753 B
Python

from __future__ import annotations
import re
from typing import TYPE_CHECKING
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()
def enum_from_kebab(enum_cls: type[EnumType], value: str) -> EnumType:
return enum_cls[kebab_to_pascal(value)]
def enum_to_kebab(member: Enum) -> str:
return pascal_to_kebab(member.name)