|
59 | 59 | ContainerId,
|
60 | 60 | EdgeId,
|
61 | 61 | NodeId,
|
| 62 | + SourceId, |
| 63 | + SourceIdentifier, |
62 | 64 | ViewId,
|
63 |
| - ViewIdentifier, |
| 65 | + _load_source_id, |
64 | 66 | )
|
65 | 67 | from cognite.client.utils._auxiliary import exactly_one_is_not_none, find_duplicates, flatten_dict
|
66 | 68 | from cognite.client.utils._identifier import InstanceId
|
@@ -224,82 +226,93 @@ def _load(cls, resource: dict, cognite_client: CogniteClient | None = None) -> S
|
224 | 226 | _T = TypeVar("_T")
|
225 | 227 |
|
226 | 228 |
|
227 |
| -class Properties(MutableMapping[ViewIdentifier, MutableMapping[PropertyIdentifier, PropertyValue]]): |
228 |
| - def __init__(self, properties: MutableMapping[ViewId, MutableMapping[PropertyIdentifier, PropertyValue]]) -> None: |
| 229 | +class Properties(MutableMapping[SourceIdentifier, MutableMapping[PropertyIdentifier, PropertyValue]]): |
| 230 | + def __init__(self, properties: MutableMapping[SourceId, MutableMapping[PropertyIdentifier, PropertyValue]]) -> None: |
229 | 231 | self.data = properties
|
230 | 232 |
|
231 | 233 | @classmethod
|
232 | 234 | def load(
|
233 | 235 | cls, data: MutableMapping[Space, MutableMapping[str, MutableMapping[PropertyIdentifier, PropertyValue]]]
|
234 | 236 | ) -> Properties:
|
235 |
| - props: MutableMapping[ViewId, MutableMapping[PropertyIdentifier, PropertyValue]] = {} |
| 237 | + props: MutableMapping[SourceId, MutableMapping[PropertyIdentifier, PropertyValue]] = {} |
236 | 238 | for space, view_properties in data.items():
|
237 |
| - for view_id_str, properties in view_properties.items(): |
238 |
| - view_tuple = tuple(view_id_str.split("/", 1)) |
239 |
| - if len(view_tuple) != 2: |
240 |
| - warnings.warn( |
241 |
| - f"Unknown type of view id: {view_id_str}, expected format <external_id>/<version>. Skipping...", |
242 |
| - stacklevel=2, |
243 |
| - ) |
244 |
| - continue |
245 |
| - view_id = ViewId.load((space, *view_tuple)) |
246 |
| - props[view_id] = properties |
| 239 | + for source_id_str, properties in view_properties.items(): |
| 240 | + if "/" in source_id_str: |
| 241 | + view_tuple = tuple(source_id_str.split("/", 1)) |
| 242 | + if len(view_tuple) != 2: |
| 243 | + warnings.warn( |
| 244 | + f"Unknown type of view id: {source_id_str}, expected format <external_id>/<version>. Skipping...", |
| 245 | + stacklevel=2, |
| 246 | + ) |
| 247 | + continue |
| 248 | + source_id: SourceId = ViewId.load((space, *view_tuple)) |
| 249 | + else: |
| 250 | + source_id = ContainerId.load((space, source_id_str)) |
| 251 | + props[source_id] = properties |
| 252 | + |
247 | 253 | return cls(props)
|
248 | 254 |
|
249 | 255 | def dump(self) -> dict[Space, dict[str, dict[PropertyIdentifier, PropertyValue]]]:
|
250 | 256 | props: dict[Space, dict[str, dict[PropertyIdentifier, PropertyValue]]] = defaultdict(dict)
|
251 |
| - for view_id, properties in self.data.items(): |
252 |
| - view_id_str = f"{view_id.external_id}/{view_id.version}" |
253 |
| - props[view_id.space][view_id_str] = cast(dict[PropertyIdentifier, PropertyValue], properties) |
| 257 | + for source_id, properties in self.data.items(): |
| 258 | + if isinstance(source_id, ViewId): |
| 259 | + source_id_str = f"{source_id.external_id}/{source_id.version}" |
| 260 | + elif isinstance(source_id, ContainerId): |
| 261 | + source_id_str = source_id.external_id |
| 262 | + else: |
| 263 | + raise ValueError("SourceId must be either a ViewId or a ContainerId") |
| 264 | + props[source_id.space][source_id_str] = cast(dict[PropertyIdentifier, PropertyValue], properties) |
254 | 265 | # Defaultdict is not yaml serializable
|
255 | 266 | return dict(props)
|
256 | 267 |
|
257 |
| - def items(self) -> ItemsView[ViewId, MutableMapping[PropertyIdentifier, PropertyValue]]: |
| 268 | + def items(self) -> ItemsView[SourceId, MutableMapping[PropertyIdentifier, PropertyValue]]: |
258 | 269 | return self.data.items()
|
259 | 270 |
|
260 |
| - def keys(self) -> KeysView[ViewId]: |
| 271 | + def keys(self) -> KeysView[SourceId]: |
261 | 272 | return self.data.keys()
|
262 | 273 |
|
263 | 274 | def values(self) -> ValuesView[MutableMapping[PropertyIdentifier, PropertyValue]]:
|
264 | 275 | return self.data.values()
|
265 | 276 |
|
266 |
| - def __iter__(self) -> Iterator[ViewId]: |
| 277 | + def __iter__(self) -> Iterator[SourceId]: |
267 | 278 | yield from self.keys()
|
268 | 279 |
|
269 |
| - def __getitem__(self, view: ViewIdentifier) -> MutableMapping[PropertyIdentifier, PropertyValue]: |
270 |
| - view_id = ViewId.load(view) |
271 |
| - return self.data.get(view_id, {}) |
| 280 | + def __getitem__(self, item: SourceIdentifier) -> MutableMapping[PropertyIdentifier, PropertyValue]: |
| 281 | + source_id = _load_source_id(item) |
| 282 | + return self.data.get(source_id, {}) |
272 | 283 |
|
273 | 284 | def __contains__(self, item: Any) -> bool:
|
274 |
| - view_id = ViewId.load(item) |
275 |
| - return view_id in self.data |
| 285 | + source_id = _load_source_id(item) |
| 286 | + return source_id in self.data |
276 | 287 |
|
277 | 288 | @overload
|
278 |
| - def get(self, view: ViewIdentifier) -> MutableMapping[PropertyIdentifier, PropertyValue] | None: ... |
| 289 | + def get(self, view: SourceIdentifier) -> MutableMapping[PropertyIdentifier, PropertyValue] | None: ... |
279 | 290 |
|
280 | 291 | @overload
|
281 | 292 | def get(
|
282 |
| - self, view: ViewIdentifier, default: MutableMapping[PropertyIdentifier, PropertyValue] | _T |
| 293 | + self, view: SourceIdentifier, default: MutableMapping[PropertyIdentifier, PropertyValue] | _T |
283 | 294 | ) -> MutableMapping[PropertyIdentifier, PropertyValue] | _T: ...
|
284 | 295 |
|
285 | 296 | def get(
|
286 | 297 | self,
|
287 |
| - view: ViewIdentifier, |
| 298 | + view: SourceIdentifier, |
288 | 299 | default: MutableMapping[PropertyIdentifier, PropertyValue] | None | _T | None = None,
|
289 | 300 | ) -> MutableMapping[PropertyIdentifier, PropertyValue] | None | _T:
|
290 |
| - view_id = ViewId.load(view) |
291 |
| - return self.data.get(view_id, default) |
| 301 | + source_id = _load_source_id(view) |
| 302 | + return self.data.get(source_id, default) |
292 | 303 |
|
293 | 304 | def __len__(self) -> int:
|
294 | 305 | return len(self.data)
|
295 | 306 |
|
296 |
| - def __delitem__(self, view: ViewIdentifier) -> None: |
297 |
| - view_id = ViewId.load(view) |
298 |
| - del self.data[view_id] |
| 307 | + def __delitem__(self, item: SourceIdentifier) -> None: |
| 308 | + source_id = _load_source_id(item) |
| 309 | + del self.data[source_id] |
299 | 310 |
|
300 |
| - def __setitem__(self, view: ViewIdentifier, properties: MutableMapping[PropertyIdentifier, PropertyValue]) -> None: |
301 |
| - view_id = ViewId.load(view) |
302 |
| - self.data[view_id] = properties |
| 311 | + def __setitem__( |
| 312 | + self, item: SourceIdentifier, properties: MutableMapping[PropertyIdentifier, PropertyValue] |
| 313 | + ) -> None: |
| 314 | + source_id = _load_source_id(item) |
| 315 | + self.data[source_id] = properties |
303 | 316 |
|
304 | 317 | def _repr_html_(self) -> str:
|
305 | 318 | pd = local_import("pandas")
|
|
0 commit comments