|
| 1 | +"""C4 PlantUML export for architecture diagrams. |
| 2 | +
|
| 3 | +Generates C4 Container diagrams using the C4-PlantUML library syntax. |
| 4 | +See: https://github.com/plantuml-stdlib/C4-PlantUML |
| 5 | +""" |
| 6 | + |
| 7 | +from scp_sdk import SCPManifest |
| 8 | + |
| 9 | + |
| 10 | +def export_c4(manifests: list[SCPManifest], title: str = "System Architecture") -> str: |
| 11 | + """Export manifests to a C4 PlantUML Container diagram. |
| 12 | +
|
| 13 | + Generates a C4 Container-level diagram with: |
| 14 | + - Internal systems as Container elements |
| 15 | + - External systems (tier 4-5 or unscanned dependencies) as System_Ext |
| 16 | + - Dependencies as Rel relationships with capability labels |
| 17 | +
|
| 18 | + Args: |
| 19 | + manifests: List of SCP manifests |
| 20 | + title: Diagram title |
| 21 | +
|
| 22 | + Returns: |
| 23 | + PlantUML C4 diagram string (.puml format) |
| 24 | + """ |
| 25 | + lines = [ |
| 26 | + "@startuml", |
| 27 | + "!include https://raw.githubusercontent.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml", |
| 28 | + "", |
| 29 | + f"title {title}", |
| 30 | + "", |
| 31 | + ] |
| 32 | + |
| 33 | + # Track all systems and their metadata |
| 34 | + systems: dict[str, dict] = {} |
| 35 | + # Track systems that are scanned (have full manifests) |
| 36 | + scanned_urns: set[str] = set() |
| 37 | + # Track dependencies |
| 38 | + dependencies: list[tuple[str, str, str, str]] = [] # (from, to, label, type) |
| 39 | + |
| 40 | + # First pass: collect all system info from manifests |
| 41 | + for manifest in manifests: |
| 42 | + urn = manifest.system.urn |
| 43 | + scanned_urns.add(urn) |
| 44 | + |
| 45 | + tier = ( |
| 46 | + manifest.system.classification.tier |
| 47 | + if manifest.system.classification |
| 48 | + else None |
| 49 | + ) |
| 50 | + domain = ( |
| 51 | + manifest.system.classification.domain |
| 52 | + if manifest.system.classification |
| 53 | + else None |
| 54 | + ) |
| 55 | + |
| 56 | + systems[urn] = { |
| 57 | + "name": manifest.system.name, |
| 58 | + "description": manifest.system.description or "", |
| 59 | + "tier": tier, |
| 60 | + "domain": domain, |
| 61 | + "is_external": tier is not None and tier >= 4, |
| 62 | + } |
| 63 | + |
| 64 | + # Collect dependencies |
| 65 | + if manifest.depends: |
| 66 | + for dep in manifest.depends: |
| 67 | + label = dep.capability or "uses" |
| 68 | + dep_type = dep.type or "" |
| 69 | + dependencies.append((urn, dep.system, label, dep_type)) |
| 70 | + |
| 71 | + # Add stub for unknown dependencies |
| 72 | + if dep.system not in systems: |
| 73 | + dep_name = dep.system.split(":")[-1].replace("-", " ").title() |
| 74 | + systems[dep.system] = { |
| 75 | + "name": dep_name, |
| 76 | + "description": "External system", |
| 77 | + "tier": None, |
| 78 | + "domain": None, |
| 79 | + "is_external": True, |
| 80 | + } |
| 81 | + |
| 82 | + # Group systems by domain for boundaries |
| 83 | + domains: dict[str, list[str]] = {} |
| 84 | + no_domain: list[str] = [] |
| 85 | + |
| 86 | + for urn, info in systems.items(): |
| 87 | + if info["is_external"]: |
| 88 | + continue # External systems go outside boundaries |
| 89 | + domain = info.get("domain") |
| 90 | + if domain: |
| 91 | + if domain not in domains: |
| 92 | + domains[domain] = [] |
| 93 | + domains[domain].append(urn) |
| 94 | + else: |
| 95 | + no_domain.append(urn) |
| 96 | + |
| 97 | + # Output external systems first |
| 98 | + external_urns = [urn for urn, info in systems.items() if info["is_external"]] |
| 99 | + if external_urns: |
| 100 | + lines.append("' External Systems") |
| 101 | + for urn in sorted(external_urns): |
| 102 | + info = systems[urn] |
| 103 | + alias = _urn_to_alias(urn) |
| 104 | + lines.append(f'System_Ext({alias}, "{info["name"]}", "{info["description"]}")') |
| 105 | + lines.append("") |
| 106 | + |
| 107 | + # Output internal systems grouped by domain |
| 108 | + if domains: |
| 109 | + for domain, urns in sorted(domains.items()): |
| 110 | + boundary_id = _sanitize_alias(domain) |
| 111 | + lines.append(f'System_Boundary({boundary_id}, "{domain.title()}") {{') |
| 112 | + for urn in sorted(urns): |
| 113 | + info = systems[urn] |
| 114 | + alias = _urn_to_alias(urn) |
| 115 | + tier_tag = f" [Tier {info['tier']}]" if info["tier"] else "" |
| 116 | + desc = info["description"] or f"Internal service{tier_tag}" |
| 117 | + lines.append(f' Container({alias}, "{info["name"]}", "", "{desc}")') |
| 118 | + lines.append("}") |
| 119 | + lines.append("") |
| 120 | + |
| 121 | + # Output systems without domain |
| 122 | + if no_domain: |
| 123 | + lines.append("' Internal Systems (no domain)") |
| 124 | + for urn in sorted(no_domain): |
| 125 | + info = systems[urn] |
| 126 | + alias = _urn_to_alias(urn) |
| 127 | + tier_tag = f" [Tier {info['tier']}]" if info["tier"] else "" |
| 128 | + desc = info["description"] or f"Internal service{tier_tag}" |
| 129 | + lines.append(f'Container({alias}, "{info["name"]}", "", "{desc}")') |
| 130 | + lines.append("") |
| 131 | + |
| 132 | + # Output relationships |
| 133 | + if dependencies: |
| 134 | + lines.append("' Relationships") |
| 135 | + for from_urn, to_urn, label, dep_type in dependencies: |
| 136 | + from_alias = _urn_to_alias(from_urn) |
| 137 | + to_alias = _urn_to_alias(to_urn) |
| 138 | + tech = f", {dep_type}" if dep_type else "" |
| 139 | + lines.append(f'Rel({from_alias}, {to_alias}, "{label}"{tech})') |
| 140 | + lines.append("") |
| 141 | + |
| 142 | + lines.append("@enduml") |
| 143 | + return "\n".join(lines) |
| 144 | + |
| 145 | + |
| 146 | +def _urn_to_alias(urn: str) -> str: |
| 147 | + """Convert a URN to a valid PlantUML alias.""" |
| 148 | + # Extract the service name and sanitize |
| 149 | + parts = urn.split(":") |
| 150 | + name = parts[-1] if parts else urn |
| 151 | + return name.replace("-", "_") |
| 152 | + |
| 153 | + |
| 154 | +def _sanitize_alias(text: str) -> str: |
| 155 | + """Convert text to a valid PlantUML alias.""" |
| 156 | + return text.replace("-", "_").replace(" ", "_").replace(".", "_").lower() |
0 commit comments