From 663d0f9b99b1edb1f220f007bc6cdb3ad0dd52e4 Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Thu, 10 Aug 2023 22:27:25 -0500 Subject: [PATCH 1/2] Initial file writing --- .github/workflows/python-package.yml | 2 +- README.md | 4 ++-- ifgen/__init__.py | 4 ++-- ifgen/commands/gen.py | 2 +- ifgen/requirements.txt | 2 +- ifgen/struct/__init__.py | 14 +++++++++++--- local/configs/package.yaml | 2 +- local/variables/package.yaml | 2 +- pyproject.toml | 2 +- tests/data/valid/scenarios/sample/.gitignore | 1 + 10 files changed, 22 insertions(+), 13 deletions(-) create mode 100644 tests/data/valid/scenarios/sample/.gitignore diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml index 5e602ec..084d43b 100644 --- a/.github/workflows/python-package.yml +++ b/.github/workflows/python-package.yml @@ -70,7 +70,7 @@ jobs: - run: | mk python-release owner=vkottler \ - repo=ifgen version=0.1.2 + repo=ifgen version=0.1.3 if: | matrix.python-version == '3.11' && matrix.system == 'ubuntu-latest' diff --git a/README.md b/README.md index f3a066f..c7f801d 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,11 @@ ===================================== generator=datazen version=3.1.3 - hash=0cf37843aeaee823cd6a9128ae0b04d7 + hash=2f24a731d6db776ce7018fe92edcd875 ===================================== --> -# ifgen ([0.1.2](https://pypi.org/project/ifgen/)) +# ifgen ([0.1.3](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) diff --git a/ifgen/__init__.py b/ifgen/__init__.py index c30e465..f4d36ba 100644 --- a/ifgen/__init__.py +++ b/ifgen/__init__.py @@ -1,7 +1,7 @@ # ===================================== # generator=datazen # version=3.1.3 -# hash=855ef60419d3d95b426724fc9c84173a +# hash=f6fabcab1f9396a648d63c6e71143370 # ===================================== """ @@ -10,4 +10,4 @@ DESCRIPTION = "An interface generator for distributed computing." PKG_NAME = "ifgen" -VERSION = "0.1.2" +VERSION = "0.1.3" diff --git a/ifgen/commands/gen.py b/ifgen/commands/gen.py index ec1484a..3d7fb47 100644 --- a/ifgen/commands/gen.py +++ b/ifgen/commands/gen.py @@ -37,7 +37,7 @@ def gen_cmd(args: _Namespace) -> int: root, normalize(*config.data["output_dir"]) ) - generate_structs(output, config) + generate_structs(root, output, config) return 0 diff --git a/ifgen/requirements.txt b/ifgen/requirements.txt index b60c6cc..b20bc48 100644 --- a/ifgen/requirements.txt +++ b/ifgen/requirements.txt @@ -1 +1 @@ -vcorelib +vcorelib>=2.4.5 diff --git a/ifgen/struct/__init__.py b/ifgen/struct/__init__.py index c242db0..192dc7e 100644 --- a/ifgen/struct/__init__.py +++ b/ifgen/struct/__init__.py @@ -7,6 +7,9 @@ from pathlib import Path from typing import Any, Dict, NamedTuple +# third-party +from vcorelib.io import IndentedFileWriter + # internal from ifgen.config import Config @@ -16,6 +19,7 @@ class GenerateStructTask(NamedTuple): """Parameters necessary for struct generation.""" + root: Path path: Path struct: StructConfig @@ -23,18 +27,22 @@ class GenerateStructTask(NamedTuple): def create_struct(task: GenerateStructTask) -> None: """Create a header file based on a struct definition.""" - print(task) + with IndentedFileWriter.from_path(task.path, per_indent=4) as writer: + with writer.javadoc(): + writer.write("Test.") -def generate_structs(output: Path, config: Config) -> None: +def generate_structs(root: Path, output: Path, config: Config) -> None: """Generate struct files.""" + output.mkdir(parents=True, exist_ok=True) + pool = Pool() # pylint: disable=consider-using-with try: pool.map( create_struct, ( - GenerateStructTask(output.joinpath(f"{name}.h"), data) + GenerateStructTask(root, output.joinpath(f"{name}.h"), data) for name, data in config.data["structs"].items() ), ) diff --git a/local/configs/package.yaml b/local/configs/package.yaml index be1535b..b0f8620 100644 --- a/local/configs/package.yaml +++ b/local/configs/package.yaml @@ -5,7 +5,7 @@ description: An interface generator for distributed computing. entry: {{entry}} requirements: - - vcorelib + - vcorelib>=2.4.5 dev_requirements: - pytest-cov diff --git a/local/variables/package.yaml b/local/variables/package.yaml index e5b70e1..4526dfd 100644 --- a/local/variables/package.yaml +++ b/local/variables/package.yaml @@ -1,5 +1,5 @@ --- major: 0 minor: 1 -patch: 2 +patch: 3 entry: ig diff --git a/pyproject.toml b/pyproject.toml index a707f03..4808a0e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__" [project] name = "ifgen" -version = "0.1.2" +version = "0.1.3" description = "An interface generator for distributed computing." readme = "README.md" requires-python = ">=3.8" diff --git a/tests/data/valid/scenarios/sample/.gitignore b/tests/data/valid/scenarios/sample/.gitignore new file mode 100644 index 0000000..85de9cf --- /dev/null +++ b/tests/data/valid/scenarios/sample/.gitignore @@ -0,0 +1 @@ +src From 613345e7e04b866705e66583044cf09aadbe3ecd Mon Sep 17 00:00:00 2001 From: Vaughn Kottler Date: Fri, 11 Aug 2023 01:21:47 -0500 Subject: [PATCH 2/2] 0.1.3 - Initial file writing --- ifgen/data/schemas/Config.yaml | 10 ++++++ ifgen/requirements.txt | 2 +- ifgen/struct/__init__.py | 36 ++++++++++++++++++-- local/configs/package.yaml | 2 +- tests/data/valid/scenarios/sample/ifgen.yaml | 2 ++ 5 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ifgen/data/schemas/Config.yaml b/ifgen/data/schemas/Config.yaml index 264688f..4908333 100644 --- a/ifgen/data/schemas/Config.yaml +++ b/ifgen/data/schemas/Config.yaml @@ -10,6 +10,16 @@ properties: items: type: string + command: + type: string + default: "\\" + + namespace: + type: array + default: [Coral] + items: + type: string + structs: type: object additionalProperties: false diff --git a/ifgen/requirements.txt b/ifgen/requirements.txt index b20bc48..7fa53e0 100644 --- a/ifgen/requirements.txt +++ b/ifgen/requirements.txt @@ -1 +1 @@ -vcorelib>=2.4.5 +vcorelib>=2.4.6 diff --git a/ifgen/struct/__init__.py b/ifgen/struct/__init__.py index 192dc7e..836c3c4 100644 --- a/ifgen/struct/__init__.py +++ b/ifgen/struct/__init__.py @@ -3,6 +3,7 @@ """ # built-in +from json import dumps from multiprocessing import Pool from pathlib import Path from typing import Any, Dict, NamedTuple @@ -11,6 +12,7 @@ from vcorelib.io import IndentedFileWriter # internal +from ifgen import PKG_NAME, VERSION from ifgen.config import Config StructConfig = Dict[str, Any] @@ -22,14 +24,42 @@ class GenerateStructTask(NamedTuple): root: Path path: Path struct: StructConfig + config: Dict[str, Any] + + def command(self, command: str, data: str = "", space: str = " ") -> str: + """Get a command string.""" + return ( + str(self.config["command"]) + + command + + (space if data else "") + + data + ) def create_struct(task: GenerateStructTask) -> None: """Create a header file based on a struct definition.""" with IndentedFileWriter.from_path(task.path, per_indent=4) as writer: + # Write file header. with writer.javadoc(): - writer.write("Test.") + writer.write(task.command("file")) + writer.write( + task.command("brief", f"Generated by {PKG_NAME} ({VERSION}).") + ) + writer.empty() + writer.write(dumps(task.struct, indent=4)) + + writer.write("#pragma once") + writer.empty() + + # Write namespace. + namespace = "::".join(task.config["namespace"]) + writer.write(f"namespace {namespace}") + with writer.scope(suffix=f"; // namespace {namespace}"): + # Write struct definition. + writer.empty() + writer.cpp_comment("Testing.") + writer.empty() def generate_structs(root: Path, output: Path, config: Config) -> None: @@ -42,7 +72,9 @@ def generate_structs(root: Path, output: Path, config: Config) -> None: pool.map( create_struct, ( - GenerateStructTask(root, output.joinpath(f"{name}.h"), data) + GenerateStructTask( + root, output.joinpath(f"{name}.h"), data, config.data + ) for name, data in config.data["structs"].items() ), ) diff --git a/local/configs/package.yaml b/local/configs/package.yaml index b0f8620..e7d1726 100644 --- a/local/configs/package.yaml +++ b/local/configs/package.yaml @@ -5,7 +5,7 @@ description: An interface generator for distributed computing. entry: {{entry}} requirements: - - vcorelib>=2.4.5 + - vcorelib>=2.4.6 dev_requirements: - pytest-cov diff --git a/tests/data/valid/scenarios/sample/ifgen.yaml b/tests/data/valid/scenarios/sample/ifgen.yaml index 5e38455..c802cc5 100644 --- a/tests/data/valid/scenarios/sample/ifgen.yaml +++ b/tests/data/valid/scenarios/sample/ifgen.yaml @@ -1,4 +1,6 @@ --- +namespace: [A, B] + structs: Test1: {} Test2: {}