Skip to content

Commit

Permalink
Enum generation working
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Aug 18, 2024
1 parent a744ac4 commit 8255103
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 47 deletions.
2 changes: 0 additions & 2 deletions ifgen/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

# internal
from ifgen.common.python import create_python_common
from ifgen.generation.interface import GenerateTask
from ifgen.generation.test import unit_test_boilerplate

Expand All @@ -12,7 +11,6 @@ def create_common_test(task: GenerateTask) -> None:
"""Create a unit test for the enum string-conversion methods."""

if task.is_python:
create_python_common(task)
return

with unit_test_boilerplate(task, declare_namespace=True) as writer:
Expand Down
30 changes: 0 additions & 30 deletions ifgen/common/python.py

This file was deleted.

65 changes: 62 additions & 3 deletions ifgen/enum/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,55 @@
"""

# third-party
from runtimepy.enum.registry import DEFAULT_ENUM_PRIMITIVE
from vcorelib.io import IndentedFileWriter

# internal
from ifgen.generation.interface import GenerateTask
from ifgen.generation.python import python_class, python_imports
from ifgen.generation.python import (
python_class,
python_function,
python_imports,
)


def strip_t_suffix(data: str) -> str:
"""Strip a possible '_t' suffix from a string."""
return data.replace("_t", "") if data.endswith("_t") else data


def uses_auto(task: GenerateTask) -> bool:
"""
Determine if a generation task will require 'auto' from the enum module.
"""

result = False

for value in task.instance.get("enum", {}).values():
if not value or "value" not in value:
result = True
break

return result


def to_enum_name(data: str) -> str:
"""Ensure a candidate name string is suitable as an enumeration name."""
return data.upper()


def python_enum_header(task: GenerateTask, writer: IndentedFileWriter) -> None:
"""Create a Python module for an enumeration."""

built_in = {}
if uses_auto(task):
built_in["enum"] = ["auto"]

# Write imports.
python_imports(
writer, third_party={"runtimepy.enum.registry": ["RuntimeIntEnum"]}
writer,
third_party={"runtimepy.enum.registry": ["RuntimeIntEnum"]},
built_in=built_in,
)

with python_class(
Expand All @@ -25,4 +61,27 @@ def python_enum_header(task: GenerateTask, writer: IndentedFileWriter) -> None:
parents=["RuntimeIntEnum"],
final_empty=0,
):
writer.write("# register members")
underlying = strip_t_suffix(task.instance["underlying"])

if underlying != DEFAULT_ENUM_PRIMITIVE:
with python_function(
writer,
"primitive",
"The underlying primitive type for this runtime enumeration.",
params="cls",
return_type="str",
final_empty=1,
decorators=["classmethod"],
):
writer.write(f'return "{underlying}"')

for enum, value in task.instance.get("enum", {}).items():
final = "auto()"
if value:
final = value.get("value", final)

line = f"{to_enum_name(enum)} = {final}"
if value and "description" in value:
line += f" # {value['description']}"

writer.write(line)
9 changes: 8 additions & 1 deletion ifgen/generation/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# built-in
from contextlib import contextmanager
from typing import Iterator
from typing import Iterable, Iterator

# third-party
from vcorelib.io import IndentedFileWriter
Expand All @@ -27,11 +27,14 @@ def write_imports(

def python_imports(
writer: IndentedFileWriter,
built_in: PythonImports = None,
third_party: PythonImports = None,
final_empty: int = 2,
) -> None:
"""Write Python-module imports."""

if built_in:
write_imports(writer, "built-in", built_in)
if third_party:
write_imports(writer, "third-party", third_party)

Expand Down Expand Up @@ -70,9 +73,13 @@ def python_function(
params: str = "",
return_type: str = "None",
final_empty: int = 2,
decorators: Iterable[str] = None,
) -> Iterator[None]:
"""Write a Python function."""

if decorators:
for decorator in decorators:
writer.write("@" + decorator)
writer.write(f"def {name}({params}) -> {return_type}:")

with writer.indented():
Expand Down
12 changes: 1 addition & 11 deletions ifgen/struct/header.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ifgen.generation.interface import GenerateTask
from ifgen.struct.methods import struct_methods
from ifgen.struct.methods.fields import bit_fields
from ifgen.struct.python import python_struct_header
from ifgen.struct.stream import struct_stream_methods

FieldConfig = Dict[str, Union[int, str]]
Expand Down Expand Up @@ -190,17 +191,6 @@ def cpp_struct_header(task: GenerateTask, writer: IndentedFileWriter) -> None:
struct_instance(task, writer, instance)


def python_struct_header(
task: GenerateTask, writer: IndentedFileWriter
) -> None:
"""Create a Python module for a struct."""

writer.empty()

del task
writer.write("# todo")


def struct_header(task: GenerateTask, writer: IndentedFileWriter) -> None:
"""Create the contents of a struct header file."""

Expand Down

0 comments on commit 8255103

Please sign in to comment.