diff --git a/docker_compose_diagram/docker_images/auto_import_diagrams.py b/docker_compose_diagram/docker_images/auto_import_diagrams.py new file mode 100644 index 0000000..59da01f --- /dev/null +++ b/docker_compose_diagram/docker_images/auto_import_diagrams.py @@ -0,0 +1,75 @@ +from typing import Any, Type + +from diagrams import Node +from diagrams.onprem import _OnPrem + + +def import_on_prem_parent_nodes() -> list[Type[_OnPrem]]: + from diagrams.onprem.aggregator import _Aggregator + from diagrams.onprem.analytics import _Analytics + from diagrams.onprem.auth import _Auth + from diagrams.onprem.cd import _Cd + from diagrams.onprem.certificates import _Certificates + from diagrams.onprem.ci import _Ci + from diagrams.onprem.client import _Client + from diagrams.onprem.compute import _Compute + from diagrams.onprem.database import _Database + from diagrams.onprem.dns import _Dns + from diagrams.onprem.etl import _Etl + from diagrams.onprem.gitops import _Gitops + from diagrams.onprem.inmemory import _Inmemory + from diagrams.onprem.logging import _Logging + from diagrams.onprem.network import _Network + from diagrams.onprem.queue import _Queue + from diagrams.onprem.storage import _Storage + + return [ + _Database, + _Queue, + _Network, + _Inmemory, + _Storage, + _Logging, + _Auth, + _Client, + _Analytics, + _Aggregator, + _Certificates, + _Cd, + _Ci, + _Compute, + _Dns, + _Etl, + _Gitops, + ] + + +def _collect_subclasses( + diagrams_parent_class: Type[Node], base_class: Type[Any] +) -> list[Type[Any]]: + new_classes = [] + for diagram_child_class in diagrams_parent_class.__subclasses__(): + new_class = type( + f"{diagram_child_class.__name__}Image", + (base_class,), + { + "pattern": str(diagram_child_class.__name__).lower(), + "diagram_render_class": diagram_child_class, + }, + ) + new_classes.append(new_class) + + return new_classes + + +def register_all_icons_from_diagrams(base_class: Type[Any]) -> list[Type[Any]]: + """Collect""" + new_classes = [] + + for on_prem_diagrams_parent in import_on_prem_parent_nodes(): + database_classes = _collect_subclasses( + diagrams_parent_class=on_prem_diagrams_parent, base_class=base_class + ) + new_classes.extend(database_classes) + + return new_classes diff --git a/docker_compose_diagram/docker_images/patterns.py b/docker_compose_diagram/docker_images/patterns.py index dbdff4c..d6c3dab 100644 --- a/docker_compose_diagram/docker_images/patterns.py +++ b/docker_compose_diagram/docker_images/patterns.py @@ -1,3 +1,5 @@ +import abc + from diagrams.aws.database import Dynamodb from diagrams.aws.integration import SNS, SQS from diagrams.aws.storage import S3, Storage @@ -52,11 +54,15 @@ Typescript, ) +from docker_compose_diagram.docker_images.auto_import_diagrams import ( + register_all_icons_from_diagrams, +) + DEFAULT_ICON_CLASS = Rack -class DockerImagePattern: +class DockerImagePattern(abc.ABC): pattern = r"" diagram_render_class = None @@ -352,3 +358,6 @@ class CertificateImage(DockerImagePattern): class LoadBalancerImage(DockerImagePattern): pattern = r"^loadbalancer$" diagram_render_class = LoadBalancer + + +diagrams_classes = register_all_icons_from_diagrams(base_class=DockerImagePattern) diff --git a/embed_images.py b/embed_images.py index 25d4b5e..a2b7e11 100644 --- a/embed_images.py +++ b/embed_images.py @@ -26,4 +26,6 @@ def embed_images(svg_file, svg_file_out=None): if __name__ == "__main__": svg_file = sys.argv[1] if len(sys.argv) == 2 else "docker-compose.svg" - embed_images(svg_file) # outputs my_diagram_out.svg with base64 encoded data URLs for the images \ No newline at end of file + embed_images( + svg_file + ) # outputs my_diagram_out.svg with base64 encoded data URLs for the images diff --git a/pyproject.toml b/pyproject.toml index 9380139..230025f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "docker-compose-diagram" -version = "0.4.0" +version = "0.5.0" description = "" authors = ["Sergei Konik "]