Skip to content

Commit

Permalink
Include supplier in BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
russss committed Apr 4, 2024
1 parent 5b3e91d commit 969b35f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
35 changes: 30 additions & 5 deletions powerplan/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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"])
Expand Down
11 changes: 6 additions & 5 deletions powerplan/templates/bom.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ <h2>Equipment</h2>
<th>Used</th>
</tr>
</thead>
{% for type, used in nodes|dictsort -%}
{% for node in nodes -%}
<tr>
<td>{{used|length}}</td>
<td>{{type[0]}}</td>
<td>{{type[1]}}</td>
<td>{{used|sort|join(", ")}}</td>
<td>{{node['uses']|length}}</td>
<td>{{node['type']}}</td>
<td>{{node['model']}}</td>
<td>{{node['supplier']}}</td>
<td>{{node['uses']|sort|join(", ")}}</td>
</tr>
{% endfor -%}
</table>
Expand Down

0 comments on commit 969b35f

Please sign in to comment.