# -*- coding: utf-8 -*-
"""
HTML Writer:
This code is adapted from https://github.com/SchrodingersGat/KiBoM by Oliver Henry Walters.
Generates a HTML file.
"""
from .columnlist import ColumnList
BG_GEN = "#E6FFEE"
BG_KICAD = "#FFE6B3"
BG_USER = "#E6F9FF"
BG_EMPTY = "#FF8080"
def bg_color(col):
""" Return a background color for a given column title """
col = col.lower()
# Auto-generated columns
if col in ColumnList.COLUMNS_GEN_L:
return BG_GEN
# KiCad protected columns
elif col in ColumnList.COLUMNS_PROTECTED_L:
return BG_KICAD
# Additional user columns
return BG_USER
def link(text):
for t in ["http", "https", "ftp", "www"]:
if text.startswith(t):
return '{t}'.format(t=text)
return text
def html_table(html, groups, headings, head_names, cfg, link_datasheet, link_digikey, dnf=False):
# Table start
html.write('
\n')
# Row titles:
html.write("\n")
for i, h in enumerate(head_names):
# Cell background color
bg = bg_color(headings[i])
html.write('\t| {h} | \n'.format(h=h, bg=' bgcolor="{}"'.format(bg) if bg else ''))
html.write("
\n")
row_count = 0
for i, group in enumerate(groups):
if (cfg.ignore_dnf and not group.is_fitted()) != dnf:
continue
row = group.get_row(headings)
row_count += 1
html.write("\n")
for n, r in enumerate(row):
# A link to Digi-Key?
if link_digikey and headings[n] in link_digikey:
r = '' + r + ''
# Link this column to the datasheet?
if link_datasheet and headings[n] == link_datasheet:
r = '' + r + ''
# Empty cell?
if len(r) == 0 or r.strip() == "~":
bg = BG_EMPTY
else:
bg = bg_color(headings[n])
html.write('\t| {val} | \n'.format(bg=' bgcolor={}'.format(bg) if bg else '', val=link(r)))
html.write("
\n")
html.write("
\n")
return row_count
def write_html(filename, groups, headings, head_names, cfg):
"""
Write BoM out to a HTML file
filename = path to output file (must be a .htm or .html file)
groups = [list of ComponentGroup groups]
headings = [list of headings to search for data in the BoM file]
head_names = [list of headings to display in the BoM file]
prefs = BomPref object
"""
link_datasheet = cfg.datasheet_as_link
link_digikey = None
if cfg.digikey_link:
# TODO avoid convert
link_digikey = cfg.digikey_link.split("\t")
with open(filename, "w") as html:
# HTML Header
html.write("\n")
html.write("\n")
html.write('\t\n') # UTF-8 encoding for unicode support
html.write("\n")
html.write("\n")
# PCB info
if not cfg.hide_headers:
html.write("KiBoM PCB Bill of Materials
\n")
if not cfg.hide_pcb_info:
html.write('\n')
html.write("| Source File | {} |
\n".format(cfg.source))
# html.write("| BoM Date | {date} |
\n".format(date=cfg.date)) Same as schematic
html.write("| Schematic Revision | {} |
\n".format(cfg.revision))
html.write("| Schematic Date | {} |
\n".format(cfg.date))
html.write("| PCB Variant | {} |
\n".format(', '.join(cfg.variant)))
# html.write("| KiCad Version | {version} |
\n".format(version=net.getTool())) TODO?
html.write("| Component Groups | {} |
\n".format(cfg.n_groups))
html.write("| Component Count (per PCB) | {} |
\n".format(cfg.n_total))
html.write("| Fitted Components (per PCB) | {} |
\n".format(cfg.n_fitted))
html.write("| Number of PCBs | {} |
\n".format(cfg.number))
html.write("Total Component Count (for {n} PCBs) | {t} |
\n".
format(n=cfg.number, t=cfg.n_build))
html.write("
\n")
html.write("
\n")
if not cfg.hide_headers:
html.write("Component Groups
\n")
html.write('KiCad Fields (default)
\n'.format(bg=BG_KICAD))
html.write('Generated Fields
\n'.format(bg=BG_GEN))
html.write('User Fields
\n'.format(bg=BG_USER))
html.write('Empty Fields
\n'.format(bg=BG_EMPTY))
# Fitted groups
row_count = html_table(html, groups, headings, head_names, cfg, link_datasheet, link_digikey)
html.write("
\n")
if cfg.html_generate_dnf and row_count != len(groups):
html.write("Optional components (DNF=Do Not Fit)
\n")
# DNF component groups
html_table(html, groups, headings, head_names, cfg, link_datasheet, link_digikey, True)
html.write("
\n")
html.write("")
return True