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] = {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{%- set benum = first(payload.iter_enums()) %}
|
||||
{%- set benum = payload.iter_enums() | first %}
|
||||
const CkErrorReflectionArray CKERROR {
|
||||
{%- for entry in benum.iter_entries() %}
|
||||
{ LibCmo::CK2::CKERROR::{{ entry.get_entry_name() }}, { u8"{{ entry.get_entry_name() }}", u8"{{- entry.get_entry_comment() }}" } },
|
||||
{ LibCmo::CK2::CKERROR::{{ entry.get_entry_name() }}, { u8"{{ entry.get_entry_name() }}", u8"{{- entry.get_entry_comment() | some_or_blank | escape_string }}" } },
|
||||
{%- endfor %}
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{%- set benum = first(payload.iter_enums()) %}
|
||||
{%- set benum = payload.iter_enums() | first %}
|
||||
const CkClassidReflectionArray CK_CLASSID {
|
||||
{%- for entry in benum.iter_entries() %}
|
||||
{ LibCmo::CK2::CK_CLASSID::{{ entry.get_entry_name() }}, { { {% for item in entry.iter_hierarchy(benum) %} u8"{{ item.get_enum_name() }}" {%- if not loop.last -%}, {%- endif %} {%- endfor %} } } },
|
||||
{ LibCmo::CK2::CK_CLASSID::{{ entry.get_entry_name() }}, { { {%- for item in entry.iter_hierarchy(benum) %} u8"{{ item.get_entry_name() }}" {%- if not loop.last -%}, {%- endif %} {%- endfor %} } } },
|
||||
{%- endfor %}
|
||||
};
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
{%- for benum in payload.iter_enums() %}
|
||||
{%- if benum.get_enum_comment() is not none %}
|
||||
/**
|
||||
{{ benum.get_enum_comment() }}
|
||||
*/
|
||||
{{ benum.get_enum_comment() | block_comment('/// ') }}
|
||||
{%- 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() }} */ {%- endif %}
|
||||
{{ 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 %}
|
||||
{%- endfor %}
|
||||
};
|
||||
{%- endfor %}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
{%- for benum in payload.iter_enums() %}
|
||||
public static readonly System.Collections.Generic.Dictionary<{{ benum.get_enum_name() }}, string> {{ benum.get_enum_name() }} = new System.Collections.Generic.Dictionary<{{ benum.get_enum_name() }}, string>() {
|
||||
using System.Collections.Generic;
|
||||
|
||||
{% for benum in payload.iter_enums() %}
|
||||
public static readonly Dictionary<{{ benum.get_enum_name() }}, string> {{ benum.get_enum_name() }} = new Dictionary<{{ benum.get_enum_name() }}, string>() {
|
||||
{%- for entry in benum.iter_entries() %}
|
||||
{ {{ benum.get_enum_name() }}.{{ entry.get_entry_name() }}, "{{ entry.get_entry_name() }}" },
|
||||
{%- endfor %}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class EnumDocstring():
|
||||
display_name: str
|
||||
"""The name of this enum entry."""
|
||||
description: str
|
||||
"""The description of this enum entry."""
|
||||
|
||||
{% 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 }}"),
|
||||
{%- endfor %}
|
||||
}
|
||||
{%- endfor %}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
{%- for benum in payload.iter_enums() %}
|
||||
{%- if benum.get_enum_comment() is not none %}
|
||||
/**
|
||||
{{ benum.get_enum_comment() }}
|
||||
{{ benum.get_enum_comment() | block_comment(' * ') }}
|
||||
*/
|
||||
{%- endif %}
|
||||
enum class {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} CKDWORD {%- else -%} CKINT {%- 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() }} */ {%- endif %}
|
||||
{{ 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 %}
|
||||
{%- endfor %}
|
||||
};
|
||||
{%- endfor %}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import enum
|
||||
|
||||
{% for benum in payload.iter_enums() %}
|
||||
{%- if benum.get_enum_comment() is not none %}
|
||||
/**
|
||||
{{ benum.get_enum_comment() }}
|
||||
*/
|
||||
{%- endif %}
|
||||
class {{ benum.get_enum_name() }}(enum.IntEnum):
|
||||
{%- if benum.get_enum_comment() is not none %}
|
||||
"""
|
||||
{{ benum.get_enum_comment() | block_comment('') }}
|
||||
"""
|
||||
{%- endif %}
|
||||
{%- for entry in benum.iter_entries() %}
|
||||
{{ entry.get_entry_name() }} = {% if entry.get_entry_value() is none -%} auto() {%- else -%} {{ entry.get_entry_value() }} {%- endif %} {%- if entry.get_entry_comment() is not none %}
|
||||
"""{{ entry.get_entry_comment() }}""" {%- endif %}
|
||||
{{ 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_comment() | line_comment }}""" {%- endif %}
|
||||
{%- endfor %}
|
||||
{%- endfor %}
|
||||
|
||||
Reference in New Issue
Block a user