|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright (c) 2026 Salvador E. Tropea |
| 3 | +# Copyright (c) 2026 Instituto Nacional de Tecnología Industrial |
| 4 | +# License: AGPL |
| 5 | +# Project: KiBot (formerly KiPlot) |
| 6 | +""" |
| 7 | +JSON Writer: Generates a JSON BoM file. |
| 8 | +""" |
| 9 | +import json |
| 10 | +import urllib.parse |
| 11 | + |
| 12 | +from .columnlist import ColumnList |
| 13 | +from .html_writer import cell_class |
| 14 | +from .. import log |
| 15 | +logger = log.get_logger() |
| 16 | + |
| 17 | + |
| 18 | +def write_stats(data, cfg): |
| 19 | + multi = len(cfg.aggregate) > 1 |
| 20 | + stats = {} |
| 21 | + stats['variant'] = cfg.variant.name if cfg.variant else 'Default' |
| 22 | + stats['kicad_version'] = cfg.kicad_version |
| 23 | + stats['component_groups'] = cfg.n_groups |
| 24 | + stats['component_count'] = cfg.total_str |
| 25 | + stats['fitted_components'] = cfg.fitted_str |
| 26 | + stats['number_of_pcbs'] = cfg.number |
| 27 | + stats['total_components'] = cfg.n_build |
| 28 | + prjs = [] |
| 29 | + for prj in cfg.aggregate: |
| 30 | + d = {} |
| 31 | + d['schematic'] = prj.name |
| 32 | + d['revision'] = prj.sch.revision |
| 33 | + d['date'] = prj.sch.date |
| 34 | + if prj.sch.company: |
| 35 | + d['company'] = prj.sch.company |
| 36 | + if prj.ref_id: |
| 37 | + d['id'] = prj.ref_id |
| 38 | + if multi: |
| 39 | + d['component_groups'] = prj.comp_groups |
| 40 | + d['component_count'] = prj.total_str |
| 41 | + d['fitted_components'] = prj.fitted_str |
| 42 | + d['number_of_pcbs'] = prj.number |
| 43 | + d['total_components'] = prj.comp_build |
| 44 | + prjs.append(d) |
| 45 | + stats['projects'] = prjs |
| 46 | + data['stats'] = stats |
| 47 | + |
| 48 | + |
| 49 | +def write_json(filename, groups, headings, head_names, cfg): |
| 50 | + """ |
| 51 | + Write BoM out to a JSON file |
| 52 | + filename = path to output file (should be a .json) |
| 53 | + groups = [list of ComponentGroup groups] |
| 54 | + headings = [list of headings to search for data in the BoM file] |
| 55 | + head_names = [list of headings to display in the BoM file] |
| 56 | + cfg = BoMOptions object with all the configuration |
| 57 | + """ |
| 58 | + # TODO: Copiar el logo si es que no esta dentro del "output_dir" |
| 59 | + # Si esta vacio generar el nuestro y ponerle ese nombre |
| 60 | + link_datasheet = -1 |
| 61 | + if cfg.json.datasheet_as_link and cfg.json.datasheet_as_link in headings: |
| 62 | + link_datasheet = headings.index(cfg.json.datasheet_as_link) |
| 63 | + link_digikey = cfg.json.digikey_link |
| 64 | + link_mouser = cfg.json.mouser_link |
| 65 | + link_lcsc = cfg.json.lcsc_link |
| 66 | + hl_empty = cfg.json.highlight_empty |
| 67 | + |
| 68 | + data = {'title': cfg.json.title, 'logo': cfg.json.logo, 'logo_width': cfg.json.logo_width} |
| 69 | + write_stats(data, cfg) |
| 70 | + |
| 71 | + # Headings and where the data came from |
| 72 | + data['headings'] = [{'name': h, 'class': cell_class(f)} for h, f in zip(head_names, headings)] |
| 73 | + |
| 74 | + # User defined strings |
| 75 | + if cfg.json.extra_info: |
| 76 | + data['extra_info'] = cfg.json.extra_info |
| 77 | + |
| 78 | + # We use this code twice (regular + DNF) so I encapsulated it in a function ... the Pythonic way |
| 79 | + def do_groups(dnf=False): |
| 80 | + rows = [] |
| 81 | + for group in groups: |
| 82 | + if (cfg.ignore_dnf and not group.is_fitted()) != dnf: |
| 83 | + continue |
| 84 | + row = group.get_row(headings) |
| 85 | + if link_datasheet != -1: |
| 86 | + datasheet = group.get_field(ColumnList.COL_DATASHEET_L) |
| 87 | + cells = [] |
| 88 | + for n, r in enumerate(row): |
| 89 | + cell = {'value': r} |
| 90 | + field = headings[n] |
| 91 | + # |
| 92 | + # Solve any link |
| 93 | + # |
| 94 | + # A link to Digi-Key? |
| 95 | + if link_digikey and field in link_digikey and r: |
| 96 | + cell['link'] = 'https://www.digikey.com/en/products/result?keywords=' + urllib.parse.quote(r) |
| 97 | + if link_mouser and field in link_mouser and r: |
| 98 | + cell['link'] = 'https://www.mouser.com/ProductDetail/' + r |
| 99 | + if link_lcsc and field in link_lcsc and r: |
| 100 | + cell['link'] = 'https://www.lcsc.com/product-detail/' + r + '.html' |
| 101 | + # Link this column to the datasheet? |
| 102 | + if link_datasheet == n and datasheet.startswith('http') and r: |
| 103 | + cell['link'] = datasheet |
| 104 | + # |
| 105 | + # Color hint |
| 106 | + # |
| 107 | + # Empty cell? |
| 108 | + if hl_empty and ((len(r) == 0 and field not in group.fields_with_tilde) or r.strip() == "~"): |
| 109 | + cell['empty'] = True |
| 110 | + # Add the cell |
| 111 | + cells.append(cell) |
| 112 | + rows.append(cells) |
| 113 | + return rows |
| 114 | + # End of helper function "do_groups" |
| 115 | + |
| 116 | + data['rows'] = do_groups() |
| 117 | + # DNF component groups |
| 118 | + if cfg.json.generate_dnf and cfg.n_total != cfg.n_fitted: |
| 119 | + data['rows_dnf'] = do_groups(True) |
| 120 | + |
| 121 | + with open(filename, "wt") as output: |
| 122 | + text = json.dumps(data, sort_keys=True, indent=2) |
| 123 | + output.write(text) |
| 124 | + |
| 125 | + return True |
0 commit comments