aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tools/interface_generator/bin/interface_definition.py57
-rw-r--r--tools/interface_generator/bin/interface_definition_unittest.py19
-rwxr-xr-xtools/interface_generator/bin/interface_generator.py14
-rw-r--r--tools/interface_generator/bin/templates/__header.mako2
-rw-r--r--[-rwxr-xr-x]tools/interface_generator/bin/templates_unittest.py8
5 files changed, 63 insertions, 37 deletions
diff --git a/tools/interface_generator/bin/interface_definition.py b/tools/interface_generator/bin/interface_definition.py
index 3b9c4a3..2fd7e9a 100644
--- a/tools/interface_generator/bin/interface_definition.py
+++ b/tools/interface_generator/bin/interface_definition.py
@@ -2,29 +2,39 @@
interface_definition.py
"""
+from dataclasses import dataclass, asdict
+from pathlib import Path
-def __read_license(path):
- """read and starrify a license"""
- license_text = ""
- with open(path, encoding="utf-8") as license_file:
- license_text = "".join(
- [
- f" * { line.rstrip().ljust(72)[:72] } * \n"
- for line in license_file.readlines()
- ]
- ).rstrip()
- return license_text
+@dataclass
+class InterfaceDefinition:
+ """interface definition class"""
+ name: str
+ license_hdr: str
+ system_includes: list[str]
+ types: list[dict]
+ functions: list[dict]
-def parse(args):
- """return a mock interface definition"""
+ def read_license(self, path: Path):
+ """read and starrify a license"""
+ if path is None:
+ self.license_hdr = ""
+ return
- interface_dict = {
- "name": "kstdio",
- "license": __read_license(args.license),
- "system_includes": ["stdarg.h"],
- "types": [
+ with open(path, encoding="utf-8") as license_file:
+ self.license_hdr = "".join(
+ [
+ f" * { line.rstrip().ljust(72)[:72] } * \n"
+ for line in license_file.readlines()
+ ]
+ ).rstrip()
+
+ def __init__(self, name: str, license_path: Path):
+ self.name = name if name is not None else "kstdio"
+ self.read_license(license_path)
+ self.system_includes = ["stdarg.h"]
+ self.types = [
{
"name": "File",
"members": [
@@ -33,15 +43,16 @@ def parse(args):
"int (*puts)(const struct kstdio_File*, const char*)",
],
},
- ],
- "functions": [
+ ]
+ self.functions = [
{
"name": "printf",
"return": "int",
"arguments": ["const char* format"],
"argument_names": ["format"],
},
- ],
- }
+ ]
- return interface_dict
+ def into_dict(self) -> dict:
+ """create a dictionary for use in mako"""
+ return asdict(self)
diff --git a/tools/interface_generator/bin/interface_definition_unittest.py b/tools/interface_generator/bin/interface_definition_unittest.py
new file mode 100644
index 0000000..b459cb4
--- /dev/null
+++ b/tools/interface_generator/bin/interface_definition_unittest.py
@@ -0,0 +1,19 @@
+"""
+interface definition unit tests
+"""
+
+import unittest
+from interface_definition import InterfaceDefinition
+
+
+class InterfaceDefinitionUnittest(unittest.TestCase):
+ """interface_definition unit tests"""
+
+ def test_interfacedefinition_class_asdict(self):
+ """test mock interface"""
+ interface = InterfaceDefinition("kstdio", None)
+ interface_dict = interface.into_dict()
+
+ self.assertEqual(interface.name, "kstdio")
+ self.assertEqual(len(interface.license_hdr), 0)
+ self.assertEqual(interface_dict["name"], "kstdio")
diff --git a/tools/interface_generator/bin/interface_generator.py b/tools/interface_generator/bin/interface_generator.py
index 04553e3..0534709 100755
--- a/tools/interface_generator/bin/interface_generator.py
+++ b/tools/interface_generator/bin/interface_generator.py
@@ -8,7 +8,7 @@ from argparse import ArgumentParser
from pathlib import Path
import sys
from mako.lookup import TemplateLookup
-from interface_definition import parse as parse_interface
+from interface_definition import InterfaceDefinition
from templates import get_templates, get_templates_dir
PROG = {
@@ -17,14 +17,16 @@ PROG = {
}
-def generate_file(template: Path, templates: Path, output, kwargs):
+def generate_file(
+ template: Path, templates: Path, output, interface: InterfaceDefinition
+):
"""generate file using a tempalte and write it to the output location"""
lookup = TemplateLookup(directories=[".", templates.absolute()])
mako_template = lookup.get_template(str(template.relative_to(templates)))
- result = mako_template.render(**kwargs, PROG=PROG)
+ output_name = template.stem.replace("interface", interface.name)
+ print(f"{interface.name} via {template.name} => {output_name}")
- output_name = template.stem.replace("interface", kwargs["name"])
- print(f'{kwargs["name"]} via {template.name} => {output_name}')
+ result = mako_template.render(**interface.into_dict(), PROG=PROG)
if isinstance(output, Path):
# print(f"writing to {(output / output_name).absolute()}")
@@ -72,7 +74,7 @@ def main():
args = parser.parse_args()
# print(args)
- interface = parse_interface(args)
+ interface = InterfaceDefinition(args.interface, args.license)
# print(interface)
for template in get_templates(args.templates):
diff --git a/tools/interface_generator/bin/templates/__header.mako b/tools/interface_generator/bin/templates/__header.mako
index 24b0381..43ce1e7 100644
--- a/tools/interface_generator/bin/templates/__header.mako
+++ b/tools/interface_generator/bin/templates/__header.mako
@@ -1,5 +1,5 @@
/* This file is generated by ${PROG['name']} v${PROG['version']} */
/******************************************************************************
-${ license }
+${ license_hdr }
******************************************************************************/
diff --git a/tools/interface_generator/bin/templates_unittest.py b/tools/interface_generator/bin/templates_unittest.py
index a5adf0f..acb1685 100755..100644
--- a/tools/interface_generator/bin/templates_unittest.py
+++ b/tools/interface_generator/bin/templates_unittest.py
@@ -1,5 +1,3 @@
-#!/usr/bin/env python3
-
"""
templates unit tests
"""
@@ -9,7 +7,7 @@ from pathlib import Path
import templates
-class Templates(unittest.TestCase):
+class TemplatesUnittest(unittest.TestCase):
"""templates unit tests"""
def test_get_templates_dir_is_valid(self):
@@ -30,7 +28,3 @@ class Templates(unittest.TestCase):
self.assertTrue(isinstance(template, Path))
self.assertTrue(template.exists())
self.assertTrue(template.is_file())
-
-
-if __name__ == "__main__":
- unittest.main()