mirror of
https://github.com/bec-project/bec.git
synced 2026-06-01 15:58:31 +02:00
feat: add helper functions for plugin template
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING, TypeVar
|
||||
|
||||
from jinja2.ext import Extension
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover
|
||||
from copier import YieldEnvironment
|
||||
|
||||
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
def _snake_to_pascal(value: str) -> str:
|
||||
return "".join(map(str.capitalize, value.split("_")))
|
||||
|
||||
|
||||
def _snake_to_camel(value: str) -> str:
|
||||
res = _snake_to_pascal(value)
|
||||
if len(res) >= 1:
|
||||
res = res[0].lower() + res[1:]
|
||||
return res
|
||||
|
||||
|
||||
def _debug(value: _T) -> _T:
|
||||
print(value)
|
||||
return value
|
||||
|
||||
|
||||
class CopierFilters(Extension):
|
||||
identifier = "BEC copier jinja filters"
|
||||
|
||||
def __init__(self, env: "YieldEnvironment") -> None:
|
||||
self._env = env
|
||||
|
||||
self._env.filters["snake_to_pascal"] = _snake_to_pascal
|
||||
self._env.filters["snake_to_camel"] = _snake_to_camel
|
||||
self._env.filters["debug"] = _debug
|
||||
@@ -48,6 +48,7 @@ dev = [
|
||||
"pytest-random-order~=1.1",
|
||||
"pytest-timeout~=2.2",
|
||||
"pytest-redis~=3.0",
|
||||
"Jinja2~=3.1",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
from bec_lib.utils.copier_jinja_filters import _snake_to_camel, _snake_to_pascal
|
||||
|
||||
# pylint: disable=protected-access
|
||||
|
||||
|
||||
def test_camel_case():
|
||||
assert _snake_to_camel("test_string") == "testString"
|
||||
assert _snake_to_camel("test_string_2") == "testString2"
|
||||
assert _snake_to_camel("test_strIng_wITH_CAPS") == "testStringWithCaps"
|
||||
assert _snake_to_camel("TestStringAlreadyPascal") == "teststringalreadypascal"
|
||||
|
||||
|
||||
def test_pascal_case():
|
||||
assert _snake_to_pascal("test_string") == "TestString"
|
||||
assert _snake_to_pascal("test_string_2") == "TestString2"
|
||||
assert _snake_to_pascal("test_strIng_wITH_CAPS") == "TestStringWithCaps"
|
||||
assert _snake_to_pascal("testStringAlreadyCamel") == "Teststringalreadycamel"
|
||||
assert _snake_to_pascal("t_e_s_t") == "TEST"
|
||||
Reference in New Issue
Block a user