Skip to content

Commit

Permalink
0.2.0 - Enum generation functional
Browse files Browse the repository at this point in the history
  • Loading branch information
vkottler committed Aug 13, 2023
1 parent a877d39 commit 09b2e81
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
- run: |
mk python-release owner=vkottler \
repo=ifgen version=0.1.3
repo=ifgen version=0.2.0
if: |
matrix.python-version == '3.11'
&& matrix.system == 'ubuntu-latest'
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
=====================================
generator=datazen
version=3.1.3
hash=2f24a731d6db776ce7018fe92edcd875
hash=5d44eb9869d4f87218fafc86da0ac5a1
=====================================
-->

# ifgen ([0.1.3](https://pypi.org/project/ifgen/))
# ifgen ([0.2.0](https://pypi.org/project/ifgen/))

[![python](https://img.shields.io/pypi/pyversions/ifgen.svg)](https://pypi.org/project/ifgen/)
![Build Status](https://github.com/vkottler/ifgen/workflows/Python%20Package/badge.svg)
Expand Down
4 changes: 2 additions & 2 deletions ifgen/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =====================================
# generator=datazen
# version=3.1.3
# hash=f6fabcab1f9396a648d63c6e71143370
# hash=c5b0f162b1edf1df565f2201af7b1e0f
# =====================================

"""
Expand All @@ -10,4 +10,4 @@

DESCRIPTION = "An interface generator for distributed computing."
PKG_NAME = "ifgen"
VERSION = "0.1.3"
VERSION = "0.2.0"
31 changes: 31 additions & 0 deletions ifgen/data/schemas/Enum.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
---
includes:
- has_description.yaml

required: [enum]

properties:
enum:
type: object
additionalProperties: false
patternProperties:
"^[a-zA-Z0-9-_.]+$":
type: [object, "null"]
additionalProperties: false
required: []
properties:
value:
type: [string, number]
description:
type: string

underlying:
type: string
default: uint8_t
enum:
- int8_t
- int16_t
- int32_t
- int64_t

- uint8_t
- uint16_t
- uint32_t
- uint64_t
30 changes: 28 additions & 2 deletions ifgen/enum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@
A module implementing interfaces for enum-file generation.
"""

# built-in
from typing import Dict, Optional, Union

# internal
from ifgen.generation.interface import GenerateTask

EnumConfig = Optional[Dict[str, Union[int, str]]]


def enum_line(name: str, value: EnumConfig) -> str:
"""Build a string representing a line in an enumeration."""

line = name

if value and value.get("value"):
line += f" = {value['value']}"

if value and value.get("description"):
line += f" /*!< {value['description']} */"

return line


def create_enum(task: GenerateTask) -> None:
"""Create a header file based on an enum definition."""

with task.boilerplate() as writer:
assert writer
with task.boilerplate(includes=["<cstdint>"]) as writer:
writer.write(f"enum class {task.name} : {task.instance['underlying']}")
with writer.scope(suffix=";"):
writer.join(
*(
enum_line(enum, value)
for enum, value in task.instance.get("enum", {}).items()
)
)
12 changes: 9 additions & 3 deletions ifgen/generation/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from contextlib import contextmanager
from json import dumps
from pathlib import Path
from typing import Any, Callable, Dict, Iterator, NamedTuple
from typing import Any, Callable, Dict, Iterable, Iterator, NamedTuple

# third-party
from vcorelib.io import IndentedFileWriter
Expand Down Expand Up @@ -36,7 +36,9 @@ def command(self, command: str, data: str = "", space: str = " ") -> str:
)

@contextmanager
def boilerplate(self) -> Iterator[IndentedFileWriter]:
def boilerplate(
self, includes: Iterable[str] = None
) -> Iterator[IndentedFileWriter]:
"""
Create standard generation boilerplate and yield the file writer to
use for writing the remaining content.
Expand All @@ -54,7 +56,11 @@ def boilerplate(self) -> Iterator[IndentedFileWriter]:
writer.write(dumps(self.instance, indent=2))

writer.write("#pragma once")
writer.empty()

with writer.padding():
# Write any includes.
for include in sorted(includes if includes else []):
writer.write(f"#include {include}")

# Write namespace.
namespace = "::".join(self.config["namespace"])
Expand Down
4 changes: 2 additions & 2 deletions local/variables/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
major: 0
minor: 1
patch: 3
minor: 2
patch: 0
entry: ig
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"

[project]
name = "ifgen"
version = "0.1.3"
version = "0.2.0"
description = "An interface generator for distributed computing."
readme = "README.md"
requires-python = ">=3.8"
Expand Down
9 changes: 9 additions & 0 deletions tests/data/valid/scenarios/sample/ifgen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ structs:
enums:
Enum1:
description: "Sample enum 1."
underlying: uint16_t
enum:
A:
B: {description: A sample value.}
C: {value: 1000}

Enum2:
description: "Sample enum 2."
enum:
red: {value: 1, description: A sample value.}
green:
blue: {value: 5}

0 comments on commit 09b2e81

Please sign in to comment.