2
2
3
3
from abc import ABC , abstractmethod
4
4
from dataclasses import asdict , dataclass
5
- from typing import TYPE_CHECKING , Any , Literal
5
+ from typing import TYPE_CHECKING , Any , Literal , cast
6
6
7
7
from typing_extensions import Self
8
8
9
9
from cognite .client .data_classes ._base import (
10
10
CogniteFilter ,
11
+ CogniteObject ,
11
12
CogniteResourceList ,
12
13
)
13
- from cognite .client .data_classes .data_modeling ._core import DataModelingResource
14
14
from cognite .client .data_classes .data_modeling ._validation import validate_data_modeling_identifier
15
+ from cognite .client .data_classes .data_modeling .core import DataModelingResource
15
16
from cognite .client .data_classes .data_modeling .data_types import (
16
17
DirectRelation ,
17
18
PropertyType ,
@@ -112,6 +113,23 @@ def __init__(
112
113
validate_data_modeling_identifier (space , external_id )
113
114
super ().__init__ (space , external_id , properties , description , name , used_for , constraints , indexes )
114
115
116
+ @classmethod
117
+ def _load (cls , resource : dict , cognite_client : CogniteClient | None = None ) -> ContainerApply :
118
+ return ContainerApply (
119
+ space = resource ["space" ],
120
+ external_id = resource ["externalId" ],
121
+ properties = {k : ContainerProperty .load (v ) for k , v in resource ["properties" ].items ()},
122
+ description = resource .get ("description" ),
123
+ name = resource .get ("name" ),
124
+ used_for = resource .get ("usedFor" ),
125
+ constraints = {k : Constraint .load (v ) for k , v in resource ["constraints" ].items ()}
126
+ if "constraints" in resource
127
+ else None ,
128
+ indexes = {k : Index .load (v ) for k , v in resource ["indexes" ].items ()} or None
129
+ if "indexes" in resource
130
+ else None ,
131
+ )
132
+
115
133
116
134
class Container (ContainerCore ):
117
135
"""Represent the physical storage of data. This is the read format of the container
@@ -210,7 +228,7 @@ def __init__(self, space: str | None = None, include_global: bool = False) -> No
210
228
211
229
212
230
@dataclass (frozen = True )
213
- class ContainerProperty :
231
+ class ContainerProperty ( CogniteObject ) :
214
232
type : PropertyType
215
233
nullable : bool = True
216
234
auto_increment : bool = False
@@ -219,21 +237,21 @@ class ContainerProperty:
219
237
description : str | None = None
220
238
221
239
@classmethod
222
- def load (cls , data : dict [str , Any ]) -> ContainerProperty :
223
- if "type" not in data :
240
+ def _load (cls , resource : dict [str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
241
+ if "type" not in resource :
224
242
raise ValueError ("Type not specified" )
225
- if data ["type" ].get ("type" ) == "direct" :
226
- type_ : PropertyType = DirectRelation .load (data ["type" ])
243
+ if resource ["type" ].get ("type" ) == "direct" :
244
+ type_ : PropertyType = DirectRelation .load (resource ["type" ])
227
245
else :
228
- type_ = PropertyType .load (data ["type" ])
246
+ type_ = PropertyType .load (resource ["type" ])
229
247
return cls (
230
248
type = type_ ,
231
249
# If nothing is specified, we will pass through null values
232
- nullable = data .get ("nullable" ), # type: ignore[arg-type]
233
- auto_increment = data .get ("autoIncrement" ), # type: ignore[arg-type]
234
- name = data .get ("name" ),
235
- default_value = data .get ("defaultValue" ),
236
- description = data .get ("description" ),
250
+ nullable = resource .get ("nullable" ), # type: ignore[arg-type]
251
+ auto_increment = resource .get ("autoIncrement" ), # type: ignore[arg-type]
252
+ name = resource .get ("name" ),
253
+ default_value = resource .get ("defaultValue" ),
254
+ description = resource .get ("description" ),
237
255
)
238
256
239
257
def dump (self , camel_case : bool = True ) -> dict [str , str | dict ]:
@@ -247,14 +265,14 @@ def dump(self, camel_case: bool = True) -> dict[str, str | dict]:
247
265
248
266
249
267
@dataclass (frozen = True )
250
- class Constraint (ABC ):
268
+ class Constraint (CogniteObject , ABC ):
251
269
@classmethod
252
- def load (cls , data : dict ) -> RequiresConstraint | UniquenessConstraint :
253
- if data ["constraintType" ] == "requires" :
254
- return RequiresConstraint .load (data )
255
- elif data ["constraintType" ] == "uniqueness" :
256
- return UniquenessConstraint .load (data )
257
- raise ValueError (f"Invalid constraint type { data ['constraintType' ]} " )
270
+ def _load (cls , resource : dict [ str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
271
+ if resource ["constraintType" ] == "requires" :
272
+ return cast ( Self , RequiresConstraint .load (resource ) )
273
+ elif resource ["constraintType" ] == "uniqueness" :
274
+ return cast ( Self , UniquenessConstraint .load (resource ) )
275
+ raise ValueError (f"Invalid constraint type { resource ['constraintType' ]} " )
258
276
259
277
@abstractmethod
260
278
def dump (self , camel_case : bool = True ) -> dict [str , str | dict ]:
@@ -266,8 +284,8 @@ class RequiresConstraint(Constraint):
266
284
require : ContainerId
267
285
268
286
@classmethod
269
- def load (cls , data : dict ) -> RequiresConstraint :
270
- return cls (require = ContainerId .load (data ["require" ]))
287
+ def _load (cls , resource : dict [ str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
288
+ return cls (require = ContainerId .load (resource ["require" ]))
271
289
272
290
def dump (self , camel_case : bool = True ) -> dict [str , str | dict ]:
273
291
as_dict = asdict (self )
@@ -284,8 +302,8 @@ class UniquenessConstraint(Constraint):
284
302
properties : list [str ]
285
303
286
304
@classmethod
287
- def load (cls , data : dict ) -> UniquenessConstraint :
288
- return cls (properties = data ["properties" ])
305
+ def _load (cls , resource : dict [ str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
306
+ return cls (properties = resource ["properties" ])
289
307
290
308
def dump (self , camel_case : bool = True ) -> dict [str , str | dict ]:
291
309
as_dict = asdict (self )
@@ -296,14 +314,14 @@ def dump(self, camel_case: bool = True) -> dict[str, str | dict]:
296
314
297
315
298
316
@dataclass (frozen = True )
299
- class Index (ABC ):
317
+ class Index (CogniteObject , ABC ):
300
318
@classmethod
301
- def load (cls , data : dict ) -> Index :
302
- if data ["indexType" ] == "btree" :
303
- return BTreeIndex .load (data )
304
- if data ["indexType" ] == "inverted" :
305
- return InvertedIndex .load (data )
306
- raise ValueError (f"Invalid index type { data ['indexType' ]} " )
319
+ def _load (cls , resource : dict [ str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
320
+ if resource ["indexType" ] == "btree" :
321
+ return cast ( Self , BTreeIndex .load (resource ) )
322
+ if resource ["indexType" ] == "inverted" :
323
+ return cast ( Self , InvertedIndex .load (resource ) )
324
+ raise ValueError (f"Invalid index type { resource ['indexType' ]} " )
307
325
308
326
@abstractmethod
309
327
def dump (self , camel_case : bool = True ) -> dict [str , str | dict ]:
@@ -316,8 +334,8 @@ class BTreeIndex(Index):
316
334
cursorable : bool = False
317
335
318
336
@classmethod
319
- def load (cls , data : dict [str , Any ]) -> BTreeIndex :
320
- return cls (properties = data ["properties" ], cursorable = data .get ("cursorable" )) # type: ignore[arg-type]
337
+ def _load (cls , resource : dict [str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
338
+ return cls (properties = resource ["properties" ], cursorable = resource .get ("cursorable" )) # type: ignore[arg-type]
321
339
322
340
def dump (self , camel_case : bool = True ) -> dict [str , Any ]:
323
341
dumped : dict [str , Any ] = {"properties" : self .properties }
@@ -332,8 +350,8 @@ class InvertedIndex(Index):
332
350
properties : list [str ]
333
351
334
352
@classmethod
335
- def load (cls , data : dict [str , Any ]) -> InvertedIndex :
336
- return cls (properties = data ["properties" ])
353
+ def _load (cls , resource : dict [str , Any ], cognite_client : CogniteClient | None = None ) -> Self :
354
+ return cls (properties = resource ["properties" ])
337
355
338
356
def dump (self , camel_case : bool = True ) -> dict [str , Any ]:
339
357
dumped : dict [str , Any ] = {"properties" : self .properties }
0 commit comments