Last active
September 10, 2025 21:41
-
-
Save ftoledo/8c17113d30e847a5e69f92bf0bbab82c to your computer and use it in GitHub Desktop.
Script para crear un grafico dot de graphviz a a partir de un nodelist de fido de la Zona4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python3 | |
| import csv | |
| import sys | |
| if len(sys.argv) < 2: | |
| print("Uso: python3 node2dot.py <archivo_nodelist>") | |
| sys.exit(1) | |
| nodelist_file = sys.argv[1] | |
| zona = "4" | |
| nodes = {} | |
| node_types = {} | |
| edges = [] | |
| current_region = None | |
| current_host = None | |
| zona_found = False | |
| with open(nodelist_file, newline="", encoding="utf-8") as f: | |
| reader = csv.reader(f) | |
| for row in reader: | |
| if not row or row[0].startswith(";"): | |
| continue | |
| # Zona | |
| if row[0].startswith("Zone") and row[1] == zona: | |
| zona_found = True | |
| nodes[zona] = f"ZONA {zona}\n({row[4].replace('_',' ')})" | |
| node_types[zona] = "zona" | |
| continue | |
| if not zona_found: | |
| continue | |
| # Region | |
| if row[0].startswith("Region"): | |
| current_region = f"{zona}:{row[1]}" | |
| current_host = None # reiniciar host al entrar en región | |
| region_label = f"{current_region}\n({row[2].replace('_',' ')})" | |
| nodes[current_region] = region_label | |
| node_types[current_region] = "region" | |
| edges.append((zona, current_region)) | |
| continue | |
| # Host | |
| if row[0].startswith("Host"): | |
| current_host = f"{zona}:{row[1]}" | |
| host_label = f"{current_host}\n({row[2].replace('_',' ')})\n({row[4].replace('_',' ')})" | |
| nodes[current_host] = host_label | |
| node_types[current_host] = "host" | |
| # verificar si existe nodo /1 en esta región | |
| entry_node = f"{current_region}/1" | |
| if entry_node in nodes: | |
| edges.append((entry_node, current_host)) | |
| else: | |
| edges.append((current_region, current_host)) | |
| continue | |
| # Nodos (pueden depender de host o región) | |
| if row[0] in ("Pvt", "Hold") or row[0] == "": | |
| num = row[1] | |
| name = row[2].replace("_"," ") | |
| person = row[4].replace("_"," ") if len(row) > 4 else "" | |
| if current_host: | |
| node_addr = f"{current_host}/{num}" | |
| parent = current_host | |
| else: | |
| node_addr = f"{current_region}/{num}" | |
| parent = current_region | |
| label_lines = [f"{node_addr}", f"{name}"] | |
| if person: | |
| label_lines.append(f"({person})") | |
| node_label = "\n".join(label_lines) | |
| nodes[node_addr] = node_label | |
| if row[0] == "Pvt": | |
| node_types[node_addr] = "pvt" | |
| elif row[0] == "Hold": | |
| node_types[node_addr] = "hold" | |
| else: | |
| node_types[node_addr] = "pvt" | |
| edges.append((parent, node_addr)) | |
| # Colores pastel | |
| color_map = { | |
| "zona": "lightblue", | |
| "region": "palegreen", | |
| "host": "lightpink", | |
| "pvt": "lightyellow", | |
| "hold": "lightcoral" | |
| } | |
| print("<graphviz>") | |
| print("digraph NODOS {") | |
| print("node [shape=tab, style=filled]") | |
| print("rankdir=LR\n") | |
| for addr, label in nodes.items(): | |
| node_type = node_types.get(addr, "pvt") | |
| fill = color_map.get(node_type, "lightyellow") | |
| print(f'"{addr}" [label="{label}", fillcolor={fill}]') | |
| for parent, child in edges: | |
| print(f'"{parent}" -> "{child}"') | |
| print("}") | |
| print("</graphviz>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment