Skip to content

Commit 87a6f34

Browse files
ilaifMidnighter
andcommitted
feat: add a container view
Co-authored-by: Midnighter <[email protected]>
1 parent c039f78 commit 87a6f34

File tree

2 files changed

+115
-4
lines changed

2 files changed

+115
-4
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright (c) 2020, Moritz E. Beber.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
"""Provide a container view."""
17+
18+
19+
from typing import Union
20+
21+
from pydantic import Field
22+
23+
from ..model import Container, Element, Person, SoftwareSystem
24+
from .static_view import StaticView, StaticViewIO
25+
26+
27+
__all__ = ("ContainerView", "ContainerViewIO")
28+
29+
30+
class ContainerViewIO(StaticViewIO):
31+
"""
32+
Represent the container view from the C4 model.
33+
34+
Attributes:
35+
external_software_system_boundary_visible (bool): Determines whether software
36+
system boundaries should be visible for "external" containers (those outside
37+
the software system in scope).
38+
39+
"""
40+
41+
external_software_system_boundary_visible: bool = Field(
42+
default=True, alias="externalSoftwareSystemBoundariesVisible"
43+
)
44+
45+
46+
class ContainerView(StaticView):
47+
"""
48+
Represent the container view from the C4 model.
49+
``
50+
Attributes:
51+
external_software_system_boundary_visible (bool):
52+
53+
"""
54+
55+
def __init__(
56+
self, *, external_software_system_boundary_visible: bool = True, **kwargs
57+
) -> None:
58+
"""Initialize a container view."""
59+
super().__init__(**kwargs)
60+
self.external_software_system_boundary_visible = (
61+
external_software_system_boundary_visible
62+
)
63+
64+
def add(
65+
self,
66+
static_element: Union[Person, SoftwareSystem, Container],
67+
add_relationships: bool = True,
68+
) -> None:
69+
"""
70+
Add the given person, software system, or container to this view.
71+
72+
Args:
73+
static_element (Person, SoftwareSystem or Container): The static element
74+
to add to this view.
75+
add_relationships (bool, optional): Whether to include all of the static
76+
element's relationships with other elements (default `True`).
77+
78+
"""
79+
return self._add_element(static_element, add_relationships=True)
80+
81+
def add_all_elements(self) -> None:
82+
"""Add all people, software systems, and containers to this view."""
83+
self.add_all_people()
84+
self.add_all_software_systems()
85+
self.add_all_containers()
86+
87+
def add_all_containers(self) -> None:
88+
for container in self.software_system.containers:
89+
self.add(container)
90+
91+
def add_nearest_neighbours(self, element: Element, _=None) -> None:
92+
super().add_nearest_neighbours(element, SoftwareSystem)
93+
super().add_nearest_neighbours(element, Person)
94+
super().add_nearest_neighbours(element, Container)
95+
96+
@classmethod
97+
def hydrate(cls, container_view_io: ContainerViewIO) -> "ContainerView":
98+
""""""
99+
return cls(
100+
external_software_system_boundary_visible=(
101+
container_view_io.external_software_system_boundary_visible
102+
),
103+
description=container_view_io.description,
104+
key=container_view_io.key,
105+
# TODO: Need an instance here or get an instance from the model by
106+
# reference.
107+
# software_system=container_view_io.software_system,
108+
)

src/structurizr/view/static_view.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818

1919
from abc import ABC, abstractmethod
20-
from typing import Iterable, List, Optional, Union
20+
from typing import Iterable, List, Optional, Type, Union
2121

22-
from ..model import Element, Person, SoftwareSystem
22+
from ..model import Container, Element, Person, SoftwareSystem
2323
from .animation import Animation, AnimationIO
2424
from .view import View, ViewIO
2525

@@ -93,11 +93,14 @@ def add_all_software_systems(self) -> None:
9393
for system in self.model.software_systems:
9494
self.add(system)
9595

96-
def add_nearest_neighbours(self, element: Element,) -> None:
96+
def add_nearest_neighbours(
97+
self,
98+
element: Element,
99+
element_type: Union[Type[Person], Type[SoftwareSystem], Type[Container]],
100+
) -> None:
97101
"""Add all permitted elements from a model to this view."""
98102
self._add_element(element, True)
99103

100-
element_type = type(element)
101104
# TODO(ilaif): @midnighter - Should we move to @property instead
102105
# of get_X()? More pythonic.
103106
# (midnighter): Probably yes.

0 commit comments

Comments
 (0)