Skip to content

Commit 56fbb2f

Browse files
committed
Load directory file-api objects
Signed-off-by: Cristian Le <[email protected]>
1 parent 74af638 commit 56fbb2f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/scikit_build_core/file_api/_cattrs_converter.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from .model.cache import Cache
1212
from .model.cmakefiles import CMakeFiles
1313
from .model.codemodel import CodeModel, Target
14+
from .model.codemodel import Directory as CodeModelDirectory
15+
from .model.directory import Directory
1416
from .model.index import Index, Reply
1517

1618
T = TypeVar("T")
@@ -37,16 +39,21 @@ def make_converter(base_dir: Path) -> cattr.preconf.json.JsonConverter:
3739
converter.register_structure_hook(Reply, st_hook)
3840

3941
def from_json_file(with_path: Dict[str, Any], t: Type[T]) -> T:
42+
if "jsonFile" not in with_path and t is CodeModelDirectory:
43+
return converter.structure_attrs_fromdict(with_path, t)
4044
if with_path["jsonFile"] is None:
4145
return converter.structure_attrs_fromdict({}, t)
4246
path = base_dir / Path(with_path["jsonFile"])
4347
raw = json.loads(path.read_text(encoding="utf-8"))
48+
if t is CodeModelDirectory:
49+
t = Directory # type: ignore[assignment]
4450
return converter.structure_attrs_fromdict(raw, t)
4551

4652
converter.register_structure_hook(CodeModel, from_json_file)
4753
converter.register_structure_hook(Target, from_json_file)
4854
converter.register_structure_hook(Cache, from_json_file)
4955
converter.register_structure_hook(CMakeFiles, from_json_file)
56+
converter.register_structure_hook(CodeModelDirectory, from_json_file)
5057
return converter
5158

5259

src/scikit_build_core/file_api/reply.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from .model.cache import Cache
1212
from .model.cmakefiles import CMakeFiles
1313
from .model.codemodel import CodeModel, Target
14+
from .model.codemodel import Directory as CodeModelDirectory
1415
from .model.directory import Directory
1516
from .model.index import Index
1617

@@ -51,10 +52,14 @@ def make_class(self, data: InputDict, target: Type[T]) -> T:
5152
Convert a dict to a dataclass. Automatically load a few nested jsonFile classes.
5253
"""
5354
if (
54-
target in {CodeModel, Target, Cache, CMakeFiles, Directory}
55+
target in {CodeModel, Target, Cache, CMakeFiles, CodeModelDirectory}
5556
and "jsonFile" in data
5657
and data["jsonFile"] is not None
5758
):
59+
# Transform CodeModelDirectory to Directory object
60+
# TODO: inherit the fields from CodeModel counterparts
61+
if target is CodeModelDirectory:
62+
target = Directory # type: ignore[assignment]
5863
return self._load_from_json(Path(data["jsonFile"]), target)
5964

6065
input_dict: Dict[str, Type[Any]] = {}

0 commit comments

Comments
 (0)