• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aas-core-works / aas-core-codegen / 21650440193

03 Feb 2026 10:33PM UTC coverage: 82.263% (+0.05%) from 82.215%
21650440193

push

github

web-flow
Refactor python generator for many files (#578)

We refactor the code of Python generator so that it can be extended with
many more files. In the future, we plan to also generate the code of
unit test files, so we already prepare for that change.

12 of 17 new or added lines in 1 file covered. (70.59%)

28626 of 34798 relevant lines covered (82.26%)

3.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

64.58
/aas_core_codegen/python/main.py
1
"""Generate Python code to handle AAS models based on the meta-model."""
2
import pathlib
4 ✔
3
from typing import TextIO, Sequence, Callable, Tuple, Optional, List
4 ✔
4

5
from aas_core_codegen import specific_implementations, run, intermediate
4 ✔
6
from aas_core_codegen.common import Error
4 ✔
7
from aas_core_codegen.python import (
4 ✔
8
    common as python_common,
9
    structure as python_structure,
10
    constants as python_constants,
11
    aas_common as python_aas_common,
12
    stringification as python_stringification,
13
    verification as python_verification,
14
    jsonization as python_jsonization,
15
    xmlization as python_xmlization,
16
)
17

18

19
def execute(context: run.Context, stdout: TextIO, stderr: TextIO) -> int:
4 ✔
20
    """Generate the code."""
21
    verified_ir_table, errors = python_structure.verify(
4 ✔
22
        symbol_table=context.symbol_table
23
    )
24

25
    if errors is not None:
4 ✔
26
        run.write_error_report(
×
27
            message=f"Failed to verify the intermediate symbol table "
28
            f"for generation of Python code"
29
            f"based on {context.model_path}",
30
            errors=[context.lineno_columner.error_message(error) for error in errors],
31
            stderr=stderr,
32
        )
33
        return 1
×
34

35
    assert verified_ir_table is not None
4 ✔
36

37
    unsupported_contracts_errors = (
4 ✔
38
        intermediate.errors_if_contracts_for_functions_or_methods_defined(
39
            verified_ir_table
40
        )
41
    )
42
    if unsupported_contracts_errors is not None:
4 ✔
43
        run.write_error_report(
×
44
            message=f"We do not support pre and post-conditions and snapshots "
45
            f"at the moment. Please notify the developers if you need this "
46
            f"feature (based on meta-model {context.model_path})",
47
            errors=[
48
                context.lineno_columner.error_message(error)
49
                for error in unsupported_contracts_errors
50
            ],
51
            stderr=stderr,
52
        )
53
        return 1
×
54

55
    unsupported_methods_errors = (
4 ✔
56
        intermediate.errors_if_non_implementation_specific_methods(verified_ir_table)
57
    )
58
    if unsupported_methods_errors is not None:
4 ✔
59
        run.write_error_report(
×
60
            message=f"We added some support for understood methods already and keep "
61
            f"maintaining it as it is only a matter of time when we will "
62
            f"introduce their transpilation. Introducing them after the fact "
63
            f"would have been much more difficult.\n"
64
            f"\n"
65
            f"At the given moment, however, we deliberately focus only on "
66
            f"implementation-specific methods. "
67
            f"(based on meta-model {context.model_path})",
68
            errors=[
69
                context.lineno_columner.error_message(error)
70
                for error in unsupported_methods_errors
71
            ],
72
            stderr=stderr,
73
        )
74
        return 1
×
75

76
    aas_module_key = specific_implementations.ImplementationKey(
4 ✔
77
        "qualified_module_name.txt"
78
    )
79

80
    aas_module_text = context.spec_impls.get(aas_module_key, None)
4 ✔
81
    if aas_module_text is None:
4 ✔
82
        stderr.write(
×
83
            f"The snippet with the qualified module name is missing: {aas_module_key}\n"
84
        )
85
        return 1
×
86

87
    if not python_common.QUALIFIED_MODULE_NAME_RE.fullmatch(aas_module_text):
4 ✔
88
        stderr.write(
×
89
            f"The text from the snippet {aas_module_key} "
90
            f"is not a valid qualified module name: {aas_module_text!r}\n"
91
        )
92
        return 1
×
93

94
    aas_module = python_common.QualifiedModuleName(aas_module_text)
4 ✔
95

96
    verify_errors = python_verification.verify(
4 ✔
97
        spec_impls=context.spec_impls,
98
        verification_functions=verified_ir_table.verification_functions,
99
    )
100

101
    if verify_errors is not None:
4 ✔
102
        run.write_error_report(
×
103
            message="Failed to verify the Python-specific structures",
104
            errors=verify_errors,
105
            stderr=stderr,
106
        )
107
        return 1
×
108

109
    rel_paths_generators: Sequence[
4 ✔
110
        Tuple[pathlib.Path, Callable[[], Tuple[Optional[str], Optional[List[Error]]]]]
111
    ] = [
112
        (
113
            pathlib.Path("types.py"),
114
            lambda: python_structure.generate(
115
                symbol_table=verified_ir_table,
116
                aas_module=aas_module,
117
                spec_impls=context.spec_impls,
118
            ),
119
        ),
120
        (
121
            pathlib.Path("constants.py"),
122
            lambda: python_constants.generate(
123
                symbol_table=context.symbol_table, aas_module=aas_module
124
            ),
125
        ),
126
        (
127
            pathlib.Path("common.py"),
128
            lambda: (python_aas_common.generate(aas_module=aas_module), None),
129
        ),
130
        (
131
            pathlib.Path("stringification.py"),
132
            lambda: python_stringification.generate(
133
                symbol_table=context.symbol_table, aas_module=aas_module
134
            ),
135
        ),
136
        (
137
            pathlib.Path("verification.py"),
138
            lambda: python_verification.generate(
139
                symbol_table=verified_ir_table,
140
                aas_module=aas_module,
141
                spec_impls=context.spec_impls,
142
            ),
143
        ),
144
        (
145
            pathlib.Path("jsonization.py"),
146
            lambda: python_jsonization.generate(
147
                symbol_table=context.symbol_table,
148
                aas_module=aas_module,
149
                spec_impls=context.spec_impls,
150
            ),
151
        ),
152
        (
153
            pathlib.Path("xmlization.py"),
154
            lambda: python_xmlization.generate(
155
                symbol_table=context.symbol_table,
156
                aas_module=aas_module,
157
                spec_impls=context.spec_impls,
158
            ),
159
        ),
160
    ]
161

162
    for rel_path, generator_func in rel_paths_generators:
4 ✔
163
        assert not rel_path.is_absolute()
4 ✔
164

165
        code, errors = generator_func()
4 ✔
166

167
        if errors is not None:
4 ✔
NEW
168
            run.write_error_report(
×
169
                message=f"Failed to generate {rel_path} "
170
                f"based on {context.model_path}",
171
                errors=[
172
                    context.lineno_columner.error_message(error) for error in errors
173
                ],
174
                stderr=stderr,
175
            )
NEW
176
            return 1
×
177

178
        assert code is not None
4 ✔
179

180
        pth = context.output_dir / rel_path
4 ✔
181
        try:
4 ✔
182
            pth.write_text(code, encoding="utf-8")
4 ✔
NEW
183
        except Exception as exception:
×
NEW
184
            run.write_error_report(
×
185
                message=f"Failed to write to {pth}",
186
                errors=[str(exception)],
187
                stderr=stderr,
188
            )
NEW
189
            return 1
×
190

191
    stdout.write(f"Code generated to: {context.output_dir}\n")
4 ✔
192
    return 0
4 ✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc