Skip to content

Commit a679066

Browse files
committed
Many fixes
1 parent 8822975 commit a679066

18 files changed

+597
-178
lines changed

src/semiwrap/autowrap/context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,9 @@ class ClassContext:
372372

373373
parent: typing.Optional["ClassContext"]
374374

375+
#: lookup key for dat2trampoline
376+
yml_id: str
377+
375378
#: Namespace that this class lives in
376379
namespace: str
377380

src/semiwrap/autowrap/cxxparser.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ def on_class_start(self, state: AWClassBlockState) -> typing.Optional[bool]:
721721

722722
ctx = ClassContext(
723723
parent=parent_ctx,
724+
yml_id=cls_key,
724725
namespace=cls_namespace,
725726
cpp_name=cls_name,
726727
full_cpp_name=cls_qualname,

src/semiwrap/casters.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import dataclasses
22
import json
3+
import pathlib
34
import typing as T
45

5-
from validobj.errors import ValidationError
6-
from validobj.validation import parse_input
6+
from .config.util import parse_input
7+
from .util import maybe_write_file
78

89
PKGCONF_CASTER_EXT = ".pybind11.json"
910

@@ -23,15 +24,11 @@ class TypeCasterData:
2324

2425
def load_typecaster_data(fname) -> TypeCasterData:
2526
with open(fname) as fp:
26-
try:
27-
return parse_input(json.load(fp), TypeCasterData)
28-
except ValidationError as e:
29-
raise ValidationError(f"Error processing {fname}")
27+
return parse_input(json.load(fp), TypeCasterData, fname)
3028

3129

32-
def save_typecaster_data(fname, data: TypeCasterData):
33-
with open(fname, "w") as fp:
34-
json.dump(dataclasses.asdict(data), fp)
30+
def save_typecaster_data(fname: pathlib.Path, data: TypeCasterData):
31+
maybe_write_file(fname, json.dumps(dataclasses.asdict(data)))
3532

3633

3734
#: content of pickle file

src/semiwrap/cmd_creategen.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ def main():
3636
if isinstance(arg, str):
3737
argv.append(arg)
3838
elif isinstance(arg, InputFile):
39-
argv.append(str(arg.path))
39+
argv.append(str(arg.path.absolute()))
40+
elif isinstance(arg, pathlib.Path):
41+
argv.append(str(arg.absolute()))
4042
else:
4143
# anything else shouldn't matter
4244
argv.append("ignored")
@@ -50,6 +52,7 @@ def main():
5052
name=sargs.name,
5153
src_yml=sargs.src_yml,
5254
src_h=sargs.src_h,
55+
src_h_root=sargs.src_h_root,
5356
dst_dat=None,
5457
dst_depfile=None,
5558
include_paths=sargs.include_paths,

src/semiwrap/cmd_dat2tmplcpp.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Creates an output .hpp file from a .dat file created by parsing a header
2+
Creates a template instance .cpp file from a .dat file created by parsing a header
33
"""
44

55
import inspect
@@ -8,36 +8,36 @@
88
import sys
99

1010
from .autowrap.context import HeaderContext
11-
from .autowrap.render_cls_rpy_include import render_cls_rpy_include_hpp
11+
from .autowrap.render_tmpl_inst import render_template_inst_cpp
1212
from .util import maybe_write_file
1313

1414

1515
def _write_wrapper_cpp(
16-
input_dat: pathlib.Path, class_name: str, output_hpp: pathlib.Path
16+
input_dat: pathlib.Path, py_name: str, output_cpp: pathlib.Path
1717
):
1818
with open(input_dat, "rb") as fp:
1919
hctx = pickle.load(fp)
2020

2121
assert isinstance(hctx, HeaderContext)
2222

23-
for cls in hctx.classes:
24-
if cls.full_cpp_name == class_name:
23+
for tmpl in hctx.template_instances:
24+
if tmpl.py_name == py_name:
2525
break
2626
else:
27-
raise ValueError(f"internal error: cannot find {class_name}")
27+
raise ValueError(f"internal error: cannot find {py_name}")
2828

29-
content = render_cls_rpy_include_hpp(hctx, cls)
30-
maybe_write_file(output_hpp, content)
29+
content = render_template_inst_cpp(hctx, tmpl)
30+
maybe_write_file(output_cpp, content)
3131

3232

3333
def main():
3434
try:
35-
_, input_dat, class_name, output_hpp = sys.argv
35+
_, input_dat, py_name, output_cpp = sys.argv
3636
except ValueError:
3737
print(inspect.cleandoc(__doc__ or ""), file=sys.stderr)
3838
sys.exit(1)
3939

40-
_write_wrapper_cpp(pathlib.Path(input_dat), class_name, pathlib.Path(output_hpp))
40+
_write_wrapper_cpp(pathlib.Path(input_dat), py_name, pathlib.Path(output_cpp))
4141

4242

4343
if __name__ == "__main__":

src/semiwrap/cmd_dat2trampoline.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,51 @@
77
import pickle
88
import sys
99

10-
from .autowrap.context import HeaderContext
10+
from .autowrap.context import HeaderContext, ClassContext
1111
from .autowrap.render_cls_rpy_include import render_cls_rpy_include_hpp
1212
from .util import maybe_write_file
1313

1414

15+
def _get_classes(hctx: HeaderContext):
16+
def _get_child_classes(c: ClassContext):
17+
for c in c.child_classes:
18+
yield c
19+
_get_child_classes(c)
20+
21+
for cls in hctx.classes:
22+
yield cls
23+
yield from _get_child_classes(cls)
24+
25+
1526
def _write_wrapper_cpp(
16-
input_dat: pathlib.Path, class_name: str, output_hpp: pathlib.Path
27+
input_dat: pathlib.Path, yml_id: str, output_hpp: pathlib.Path
1728
):
1829
with open(input_dat, "rb") as fp:
1930
hctx = pickle.load(fp)
2031

2132
assert isinstance(hctx, HeaderContext)
2233

23-
for cls in hctx.classes:
24-
if cls.full_cpp_name == class_name:
34+
avail = []
35+
for cls in _get_classes(hctx):
36+
avail.append(cls.yml_id)
37+
if cls.yml_id == yml_id:
2538
break
2639
else:
27-
raise ValueError(f"internal error: cannot find {class_name}")
40+
avail = ", ".join(avail)
41+
raise ValueError(f"internal error: cannot find {yml_id} (found {avail})")
2842

2943
content = render_cls_rpy_include_hpp(hctx, cls)
3044
maybe_write_file(output_hpp, content)
3145

3246

3347
def main():
3448
try:
35-
_, input_dat, class_name, output_hpp = sys.argv
49+
_, input_dat, yml_id, output_hpp = sys.argv
3650
except ValueError:
3751
print(inspect.cleandoc(__doc__ or ""), file=sys.stderr)
3852
sys.exit(1)
3953

40-
_write_wrapper_cpp(pathlib.Path(input_dat), class_name, pathlib.Path(output_hpp))
54+
_write_wrapper_cpp(pathlib.Path(input_dat), yml_id, pathlib.Path(output_hpp))
4155

4256

4357
if __name__ == "__main__":

src/semiwrap/cmd_gen_libinit.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
# TODO: this belongs in a separate script/api that can be used from multiple tools
2525
# .. maybe in dynamic-library
2626
def _write_libinit_py(
27-
project: PyProject,
27+
# project: PyProject,
2828
init_py: pathlib.Path,
29-
libs: T.List[pathlib.Path],
30-
requires: T.List[str],
29+
modules: T.List[str],
30+
# libs: T.List[pathlib.Path],
31+
# requires: T.List[str],
3132
):
3233
"""
3334
:param init_py: the _init module for the library(ies) that is written out
@@ -38,22 +39,25 @@ def _write_libinit_py(
3839
Their init_py will be looked up and imported first.
3940
"""
4041

41-
platform = project.platform
42+
# platform = project.platform
4243
r = RenderBuffer()
4344

4445
r.writeln("# This file is automatically generated, DO NOT EDIT")
4546
r.writeln("# fmt: off")
4647
r.writeln()
4748

49+
for mod in modules:
50+
r.writeln(f"import {mod}")
51+
4852
need_space = False
49-
for req in requires:
50-
module = project.pkgconf.get(req).libinit_py
51-
if module:
52-
r.writeln(f"import {module}")
53-
need_space = True
53+
# for req in requires:
54+
# module = project.pkgconf.get(req).libinit_py
55+
# if module:
56+
# r.writeln(f"import {module}")
57+
# need_space = True
5458

55-
if need_space:
56-
r.writeln()
59+
# if need_space:
60+
# r.writeln()
5761

5862
# TODO: don't need this?
5963

@@ -114,27 +118,28 @@ def _write_libinit_py(
114118

115119
def main():
116120
try:
117-
_, pyproject_toml, package_name, libinit_py = sys.argv
121+
_, libinit_py = sys.argv[:2]
122+
modules = sys.argv[2:]
118123
except ValueError:
119124
print(inspect.cleandoc(__doc__ or ""), file=sys.stderr)
120125
sys.exit(1)
121126

122-
pyproject = PyProject(pathlib.Path(pyproject_toml))
127+
# pyproject = PyProject(pathlib.Path(pyproject_toml))
123128

124-
module = pyproject.get_extension(package_name)
125-
depends = pyproject.get_extension_deps(module)
129+
# module = pyproject.get_extension(package_name)
130+
# depends = pyproject.get_extension_deps(module)
126131

127132
# TODO: libs
128133
# .. oh, those were going to get created by the downloader hatch plugin
129134
# so we don't need them? maybe
130-
libs = []
135+
# libs = []
131136

132137
# need os, dependencies, section name
133138
_write_libinit_py(
134-
pyproject,
139+
# pyproject,
135140
pathlib.Path(libinit_py),
136-
libs,
137-
depends,
141+
modules,
142+
# depends,
138143
)
139144

140145

0 commit comments

Comments
 (0)