From 969b35f55a25fa90ae8527a045386c466aba39ca Mon Sep 17 00:00:00 2001 From: Russ Garrett Date: Thu, 4 Apr 2024 20:27:54 +0100 Subject: [PATCH] Include supplier in BOM --- powerplan/bom.py | 35 ++++++++++++++++++++++++++++++----- powerplan/templates/bom.html | 11 ++++++----- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/powerplan/bom.py b/powerplan/bom.py index 4ea47d4..e622499 100644 --- a/powerplan/bom.py +++ b/powerplan/bom.py @@ -3,6 +3,7 @@ import csv from collections import defaultdict from typing import TYPE_CHECKING, TextIO +from .data import Generator, Distro from jinja2 import Environment, PackageLoader, select_autoescape @@ -16,9 +17,31 @@ def generate_bom(plan: Plan): + if plan.spec is None: + raise ValueError("Plan has no spec") + node_types = defaultdict(list) for node in plan.nodes(): - node_types[(type(node).__name__, node.type)].append(node.name) + node_types[(type(node), node.type)].append(node.name) + + node_data: list[dict] = [] + + for (node_type, node_model), nodes in node_types.items(): + if node_type is Generator: + spec = plan.spec.generator[node_model] + elif node_type is Distro: + spec = plan.spec.distro[node_model] + else: + raise ValueError(f"Unknown node type: {type(node)}") + + node_data.append( + { + "type": node_type.__name__, + "model": node_model, + "uses": sorted(nodes), + "supplier": spec["supplier"], + } + ) edge_types = defaultdict(list) @@ -30,7 +53,7 @@ def generate_bom(plan: Plan): f"{u.name} -> {v.name}" ) - return node_types, edge_types + return node_data, edge_types def generate_bom_html(plan: Plan): @@ -42,9 +65,11 @@ def generate_bom_html(plan: Plan): def generate_bom_csvs(plan: Plan, distros_file: TextIO, cables_file: TextIO): nodes, edges = generate_bom(plan) distros_writer = csv.writer(distros_file) - distros_writer.writerow(["type", "part", "count"]) - for node_type, used in nodes.items(): - distros_writer.writerow([node_type[0], node_type[1], len(used)]) + distros_writer.writerow(["supplier", "type", "part", "count"]) + for node in nodes: + distros_writer.writerow( + [node["supplier"], node["type"], node["model"], len(node["uses"])] + ) cables_writer = csv.writer(cables_file) cables_writer.writerow(["I", "phases", "length", "count"]) diff --git a/powerplan/templates/bom.html b/powerplan/templates/bom.html index 323e522..2521ae5 100644 --- a/powerplan/templates/bom.html +++ b/powerplan/templates/bom.html @@ -13,12 +13,13 @@

Equipment

Used - {% for type, used in nodes|dictsort -%} + {% for node in nodes -%} - {{used|length}} - {{type[0]}} - {{type[1]}} - {{used|sort|join(", ")}} + {{node['uses']|length}} + {{node['type']}} + {{node['model']}} + {{node['supplier']}} + {{node['uses']|sort|join(", ")}} {% endfor -%}