fix: finish enums migration
This commit is contained in:
@@ -1,13 +1,99 @@
|
||||
import jinja2
|
||||
import jinja2.filters
|
||||
import re
|
||||
import typing
|
||||
from json_loader import BEnumCollection, BEnum
|
||||
from utils import CKParts
|
||||
import utils
|
||||
|
||||
class RenderUtils:
|
||||
pass
|
||||
"""Possible used functions for jinja when rendering templates"""
|
||||
|
||||
TRANTABLE_ESCAPE_STRING: typing.ClassVar[dict[int, str]] = str.maketrans({
|
||||
"\\": "\\\\",
|
||||
"\t": "\\t",
|
||||
"\b": "\\b",
|
||||
"\n": "\\n",
|
||||
"\r": "\\r",
|
||||
"\f": "\\f",
|
||||
"\"": "\\\"",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def escape_string(strl: str) -> str:
|
||||
"""
|
||||
Escape string
|
||||
|
||||
Escape all characters which are invalid in string quote.
|
||||
|
||||
:param strl: The string need to be escaped.
|
||||
:return: The escaped string.
|
||||
"""
|
||||
return strl.translate(RenderUtils.TRANTABLE_ESCAPE_STRING)
|
||||
|
||||
TRANTABLE_REMOVE_EOL: typing.ClassVar[dict[int, str]] = str.maketrans({
|
||||
"\n": "",
|
||||
"\r": "",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def remove_eol(strl: str) -> str:
|
||||
"""
|
||||
Remove EOL of given string.
|
||||
|
||||
When rendering code, adding line comment is a common case.
|
||||
However, comment may have EOL.
|
||||
So when adding this to line comment, we need to remove all EOL.
|
||||
"""
|
||||
return strl.translate(RenderUtils.TRANTABLE_REMOVE_EOL)
|
||||
|
||||
REGEX_PY_TO_LITERAL_NUMBER: typing.ClassVar[re.Pattern] = re.compile("[ulUL]+$")
|
||||
|
||||
@staticmethod
|
||||
def convert_to_python_number(numstr: str) -> str:
|
||||
"""
|
||||
Convert accepted string into Python cupported format.
|
||||
|
||||
It actually just remove trail "UL".
|
||||
|
||||
This function is only served for Python code generation.
|
||||
|
||||
:param numstr: The captured number.
|
||||
:return: The Python style number string.
|
||||
"""
|
||||
return RenderUtils.REGEX_PY_TO_LITERAL_NUMBER.sub("", numstr, 1)
|
||||
|
||||
REGEX_PY_EXT_HUMANRDABLE_ENTRY_NAME: typing.ClassVar[re.Pattern] = re.compile("^[a-zA-Z0-9]+_")
|
||||
|
||||
@staticmethod
|
||||
def extract_human_readable_entry_name(entry_name: str) -> str:
|
||||
"""
|
||||
Try generate human readable name from enum entry name.
|
||||
|
||||
This function is only served for Python code generation.
|
||||
|
||||
As you noticed, almost entries of CK enums are fully capital and splitted by
|
||||
underline. This is really not good for human reading, especially those who
|
||||
are not programmer. So this function will try give these programmer-oriented
|
||||
entry name a human readable name as its display name. However, this extract
|
||||
method is not perfect. It simply do some split and replacement so the
|
||||
generated content may still not good for reader.
|
||||
|
||||
:param entry_name: The name of enum entry
|
||||
:return: A human readable entry name. No guaranteen that return value is must human readable.
|
||||
"""
|
||||
# remove first part (any content before underline '_')
|
||||
entry_name = RenderUtils.REGEX_PY_EXT_HUMANRDABLE_ENTRY_NAME.sub("", entry_name, 1)
|
||||
|
||||
# lower all chars except first char
|
||||
if len(entry_name) < 1:
|
||||
return entry_name
|
||||
else:
|
||||
return entry_name[0:1] + entry_name[1:].lower()
|
||||
|
||||
class TemplateRender:
|
||||
"""Render templates to code files"""
|
||||
|
||||
__loader: jinja2.BaseLoader
|
||||
__environment: jinja2.Environment
|
||||
|
||||
@@ -15,6 +101,12 @@ class TemplateRender:
|
||||
self.__loader = jinja2.FileSystemLoader(utils.get_template_directory())
|
||||
self.__environment = jinja2.Environment(loader=self.__loader)
|
||||
|
||||
# prepare filters
|
||||
self.__environment.filters['some_or_blank'] = lambda s: "" if s is None else s
|
||||
self.__environment.filters['escape_string'] = lambda s: RenderUtils.escape_string(s)
|
||||
self.__environment.filters['block_comment'] = lambda s, fmt: jinja2.filters.do_indent(s, fmt, True, True)
|
||||
self.__environment.filters['line_comment'] = lambda s: RenderUtils.remove_eol(s)
|
||||
|
||||
def __render(self, template_name: str, dest_filename: str, payload: BEnumCollection, extra: dict[str, typing.Any] = {}) -> None:
|
||||
# prepare template argument
|
||||
template_argument: dict[str, typing.Any] = {
|
||||
|
||||
Reference in New Issue
Block a user