enum_helper: Add module

Add the module enum_helper, providing the following functions:

  def kebab_to_pascal(value: str) -> str
  def pascal_to_kebab(value: str) -> str
  def enum_from_kebab(enum_cls: type[EnumType], value: str) -> EnumType
  def enum_to_kebab(member: Enum) -> str

The intended use of these functions is to convert from configuration
strings to application enums and back.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-07-04 15:41:10 +02:00 • committed by janware DevOps
commit 5a8dfab722
Signed by: DevOps
SSH key fingerprint: SHA256:cZiw7ExG5q3KAVm7Jse3rGITowu0VjgUgNMPbifmY8g

View file

@ -0,0 +1,24 @@
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)