File: generate_stub.py

package info (click to toggle)
freeorion 0.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 194,940 kB
  • sloc: cpp: 186,508; python: 40,969; ansic: 1,164; xml: 719; makefile: 32; sh: 7
file content (60 lines) | stat: -rw-r--r-- 1,992 bytes parent folder | download
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
59
60
from stub_generator.interface_inspector import (
    ClassInfo,
    EnumInfo,
    FunctionInfo,
    InstanceInfo,
)
from stub_generator.stub_generator.class_generator import generate_classes
from stub_generator.stub_generator.enum_generator import generate_enums
from stub_generator.stub_generator.function_generator import generate_functions
from stub_generator.stub_generator.result_builder import Import, ResultBuilder


def make_stub(
    classes: list[ClassInfo],
    enums: list[EnumInfo],
    functions: list[FunctionInfo],
    instances: list[InstanceInfo],
    result_path,
    classes_to_ignore: set[str],
):
    header = (
        "# Autogenerated do not modify manually!\n"
        "# This is a type-hinting python stub file, used by python IDEs to provide type hints. For more information\n"
        "# about stub files, see https://www.python.org/dev/peps/pep-0484/#stub-files\n"
        "# During execution, the actual module is made available via\n"
        "# a C++ Boost-python process as part of the launch."
    )

    res = ResultBuilder(header)
    res.add_built_in_import(Import("collections.abc", ["Mapping", "Sequence", "Set"]))
    res.add_built_in_import(Import("enum", ["IntEnum"]))
    res.add_built_in_import(Import("typing", ["overload"]))

    res.add_import(
        Import(
            "common.fo_typing",
            [
                "BuildingId",
                "BuildingName",
                "EmpireId",
                "FleetId",
                "NamedTuple",
                "ObjectId",
                "PartName",
                "PlanetId",
                "PlayerId",
                "ShipId",
                "SpeciesName",
                "SystemId",
                "Turn",
            ],
        )
    )

    res.add_classes(generate_classes(classes, instances, classes_to_ignore, enums))
    res.add_enums(generate_enums(enums))
    res.add_functions(generate_functions(functions))

    with open(result_path, "w") as f:
        res.write(f)