17
17
18
18
19
19
from itertools import chain
20
- from typing import TYPE_CHECKING , Iterable , List , Optional , Set
20
+ from typing import TYPE_CHECKING , Iterable , List , Optional
21
21
22
22
from pydantic import Field
23
23
@@ -91,19 +91,71 @@ def __init__(
91
91
) -> None :
92
92
"""Initialize a view set."""
93
93
super ().__init__ (** kwargs )
94
- # self.enterprise_context_views = set(enterprise_context_views)
95
- self .system_landscape_views : Set [SystemLandscapeView ] = set (
96
- system_landscape_views
94
+ all_views = chain (
95
+ system_landscape_views ,
96
+ system_context_views ,
97
+ container_views ,
98
+ component_views ,
99
+ deployment_views ,
100
+ dynamic_views ,
101
+ filtered_views ,
97
102
)
98
- self .system_context_views : Set [SystemContextView ] = set (system_context_views )
99
- self .container_views : Set [ContainerView ] = set (container_views )
100
- self .component_views : Set [ComponentView ] = set (component_views )
101
- self .deployment_views : Set [DeploymentView ] = set (deployment_views )
102
- self .dynamic_views : Set [DynamicView ] = set (dynamic_views )
103
- self .filtered_views : Set [FilteredView ] = set (filtered_views )
103
+ self ._views = {view .key : view for view in all_views }
104
104
self .configuration = Configuration () if configuration is None else configuration
105
105
self .set_model (model )
106
106
107
+ @property
108
+ def system_landscape_views (self ) -> Iterable [SystemLandscapeView ]:
109
+ """Return the SystemLandscapeViews in this ViewSet."""
110
+ return (
111
+ view
112
+ for view in self ._views .values ()
113
+ if isinstance (view , SystemLandscapeView )
114
+ )
115
+
116
+ @property
117
+ def system_context_views (self ) -> Iterable [SystemContextView ]:
118
+ """Return the SystemContextViews in this ViewSet."""
119
+ return (
120
+ view for view in self ._views .values () if isinstance (view , SystemContextView )
121
+ )
122
+
123
+ @property
124
+ def container_views (self ) -> Iterable [ContainerView ]:
125
+ """Return the ContainerViews in this ViewSet."""
126
+ return (
127
+ view for view in self ._views .values () if isinstance (view , ContainerView )
128
+ )
129
+
130
+ @property
131
+ def component_views (self ) -> Iterable [ComponentView ]:
132
+ """Return the CompoentViews in this ViewSet."""
133
+ return (
134
+ view for view in self ._views .values () if isinstance (view , ComponentView )
135
+ )
136
+
137
+ @property
138
+ def deployment_views (self ) -> Iterable [DeploymentView ]:
139
+ """Return the DeploymentViews in this ViewSet."""
140
+ return (
141
+ view for view in self ._views .values () if isinstance (view , DeploymentView )
142
+ )
143
+
144
+ @property
145
+ def dynamic_views (self ) -> Iterable [DynamicView ]:
146
+ """Return the DynamicViews in this ViewSet."""
147
+ return (view for view in self ._views .values () if isinstance (view , DynamicView ))
148
+
149
+ @property
150
+ def filtered_views (self ) -> Iterable [FilteredView ]:
151
+ """Return the FilteredViews in this ViewSet."""
152
+ return (view for view in self ._views .values () if isinstance (view , FilteredView ))
153
+
154
+ @property
155
+ def all_views (self ) -> Iterable [AbstractView ]:
156
+ """Return all the views in this ViewSet."""
157
+ return self ._views .values ()
158
+
107
159
@classmethod
108
160
def hydrate (cls , views : ViewSetIO , model : "Model" ) -> "ViewSet" :
109
161
"""Hydrate a new ViewSet instance from its IO."""
@@ -187,6 +239,9 @@ def _hydrate_view(cls, view: View, model: "Model") -> None:
187
239
relationship_view .id
188
240
)
189
241
242
+ def _add_view (self , view : View ) -> None :
243
+ self ._views [view .key ] = view
244
+
190
245
def create_system_landscape_view (
191
246
self , system_landscape_view : Optional [SystemLandscapeView ] = None , ** kwargs
192
247
) -> SystemLandscapeView :
@@ -205,7 +260,7 @@ def create_system_landscape_view(
205
260
)
206
261
self ._ensure_key_is_specific_and_unique (system_landscape_view .key )
207
262
system_landscape_view .set_viewset (self )
208
- self .system_landscape_views . add (system_landscape_view )
263
+ self ._add_view (system_landscape_view )
209
264
return system_landscape_view
210
265
211
266
def create_system_context_view (
@@ -226,7 +281,7 @@ def create_system_context_view(
226
281
system_context_view = SystemContextView (** kwargs )
227
282
self ._ensure_key_is_specific_and_unique (system_context_view .key )
228
283
system_context_view .set_viewset (self )
229
- self .system_context_views . add (system_context_view )
284
+ self ._add_view (system_context_view )
230
285
return system_context_view
231
286
232
287
def create_container_view (
@@ -247,7 +302,7 @@ def create_container_view(
247
302
container_view = ContainerView (** kwargs )
248
303
self ._ensure_key_is_specific_and_unique (container_view .key )
249
304
container_view .set_viewset (self )
250
- self .container_views . add (container_view )
305
+ self ._add_view (container_view )
251
306
return container_view
252
307
253
308
def create_component_view (
@@ -266,7 +321,7 @@ def create_component_view(
266
321
component_view = ComponentView (** kwargs )
267
322
self ._ensure_key_is_specific_and_unique (component_view .key )
268
323
component_view .set_viewset (self )
269
- self .component_views . add (component_view )
324
+ self ._add_view (component_view )
270
325
return component_view
271
326
272
327
def create_deployment_view (self , ** kwargs ) -> DeploymentView :
@@ -280,7 +335,7 @@ def create_deployment_view(self, **kwargs) -> DeploymentView:
280
335
self ._ensure_key_is_specific_and_unique (deployment_view .key )
281
336
deployment_view .set_viewset (self )
282
337
deployment_view .set_model (self .model )
283
- self .deployment_views . add (deployment_view )
338
+ self ._add_view (deployment_view )
284
339
return deployment_view
285
340
286
341
def create_dynamic_view (self , ** kwargs ) -> DynamicView :
@@ -294,7 +349,7 @@ def create_dynamic_view(self, **kwargs) -> DynamicView:
294
349
self ._ensure_key_is_specific_and_unique (dynamic_view .key )
295
350
dynamic_view .set_viewset (self )
296
351
dynamic_view .set_model (self .model )
297
- self .dynamic_views . add (dynamic_view )
352
+ self ._add_view (dynamic_view )
298
353
return dynamic_view
299
354
300
355
def create_filtered_view (self , ** kwargs ) -> FilteredView :
@@ -307,100 +362,34 @@ def create_filtered_view(self, **kwargs) -> FilteredView:
307
362
filtered_view = FilteredView (** kwargs )
308
363
self ._ensure_key_is_specific_and_unique (filtered_view .key )
309
364
filtered_view .set_viewset (self )
310
- self .filtered_views . add (filtered_view )
365
+ self ._add_view (filtered_view )
311
366
return filtered_view
312
367
313
368
def get_view (self , key : str ) -> Optional [AbstractView ]:
314
369
"""Return the view with the given key, or None."""
315
- all_views = chain (
316
- self .system_landscape_views ,
317
- self .system_context_views ,
318
- self .container_views ,
319
- self .component_views ,
320
- self .deployment_views ,
321
- self .dynamic_views ,
322
- self .filtered_views ,
323
- )
324
- return next ((view for view in all_views if view .key == key ), None )
370
+ return self ._views .get (key )
325
371
326
372
def __getitem__ (self , key : str ) -> AbstractView :
327
373
"""Return the view with the given key or raise a KeyError."""
328
- result = self .get_view (key )
374
+ result = self ._views . get (key )
329
375
if not result :
330
376
raise KeyError (f"No view with key '{ key } ' in ViewSet" )
331
377
return result
332
378
333
379
def copy_layout_information_from (self , source : "ViewSet" ) -> None :
334
380
"""Copy all the layout information from a source ViewSet."""
335
381
336
- # Note that filtered views don't have any layout information to copy.
337
- for source_view in source .system_landscape_views :
338
- destination_view = self ._find_system_landscape_view (source_view )
339
- if destination_view :
340
- destination_view .copy_layout_information_from (source_view )
341
-
342
- for source_view in source .system_context_views :
343
- destination_view = self ._find_system_context_view (source_view )
344
- if destination_view :
345
- destination_view .copy_layout_information_from (source_view )
346
-
347
- for source_view in source .container_views :
348
- destination_view = self ._find_container_view (source_view )
349
- if destination_view :
350
- destination_view .copy_layout_information_from (source_view )
351
-
352
- for source_view in source .component_views :
353
- destination_view = self ._find_component_view (source_view )
354
- if destination_view :
355
- destination_view .copy_layout_information_from (source_view )
356
-
357
- for source_view in source .dynamic_views :
358
- destination_view = self ._find_dynamic_view (source_view )
359
- if destination_view :
360
- destination_view .copy_layout_information_from (source_view )
361
-
362
- for source_view in source .deployment_views :
363
- destination_view = self ._find_deployment_view (source_view )
364
- if destination_view :
382
+ for source_view in source .all_views :
383
+ destination_view = self .get_view (source_view .key )
384
+ if (
385
+ destination_view
386
+ and isinstance (destination_view , View )
387
+ and type (destination_view ) is type (source_view )
388
+ ):
365
389
destination_view .copy_layout_information_from (source_view )
366
390
367
391
def _ensure_key_is_specific_and_unique (self , key : str ) -> None :
368
392
if key is None or key == "" :
369
393
raise ValueError ("A key must be specified." )
370
394
if self .get_view (key ) is not None :
371
395
raise ValueError (f"View already exists in workspace with key '{ key } '." )
372
-
373
- def _find_system_landscape_view (
374
- self , view : SystemLandscapeView
375
- ) -> Optional [SystemLandscapeView ]:
376
- for current_view in self .system_landscape_views :
377
- if view .key == current_view .key :
378
- return current_view
379
-
380
- def _find_system_context_view (
381
- self ,
382
- view : SystemContextView ,
383
- ) -> Optional [SystemContextView ]:
384
- for current_view in self .system_context_views :
385
- if view .key == current_view .key :
386
- return current_view
387
-
388
- def _find_container_view (self , view : ContainerView ) -> Optional [ContainerView ]:
389
- for current_view in self .container_views :
390
- if view .key == current_view .key :
391
- return current_view
392
-
393
- def _find_component_view (self , view : ComponentView ) -> Optional [ComponentView ]:
394
- for current_view in self .component_views :
395
- if view .key == current_view .key :
396
- return current_view
397
-
398
- def _find_dynamic_view (self , view : DynamicView ) -> Optional [DynamicView ]:
399
- for current_view in self .dynamic_views :
400
- if view .key == current_view .key :
401
- return current_view
402
-
403
- def _find_deployment_view (self , view : DeploymentView ) -> DeploymentView :
404
- for current_view in self .deployment_views :
405
- if view .key == current_view .key :
406
- return current_view
0 commit comments