cognite-neat 1.0.23__py3-none-any.whl → 1.0.24__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.
@@ -1,9 +1,16 @@
1
+ import warnings
2
+ import zipfile
3
+ from collections.abc import Iterator
1
4
  from pathlib import Path
2
5
 
3
6
  import yaml
7
+ from pydantic import BaseModel
4
8
 
5
9
  from cognite.neat._data_model.exporters._base import DMSExporter, DMSFileExporter
6
10
  from cognite.neat._data_model.models.dms import RequestSchema
11
+ from cognite.neat._data_model.models.dms._container import ContainerRequest
12
+ from cognite.neat._data_model.models.dms._references import NodeReference
13
+ from cognite.neat._data_model.models.dms._views import ViewRequest
7
14
 
8
15
 
9
16
  class DMSAPIExporter(DMSExporter[RequestSchema]):
@@ -16,12 +23,90 @@ class DMSAPIExporter(DMSExporter[RequestSchema]):
16
23
 
17
24
  class DMSAPIYAMLExporter(DMSAPIExporter, DMSFileExporter[RequestSchema]):
18
25
  def export_to_file(self, data_model: RequestSchema, file_path: Path) -> None:
19
- """Export the data model to a YAML file in API format."""
20
- if file_path.suffix.lower() not in {".yaml", ".yml"}:
21
- raise ValueError("The file path must have a .yaml or .yml extension.")
26
+ """Export the data model to a YAML files or zip file in API format.
22
27
 
23
- api_format = data_model.model_dump(mode="json", by_alias=True)
24
- file_path.write_text(yaml.safe_dump(api_format, sort_keys=False), encoding=self.ENCODING, newline=self.NEW_LINE)
28
+ Args:
29
+ data_model: Request schema
30
+ file_path: The directory or zip file to save the schema to.
31
+
32
+ """
33
+
34
+ if file_path.is_dir():
35
+ self._export_to_directory(data_model, file_path)
36
+ else:
37
+ self._export_to_zip_file(data_model, file_path)
38
+
39
+ def _export_to_directory(self, data_model: RequestSchema, directory: Path) -> None:
40
+ """Save the schema to a directory as YAML files. This is compatible with the Cognite-Toolkit convention.
41
+
42
+ Args:
43
+ data_model: Request schema
44
+ directory: The directory to save the schema to.
45
+ """
46
+
47
+ subdir = directory / "data_models"
48
+ subdir.mkdir(parents=True, exist_ok=True)
49
+
50
+ for file_path, yaml_content in self._generate_yaml_entries(data_model):
51
+ full_path = subdir / file_path
52
+ # Create parent directories if needed (e.g., for views/, containers/, nodes/)
53
+ full_path.parent.mkdir(parents=True, exist_ok=True)
54
+ full_path.write_text(
55
+ yaml_content,
56
+ encoding=self.ENCODING,
57
+ newline=self.NEW_LINE,
58
+ )
59
+
60
+ def _export_to_zip_file(self, data_model: RequestSchema, zip_file: Path) -> None:
61
+ """Save the schema as a zip file containing a directory as YAML files.
62
+ This is compatible with the Cognite-Toolkit convention.
63
+
64
+ Args:
65
+ data_model: Request schema
66
+ zip_file: The zip file path to save the schema to.
67
+ """
68
+ if zip_file.suffix not in {".zip"}:
69
+ warnings.warn("File extension is not .zip, adding it to the file name", stacklevel=2)
70
+ zip_file = zip_file.with_suffix(".zip")
71
+
72
+ with zipfile.ZipFile(zip_file, "w") as zip_ref:
73
+ for file_path, yaml_content in self._generate_yaml_entries(data_model):
74
+ zip_ref.writestr(f"data_models/{file_path}", yaml_content)
75
+
76
+ def _generate_yaml_entries(self, data_model: RequestSchema) -> Iterator[tuple[str, str]]:
77
+ """Generate file paths and YAML content for all data model components.
78
+
79
+ This helper method eliminates duplication by centralizing the logic for
80
+ iterating through spaces, views, containers, and node types.
81
+
82
+ Args:
83
+ data_model: Request schema
84
+
85
+ Yields:
86
+ Tuples of (file_path, yaml_content) for each component.
87
+ File paths are relative to the data_models directory.
88
+ """
89
+
90
+ def _dump(item: BaseModel) -> str:
91
+ return yaml.safe_dump(item.model_dump(mode="json", by_alias=True), sort_keys=False)
92
+
93
+ # Export spaces
94
+ for space in data_model.spaces:
95
+ yield f"{space.space}.space.yaml", _dump(space)
96
+
97
+ # Export data model
98
+ yield f"{data_model.data_model.external_id}.datamodel.yaml", _dump(data_model.data_model)
99
+
100
+ component_configs: list[tuple[str, list[ViewRequest] | list[ContainerRequest] | list[NodeReference]]] = [
101
+ ("views", data_model.views),
102
+ ("containers", data_model.containers),
103
+ ("nodes", data_model.node_types),
104
+ ]
105
+
106
+ for dir_prefix, components in component_configs:
107
+ file_suffix = dir_prefix.removesuffix("s")
108
+ for component in components:
109
+ yield f"{dir_prefix}/{component.external_id}.{file_suffix}.yaml", _dump(component)
25
110
 
26
111
 
27
112
  class DMSAPIJSONExporter(DMSAPIExporter, DMSFileExporter[RequestSchema]):
cognite/neat/_version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = "1.0.23"
1
+ __version__ = "1.0.24"
2
2
  __engine__ = "^2.0.4"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cognite-neat
3
- Version: 1.0.23
3
+ Version: 1.0.24
4
4
  Summary: Knowledge graph transformation
5
5
  Author: Nikola Vasiljevic, Anders Albert
6
6
  Author-email: Nikola Vasiljevic <nikola.vasiljevic@cognite.com>, Anders Albert <anders.albert@cognite.com>
@@ -29,7 +29,7 @@ cognite/neat/_data_model/deployer/_differ_view.py,sha256=g1xHwsoxFUaTOTtQa19nntK
29
29
  cognite/neat/_data_model/deployer/data_classes.py,sha256=cq86u7wuKCcvH-A_cGI_gWzlQCTIG6mrXG2MahdiGco,27299
30
30
  cognite/neat/_data_model/deployer/deployer.py,sha256=lEdTh4jOwTxLkSEx-SlcnXUZyPZCUtIzop1zhAe2s44,19681
31
31
  cognite/neat/_data_model/exporters/__init__.py,sha256=AskjmB_0Vqib4kN84bWt8-M8nO42QypFf-l-E8oA5W8,482
32
- cognite/neat/_data_model/exporters/_api_exporter.py,sha256=G9cqezy_SH8VdhW4o862qBHh_QcbzfP6O1Yyjvdpeog,1579
32
+ cognite/neat/_data_model/exporters/_api_exporter.py,sha256=nBDHx9dGbaje0T4IEQv0Kulk2Yu7FkPXgXK_MgLbE50,4948
33
33
  cognite/neat/_data_model/exporters/_base.py,sha256=rG_qAU5i5Hh5hUMep2UmDFFZID4x3LEenL6Z5C6N8GQ,646
34
34
  cognite/neat/_data_model/exporters/_table_exporter/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
35
35
  cognite/neat/_data_model/exporters/_table_exporter/exporter.py,sha256=4BPu_Chtjh1EyOaKbThXYohsqllVOkCbSoNekNZuBXc,5159
@@ -321,9 +321,9 @@ cognite/neat/_v0/session/_template.py,sha256=BNcvrW5y7LWzRM1XFxZkfR1Nc7e8UgjBClH
321
321
  cognite/neat/_v0/session/_to.py,sha256=AnsRSDDdfFyYwSgi0Z-904X7WdLtPfLlR0x1xsu_jAo,19447
322
322
  cognite/neat/_v0/session/_wizard.py,sha256=baPJgXAAF3d1bn4nbIzon1gWfJOeS5T43UXRDJEnD3c,1490
323
323
  cognite/neat/_v0/session/exceptions.py,sha256=jv52D-SjxGfgqaHR8vnpzo0SOJETIuwbyffSWAxSDJw,3495
324
- cognite/neat/_version.py,sha256=rj_LKbqpGZavJZekkd20Oo3-5Wi6i3pBDjeRkCt22zY,45
324
+ cognite/neat/_version.py,sha256=PO9Xwbm-V67g819NDS3RNL4T-HEXrVj1Uh15fuaKXMw,45
325
325
  cognite/neat/legacy.py,sha256=eI2ecxOV8ilGHyLZlN54ve_abtoK34oXognkFv3yvF0,219
326
326
  cognite/neat/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
327
- cognite_neat-1.0.23.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
328
- cognite_neat-1.0.23.dist-info/METADATA,sha256=hByYgnOhvq27boiIAHGRyCwddaWgqPxcP_jX8TytSMs,6689
329
- cognite_neat-1.0.23.dist-info/RECORD,,
327
+ cognite_neat-1.0.24.dist-info/WHEEL,sha256=XV0cjMrO7zXhVAIyyc8aFf1VjZ33Fen4IiJk5zFlC3g,80
328
+ cognite_neat-1.0.24.dist-info/METADATA,sha256=2vPtDebNUHh5VM6_xSvyvtiDYEegtjFKofJk2siB6Iw,6689
329
+ cognite_neat-1.0.24.dist-info/RECORD,,