dataclass-native 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataclass_native/__init__.py +60 -0
- dataclass_native/__main__.py +5 -0
- dataclass_native/_arrow.py +822 -0
- dataclass_native/_json.py +783 -0
- dataclass_native/_mode.py +57 -0
- dataclass_native/_msgpack.py +207 -0
- dataclass_native/_native.py +234 -0
- dataclass_native/_runtime.py +607 -0
- dataclass_native/_version.py +24 -0
- dataclass_native/_wire.py +901 -0
- dataclass_native/arrays.py +13 -0
- dataclass_native/backends/__init__.py +0 -0
- dataclass_native/backends/bindings.py +74 -0
- dataclass_native/backends/cpp.py +574 -0
- dataclass_native/backends/templates/bindings.cpp.j2 +58 -0
- dataclass_native/backends/templates/extensions.cmake.j2 +12 -0
- dataclass_native/backends/templates/header.hpp.j2 +75 -0
- dataclass_native/cli.py +125 -0
- dataclass_native/cmake/dataclass_native-config.cmake +67 -0
- dataclass_native/include/dataclass_native/arrow.hpp +2022 -0
- dataclass_native/include/dataclass_native/json.hpp +1903 -0
- dataclass_native/include/dataclass_native/msgpack.hpp +1513 -0
- dataclass_native/include/dataclass_native/python.hpp +594 -0
- dataclass_native/include/dataclass_native/reflect.hpp +63 -0
- dataclass_native/include/dataclass_native/types.hpp +417 -0
- dataclass_native/introspect.py +811 -0
- dataclass_native/py.typed +0 -0
- dataclass_native/schema.py +553 -0
- dataclass_native/types.py +103 -0
- dataclass_native-1.0.0.dist-info/METADATA +339 -0
- dataclass_native-1.0.0.dist-info/RECORD +35 -0
- dataclass_native-1.0.0.dist-info/WHEEL +5 -0
- dataclass_native-1.0.0.dist-info/entry_points.txt +2 -0
- dataclass_native-1.0.0.dist-info/licenses/LICENSE +21 -0
- dataclass_native-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Generated by dataclass-native from {{ module }}: do not edit
|
|
2
|
+
#include "dataclass_native/python.hpp"
|
|
3
|
+
|
|
4
|
+
#include <nanobind/stl/map.h>
|
|
5
|
+
#include <nanobind/stl/optional.h>
|
|
6
|
+
#include <nanobind/stl/set.h>
|
|
7
|
+
#include <nanobind/stl/string.h>
|
|
8
|
+
#include <nanobind/stl/tuple.h>
|
|
9
|
+
#include <nanobind/stl/variant.h>
|
|
10
|
+
#include <nanobind/stl/vector.h>
|
|
11
|
+
|
|
12
|
+
#include "{{ header }}"
|
|
13
|
+
|
|
14
|
+
NB_MODULE({{ extension }}, m)
|
|
15
|
+
{
|
|
16
|
+
{% for dependency in dependencies %}
|
|
17
|
+
nanobind::module_::import_("{{ dependency }}");
|
|
18
|
+
{% endfor %}
|
|
19
|
+
{% if dependencies %}
|
|
20
|
+
|
|
21
|
+
{% endif %}
|
|
22
|
+
// By name, the hash of the schema each type was generated from
|
|
23
|
+
nanobind::dict schema;
|
|
24
|
+
{% for enum in enums %}
|
|
25
|
+
|
|
26
|
+
nanobind::enum_<{{ enum.type }}>(m, "{{ enum.name }}")
|
|
27
|
+
{% for member in enum.members %}
|
|
28
|
+
.value("{{ member.name }}", {{ enum.type }}::{{ member.identifier }}){{ ";" if loop.last }}
|
|
29
|
+
{% endfor %}
|
|
30
|
+
schema["{{ enum.name }}"] = "{{ enum.hash }}";
|
|
31
|
+
{% endfor %}
|
|
32
|
+
{% for struct in structs %}
|
|
33
|
+
|
|
34
|
+
nanobind::class_<{{ struct.type }}>(m, "{{ struct.name }}")
|
|
35
|
+
.def(nanobind::init<>())
|
|
36
|
+
{% for field in struct.fields %}
|
|
37
|
+
{% if struct.mutable %}
|
|
38
|
+
// A copy, as a view of a field would be of whatever it is set to next
|
|
39
|
+
.def_rw("{{ field.name }}", &{{ struct.type }}::{{ field.identifier }}, nanobind::rv_policy::copy)
|
|
40
|
+
{% else %}
|
|
41
|
+
.def_rw("{{ field.name }}", &{{ struct.type }}::{{ field.identifier }})
|
|
42
|
+
{% endif %}
|
|
43
|
+
{% endfor %}
|
|
44
|
+
.def("__msgpack__", &dataclass_native::python::to_msgpack<{{ struct.type }}>)
|
|
45
|
+
{% if struct.json %}
|
|
46
|
+
.def("__json__", &dataclass_native::python::to_json<{{ struct.type }}>)
|
|
47
|
+
.def_static("__from_json__", &dataclass_native::python::from_json<{{ struct.type }}>)
|
|
48
|
+
{% endif %}
|
|
49
|
+
{% if struct.arrow %}
|
|
50
|
+
.def_static("__arrow__", &dataclass_native::python::to_arrow<{{ struct.type }}>)
|
|
51
|
+
.def_static("__from_arrow__", &dataclass_native::python::from_arrow<{{ struct.type }}>)
|
|
52
|
+
{% endif %}
|
|
53
|
+
.def_static("__from_msgpack__", &dataclass_native::python::from_msgpack<{{ struct.type }}>);
|
|
54
|
+
schema["{{ struct.name }}"] = "{{ struct.hash }}";
|
|
55
|
+
{% endfor %}
|
|
56
|
+
|
|
57
|
+
m.attr("__schema__") = schema;
|
|
58
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Generated by dataclass-native: do not edit
|
|
2
|
+
set(DATACLASS_NATIVE_EXTENSIONS
|
|
3
|
+
{% for extension in extensions %}
|
|
4
|
+
"{{ extension }}"
|
|
5
|
+
{% endfor %}
|
|
6
|
+
)
|
|
7
|
+
# The schema modules, on changing which this must be generated again
|
|
8
|
+
set(DATACLASS_NATIVE_SCHEMAS
|
|
9
|
+
{% for schema in schemas %}
|
|
10
|
+
[==[{{ schema }}]==]
|
|
11
|
+
{% endfor %}
|
|
12
|
+
)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Generated by dataclass-native from {{ module }}: do not edit
|
|
2
|
+
#pragma once
|
|
3
|
+
{% if includes %}
|
|
4
|
+
|
|
5
|
+
{% for include in includes %}
|
|
6
|
+
#include {{ include }}
|
|
7
|
+
{% endfor %}
|
|
8
|
+
{% endif %}
|
|
9
|
+
|
|
10
|
+
namespace {{ namespace }}
|
|
11
|
+
{
|
|
12
|
+
{% set gap = joiner("\n") %}
|
|
13
|
+
{% for enum in enums %}
|
|
14
|
+
{{ gap() }} enum class {{ enum.name }} : {{ enum.underlying }}
|
|
15
|
+
{
|
|
16
|
+
{% for member in enum.members %}
|
|
17
|
+
{{ member.name }} = {{ member.value }},
|
|
18
|
+
{% endfor %}
|
|
19
|
+
};
|
|
20
|
+
{% endfor %}
|
|
21
|
+
{% if forward_declarations %}
|
|
22
|
+
{{ gap() }}{% for name in forward_declarations %}
|
|
23
|
+
struct {{ name }};
|
|
24
|
+
{% endfor %}
|
|
25
|
+
{% endif %}
|
|
26
|
+
{% for struct in structs %}
|
|
27
|
+
{{ gap() }} struct {{ struct.name }}
|
|
28
|
+
{
|
|
29
|
+
{% for member in struct.members %}
|
|
30
|
+
{{ member }}
|
|
31
|
+
{% endfor %}
|
|
32
|
+
{% if struct.members %}
|
|
33
|
+
|
|
34
|
+
{% endif %}
|
|
35
|
+
bool operator==(const {{ struct.name }}&) const = default;
|
|
36
|
+
};
|
|
37
|
+
{% endfor %}
|
|
38
|
+
} // namespace {{ namespace }}
|
|
39
|
+
{% if enums or structs %}
|
|
40
|
+
|
|
41
|
+
// What dataclass_native/reflect.hpp describes of each
|
|
42
|
+
namespace dataclass_native
|
|
43
|
+
{
|
|
44
|
+
{% set gap = joiner("\n") %}
|
|
45
|
+
{% for enum in enums %}
|
|
46
|
+
{{ gap() }} template <>
|
|
47
|
+
struct Reflect<{{ enum.qualified }}>
|
|
48
|
+
{
|
|
49
|
+
static constexpr std::string_view name = {{ enum.name_literal }};
|
|
50
|
+
static constexpr std::string_view tag = {{ enum.tag_literal }};
|
|
51
|
+
static constexpr std::array members{
|
|
52
|
+
{% for member in enum.members %}
|
|
53
|
+
Member{ {{- enum.qualified }}::{{ member.name }}, {{ member.literal }}, {{ member.text }}, {{ member.quoted }}},
|
|
54
|
+
{% endfor %}
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
{% endfor %}
|
|
58
|
+
{% for struct in structs %}
|
|
59
|
+
{{ gap() }} template <>
|
|
60
|
+
struct Reflect<{{ struct.qualified }}>
|
|
61
|
+
{
|
|
62
|
+
static constexpr std::string_view name = {{ struct.name_literal }};
|
|
63
|
+
static constexpr std::string_view tag = {{ struct.tag_literal }};
|
|
64
|
+
static constexpr bool shares = {{ struct.shares }};
|
|
65
|
+
using shared = Types<{{ struct.shared }}>;
|
|
66
|
+
static constexpr bool arrow = {{ struct.arrow }};
|
|
67
|
+
static constexpr std::tuple fields{
|
|
68
|
+
{% for field in struct.fields %}
|
|
69
|
+
Field{ {{- field.literal }}, &{{ struct.qualified }}::{{ field.name }}, {{ field.has_default }}},
|
|
70
|
+
{% endfor %}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
{% endfor %}
|
|
74
|
+
} // namespace dataclass_native
|
|
75
|
+
{% endif %}
|
dataclass_native/cli.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""
|
|
2
|
+
dataclass-native generate cpp --package my_schemas --out generated/include
|
|
3
|
+
dataclass-native generate nanobind --package my_schemas --out generated
|
|
4
|
+
dataclass-native include-dir
|
|
5
|
+
dataclass-native cmake-dir
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import pkgutil
|
|
9
|
+
import sys
|
|
10
|
+
from argparse import ArgumentParser
|
|
11
|
+
from importlib import import_module
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from ._mode import Mode, set_mode
|
|
15
|
+
from ._native import extension_name
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def include_dir() -> Path:
|
|
19
|
+
"""The directory holding dataclass_native's C++ headers, which generated headers include"""
|
|
20
|
+
return Path(__file__).parent / "include"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def cmake_dir() -> Path:
|
|
24
|
+
"""The directory of dataclass_native's CMake package, which adds the extensions of schema modules to a build"""
|
|
25
|
+
return Path(__file__).parent / "cmake"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _import_package(package: str) -> list[str]:
|
|
29
|
+
module = import_module(package)
|
|
30
|
+
extensions: set[str] = set()
|
|
31
|
+
for info in pkgutil.walk_packages(
|
|
32
|
+
getattr(module, "__path__", []), prefix=f"{package}."
|
|
33
|
+
):
|
|
34
|
+
# Not the extensions of schema modules, built by an earlier run
|
|
35
|
+
if info.name not in extensions:
|
|
36
|
+
import_module(info.name)
|
|
37
|
+
extensions.add(extension_name(info.name))
|
|
38
|
+
|
|
39
|
+
from .introspect import loaded_modules
|
|
40
|
+
|
|
41
|
+
return loaded_modules(package)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _write(out: Path, files: dict[str, str]) -> list[Path]:
|
|
45
|
+
"""Writes files under out, leaving those unchanged untouched, so as not to trigger rebuilds"""
|
|
46
|
+
written = []
|
|
47
|
+
for relative, contents in files.items():
|
|
48
|
+
path = out / relative
|
|
49
|
+
if path.exists() and path.read_text(encoding="utf-8") == contents:
|
|
50
|
+
continue
|
|
51
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
52
|
+
path.write_text(contents, encoding="utf-8")
|
|
53
|
+
written.append(path)
|
|
54
|
+
return written
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def main(argv: list[str] | None = None) -> int:
|
|
58
|
+
parser = ArgumentParser(prog="dataclass-native", description=__doc__)
|
|
59
|
+
commands = parser.add_subparsers(dest="command", required=True)
|
|
60
|
+
|
|
61
|
+
generate = commands.add_parser("generate", help="Generate code from schema modules")
|
|
62
|
+
generate.add_argument(
|
|
63
|
+
"language",
|
|
64
|
+
choices=["cpp", "nanobind"],
|
|
65
|
+
help="cpp: headers. nanobind: also the sources of the Python extension modules binding them",
|
|
66
|
+
)
|
|
67
|
+
generate.add_argument(
|
|
68
|
+
"-m",
|
|
69
|
+
"--module",
|
|
70
|
+
action="append",
|
|
71
|
+
default=[],
|
|
72
|
+
help="A schema module (repeatable)",
|
|
73
|
+
)
|
|
74
|
+
generate.add_argument(
|
|
75
|
+
"-p",
|
|
76
|
+
"--package",
|
|
77
|
+
action="append",
|
|
78
|
+
default=[],
|
|
79
|
+
help="A package, all of whose schema modules are used",
|
|
80
|
+
)
|
|
81
|
+
generate.add_argument(
|
|
82
|
+
"-o", "--out", type=Path, required=True, help="The output directory"
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
commands.add_parser(
|
|
86
|
+
"include-dir", help="Print the directory of dataclass_native's C++ headers"
|
|
87
|
+
)
|
|
88
|
+
commands.add_parser(
|
|
89
|
+
"cmake-dir", help="Print the directory of dataclass_native's CMake package"
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
args = parser.parse_args(argv)
|
|
93
|
+
|
|
94
|
+
if args.command != "generate":
|
|
95
|
+
print(include_dir() if args.command == "include-dir" else cmake_dir())
|
|
96
|
+
return 0
|
|
97
|
+
|
|
98
|
+
if not args.module and not args.package:
|
|
99
|
+
parser.error("generate needs --module or --package")
|
|
100
|
+
|
|
101
|
+
# Schema modules are imported to be described, not bound
|
|
102
|
+
set_mode(Mode.PURE)
|
|
103
|
+
|
|
104
|
+
from .backends import bindings, cpp
|
|
105
|
+
from .introspect import build_schema
|
|
106
|
+
from .schema import SchemaError
|
|
107
|
+
|
|
108
|
+
try:
|
|
109
|
+
modules = [*args.module, *(m for p in args.package for m in _import_package(p))]
|
|
110
|
+
schema = build_schema(modules)
|
|
111
|
+
files = cpp.generate(schema)
|
|
112
|
+
if args.language == "nanobind":
|
|
113
|
+
files |= bindings.generate(schema)
|
|
114
|
+
except SchemaError as e:
|
|
115
|
+
print(f"dataclass-native: {e}", file=sys.stderr)
|
|
116
|
+
return 1
|
|
117
|
+
|
|
118
|
+
for path in _write(args.out, files):
|
|
119
|
+
print(path)
|
|
120
|
+
|
|
121
|
+
return 0
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
if __name__ == "__main__":
|
|
125
|
+
sys.exit(main())
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# find_package(dataclass_native CONFIG REQUIRED), given dataclass_native_ROOT from `python -m dataclass_native cmake-dir`.
|
|
2
|
+
# Needs Python (Interpreter, Development.Module) and nanobind to have been found first.
|
|
3
|
+
|
|
4
|
+
get_filename_component(_dataclass_native_package "${CMAKE_CURRENT_LIST_DIR}" DIRECTORY)
|
|
5
|
+
set(dataclass_native_INCLUDE_DIR "${_dataclass_native_package}/include")
|
|
6
|
+
unset(_dataclass_native_package)
|
|
7
|
+
|
|
8
|
+
# dataclass_native_add_extensions(
|
|
9
|
+
# [PACKAGES <package>...] Packages, all of whose schema modules are used
|
|
10
|
+
# [MODULES <module>...] Schema modules
|
|
11
|
+
# [PYTHONPATH <dir>...] Where to import them from, if not installed
|
|
12
|
+
# [GENERATED_DIRECTORY <dir>] Where to generate the C++ (default: dataclass_native, in the binary directory)
|
|
13
|
+
# [LIBRARY_DIRECTORY <dir>] Build each extension here, under its package's path: e.g. the source directory
|
|
14
|
+
# of the packages, so as to use them in place
|
|
15
|
+
# [TARGETS_VARIABLE <variable>]) Set to the targets added
|
|
16
|
+
#
|
|
17
|
+
# Generates the C++ of the schema modules, and of those they use, when CMake configures. Adds a nanobind module for
|
|
18
|
+
# each: the extension pkg._mod_native for the schema module pkg.mod, installed to pkg/.
|
|
19
|
+
function(dataclass_native_add_extensions)
|
|
20
|
+
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "GENERATED_DIRECTORY;LIBRARY_DIRECTORY;TARGETS_VARIABLE"
|
|
21
|
+
"PACKAGES;MODULES;PYTHONPATH")
|
|
22
|
+
if(NOT ARG_GENERATED_DIRECTORY)
|
|
23
|
+
set(ARG_GENERATED_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/dataclass_native")
|
|
24
|
+
endif()
|
|
25
|
+
|
|
26
|
+
set(command "${Python_EXECUTABLE}" -m dataclass_native generate nanobind --out "${ARG_GENERATED_DIRECTORY}")
|
|
27
|
+
foreach(package IN LISTS ARG_PACKAGES)
|
|
28
|
+
list(APPEND command --package "${package}")
|
|
29
|
+
endforeach()
|
|
30
|
+
foreach(module IN LISTS ARG_MODULES)
|
|
31
|
+
list(APPEND command --module "${module}")
|
|
32
|
+
endforeach()
|
|
33
|
+
foreach(path IN LISTS ARG_PYTHONPATH)
|
|
34
|
+
list(PREPEND command "${CMAKE_COMMAND}" -E env "--modify" "PYTHONPATH=path_list_prepend:${path}")
|
|
35
|
+
endforeach()
|
|
36
|
+
execute_process(COMMAND ${command} COMMAND_ERROR_IS_FATAL ANY)
|
|
37
|
+
|
|
38
|
+
include("${ARG_GENERATED_DIRECTORY}/extensions.cmake")
|
|
39
|
+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${DATACLASS_NATIVE_SCHEMAS})
|
|
40
|
+
|
|
41
|
+
set(targets)
|
|
42
|
+
foreach(extension IN LISTS DATACLASS_NATIVE_EXTENSIONS)
|
|
43
|
+
string(REPLACE "." "/" path "${extension}")
|
|
44
|
+
string(REPLACE "." "_" target "${extension}")
|
|
45
|
+
get_filename_component(directory "${path}" DIRECTORY)
|
|
46
|
+
get_filename_component(name "${path}" NAME)
|
|
47
|
+
|
|
48
|
+
nanobind_add_module(${target} "${ARG_GENERATED_DIRECTORY}/${path}.cpp")
|
|
49
|
+
target_compile_features(${target} PRIVATE cxx_std_20)
|
|
50
|
+
target_include_directories(${target} PRIVATE "${ARG_GENERATED_DIRECTORY}" "${dataclass_native_INCLUDE_DIR}")
|
|
51
|
+
set_target_properties(${target} PROPERTIES OUTPUT_NAME "${name}")
|
|
52
|
+
if(ARG_LIBRARY_DIRECTORY)
|
|
53
|
+
# As a generator expression, so that generators such as Visual Studio do not add a directory per configuration
|
|
54
|
+
set_target_properties(${target} PROPERTIES
|
|
55
|
+
LIBRARY_OUTPUT_DIRECTORY "$<1:${ARG_LIBRARY_DIRECTORY}/${directory}>")
|
|
56
|
+
endif()
|
|
57
|
+
if(NOT directory)
|
|
58
|
+
set(directory ".")
|
|
59
|
+
endif()
|
|
60
|
+
install(TARGETS ${target} LIBRARY DESTINATION "${directory}")
|
|
61
|
+
list(APPEND targets ${target})
|
|
62
|
+
endforeach()
|
|
63
|
+
|
|
64
|
+
if(ARG_TARGETS_VARIABLE)
|
|
65
|
+
set(${ARG_TARGETS_VARIABLE} "${targets}" PARENT_SCOPE)
|
|
66
|
+
endif()
|
|
67
|
+
endfunction()
|