mirror of
https://github.com/OpenSPG/openspg.git
synced 2025-07-15 04:56:23 +00:00
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright 2023 Ant Group CO., Ltd.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
|
# in compliance with the License. You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software distributed under the License
|
|
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
|
|
# or implied.
|
|
|
|
import re
|
|
import string
|
|
from os import PathLike
|
|
from pathlib import Path
|
|
from typing import Any, Union
|
|
|
|
|
|
def render_templatefile(path: Union[str, PathLike], **kwargs: Any) -> None:
|
|
path_obj = Path(path)
|
|
raw = path_obj.read_text("utf8")
|
|
|
|
content = string.Template(raw).substitute(**kwargs)
|
|
|
|
render_path = path_obj.with_suffix("") if path_obj.suffix == ".tmpl" else path_obj
|
|
|
|
if path_obj.suffix == ".tmpl":
|
|
path_obj.rename(render_path)
|
|
|
|
render_path.write_text(content, "utf8")
|
|
|
|
|
|
CAMELCASE_INVALID_CHARS = re.compile(r"[^a-zA-Z\d]")
|
|
|
|
|
|
def string_camelcase(string: str) -> str:
|
|
"""Convert a word to its CamelCase version and remove invalid chars
|
|
|
|
>>> string_camelcase('lost-pound')
|
|
'LostPound'
|
|
|
|
>>> string_camelcase('missing_images')
|
|
'MissingImages'
|
|
|
|
"""
|
|
return CAMELCASE_INVALID_CHARS.sub("", string.title())
|