|
| 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 | + ) |
0 commit comments