import yaml #import cairosvg from weasyprint import HTML, CSS import jinja2 from pathlib import Path import networkx as nx import matplotlib.pyplot as plt def graph(roles): g = nx.Graph() g.add_nodes_from([str(role["num"]) for role in roles]) for role in roles: for lien in role["liens"]: g.add_edge(str(role["num"]), str(list(lien.keys())[0])) return g def build_topo(topo, roles): plt.figure(figsize=(2,2)) ax = plt.gca() #ax.set_title(f"Forme du réseau: {topo}") g = graph(roles) nx.draw(g, node_color='white', node_size=100, ax=ax) _ = ax.axis('off') plt.margins(0.2) plt.subplots_adjust(left=0, right=1, top=1, bottom=0) plt.savefig(f"{topo}/forme.png", transparent=True, ) plt.cla() def role_card(role, topo, template, css): card = template.render(topo=topo, **role) Path(topo).mkdir(exist_ok=True) dest = f"{topo}/role{role['num']}.html" with open(dest, "w") as f: f.write(card) #cairosvg.svg2pdf(url=dest, write_to=dest.replace("svg", "pdf")) HTML(dest).write_pdf(dest.replace("html", "pdf"), stylesheets=[css]) with open("roles.yml", "r") as f: topos = yaml.load(f, Loader=yaml.SafeLoader) with open("role.html", "r") as f: template = jinja2.Template(f.read(), undefined=jinja2.StrictUndefined) css = CSS("role.css") for topo, desc in topos.items(): build_topo(topo, desc["roles"]) for role in desc["roles"]: role_card(role, topo, template, css)