aboutsummaryrefslogtreecommitdiff
path: root/tools/interface_generator/bin/interface_definition.py
blob: 2fd7e9aeb5d136d9058945af1c0348b5638ffc98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
interface_definition.py
"""

from dataclasses import dataclass, asdict
from pathlib import Path


@dataclass
class InterfaceDefinition:
    """interface definition class"""

    name: str
    license_hdr: str
    system_includes: list[str]
    types: list[dict]
    functions: list[dict]

    def read_license(self, path: Path):
        """read and starrify a license"""
        if path is None:
            self.license_hdr = ""
            return

        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": [
                    "int fd",
                    "int (*putc)(const struct kstdio_File*, const char)",
                    "int (*puts)(const struct kstdio_File*, const char*)",
                ],
            },
        ]
        self.functions = [
            {
                "name": "printf",
                "return": "int",
                "arguments": ["const char* format"],
                "argument_names": ["format"],
            },
        ]

    def into_dict(self) -> dict:
        """create a dictionary for use in mako"""
        return asdict(self)