1
0

fix: finish enums migration

confirm project works as expected comparing before-refactor one.
This commit is contained in:
2026-01-27 17:23:58 +08:00
parent 0419dc3939
commit f601782370
4 changed files with 16 additions and 18 deletions

View File

@@ -50,11 +50,13 @@ class RenderUtils:
REGEX_PY_TO_LITERAL_NUMBER: typing.ClassVar[re.Pattern] = re.compile("[ulUL]+$")
@staticmethod
def convert_to_python_number(numstr: str) -> str:
def to_py_num_literal(numstr: str) -> str:
"""
Convert accepted string into Python cupported format.
Convert given string into Python number literal style.
It actually just remove trail "UL".
Number literal declaration in C++ is different with Python.
Previous one allow adding suffix like "UL" to indicate its type, but Python don't allow this.
So this function actually just remove "UL" suffix.
This function is only served for Python code generation.
@@ -66,9 +68,9 @@ class RenderUtils:
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:
def underline_to_camel(entry_name: str) -> str:
"""
Try generate human readable name from enum entry name.
Convert given capital underlying name into camel name.
This function is only served for Python code generation.
@@ -84,12 +86,8 @@ class RenderUtils:
"""
# 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()
# then convert result into camel
return ' '.join(map(lambda s: s[0:1].upper() + s[1:].lower(), entry_name.split('_')))
class TemplateRender:
"""Render templates to code files"""

View File

@@ -1,8 +1,8 @@
{%- for benum in payload.iter_enums() %}
{%- if benum.get_enum_comment() is not none %}
{{ benum.get_enum_comment() | block_comment('/// ') }}
{%- endif %}
{% if benum.get_use_flags() %}[Flags]{%- endif %}
{%- endif %} {%- if benum.get_use_flags() %}
[Flags]{%- endif %}
public enum {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} uint {%- else -%} int {%- endif %} {
{%- for entry in benum.iter_entries() %}
{{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /// {{ entry.get_entry_comment() | line_comment }} {%- endif %}

View File

@@ -10,7 +10,7 @@ class EnumDocstring():
{% for benum in payload.iter_enums() %}
DOCSTRING_{{ benum.get_enum_name() }}: dict[{{ benum.get_enum_name() }}, EnumDocstring] = {
{%- for entry in benum.iter_entries() %}
{{ benum.get_enum_name() }}.{{ entry.get_entry_name() }}: EnumDocstring("{{ utils.extract_human_readable_entry_name(entry.get_entry_name()) }}", "{{ entry.get_entry_comment() | some_or_blank | escape_string }}"),
{{ benum.get_enum_name() }}.{{ entry.get_entry_name() }}: EnumDocstring("{{ utils.underline_to_camel(entry.get_entry_name()) }}", "{{ entry.get_entry_comment() | some_or_blank | escape_string }}"),
{%- endfor %}
}
{%- endfor %}

View File

@@ -8,7 +8,7 @@ class {{ benum.get_enum_name() }}(enum.IntEnum):
"""
{%- endif %}
{%- for entry in benum.iter_entries() %}
{{ entry.get_entry_name() }} = {% if entry.get_entry_value() is none -%} auto() {%- else -%} {{ utils.convert_to_python_number(entry.get_entry_value()) }} {%- endif %} {%- if entry.get_entry_comment() is not none %}
{{ entry.get_entry_name() }} = {% if entry.get_entry_value() is none -%} auto() {%- else -%} {{ utils.to_py_num_literal(entry.get_entry_value()) }} {%- endif %} {%- if entry.get_entry_comment() is not none %}
"""{{ entry.get_entry_comment() | line_comment }}""" {%- endif %}
{%- endfor %}
{%- endfor %}