Added datasheet and digikey link tests for XLSX

This commit is contained in:
SET 2020-08-12 15:40:47 -03:00
parent 56c30f4227
commit 768d67d0ad
4 changed files with 120 additions and 5 deletions

View File

@ -9,8 +9,8 @@ Tests of Internal BoM files
- XLSX
- Components units
- Sort and groups of RLC_sort
- Datasheet as link
- Digi-Key link
- Datasheet as link (HTML and XLSX)
- Digi-Key link (HTML and XLSX)
- Join columns
- ignore_dnf = 0
- html_generate_dnf = 0
@ -36,7 +36,6 @@ Missing:
- hide_pcb_info
- various boards
- stats info
- datasheet and digikey for XLSX
- XLSX colors
@ -76,6 +75,7 @@ KIBOM_TEST_COMPONENTS_ALT = ['C1-C4', 'R9-R10', 'R7', 'R8', 'R1-R5']
KIBOM_TEST_COMPONENTS_ALT2 = ['C1-C4', 'R9-R10', 'R7', 'R8', 'R1-R2', 'R4-R5', 'R3']
KIBOM_TEST_EXCLUDE = ['R6']
KIBOM_TEST_GROUPS = 5
LINK_HEAD = ['References', 'Part', 'Value', 'Quantity Per PCB', 'digikey#', 'manf#']
LINKS_COMPONENTS = ['J1', 'J2', 'R1']
LINKS_EXCLUDE = ['C1']
LINKS_GROUPS = 2
@ -280,7 +280,7 @@ def test_int_bom_datasheet_link():
assert len(headers) == 2
# Test both tables has the same headings and they are the expected
assert headers[0] == headers[1]
assert headers[0] == ['References', 'Part', 'Value', 'Quantity Per PCB', 'digikey#', 'manf#']
assert headers[0] == LINK_HEAD
# Look for reference and quantity columns
ref_column = headers[0].index(REF_COLUMN_NAME)
part_column = headers[0].index('Part')
@ -309,7 +309,7 @@ def test_int_bom_digikey_link():
assert len(headers) == 2
# Test both tables has the same headings and they are the expected
assert headers[0] == headers[1]
assert headers[0] == ['References', 'Part', 'Value', 'Quantity Per PCB', 'digikey#', 'manf#']
assert headers[0] == LINK_HEAD
# Look for reference and quantity columns
ref_column = headers[0].index(REF_COLUMN_NAME)
dk_column = headers[0].index('digikey#')
@ -788,3 +788,55 @@ def test_int_bom_simple_xlsx_a():
prj = 'kibom-test'
ctx = context.TestContextSCH('test_int_bom_simple_xlsx_a', prj, 'int_bom_simple_xlsx_a', BOM_DIR)
simple_xlsx_verify(ctx, prj, False)
def test_int_bom_datasheet_link_xlsx():
prj = 'links'
ext = 'xlsx'
ctx = context.TestContextSCH('test_int_bom_datasheet_link_xlsx', prj, 'int_bom_datasheet_link_xlsx', BOM_DIR)
ctx.run()
out = prj + '.' + ext
rows, headers, sh_head = ctx.load_xlsx(out)
assert headers == LINK_HEAD
# Look for reference and quantity columns
ref_column = headers.index(REF_COLUMN_NAME)
part_column = headers.index('Part')
# Check the normal table
check_kibom_test_netlist(rows, ref_column, LINKS_GROUPS, LINKS_EXCLUDE, LINKS_COMPONENTS)
rows2, headers, sh_head = ctx.load_xlsx(out, 2)
assert headers == LINK_HEAD
# Check the DNF table
check_kibom_test_netlist(rows2, ref_column, 1, LINKS_COMPONENTS, LINKS_EXCLUDE)
# Check the datasheet link
parts = get_column(rows+rows2, part_column, False)
for c in parts:
assert c.strip().startswith('<a href')
assert 'pdf' in c
logging.debug(c + ' OK')
ctx.clean_up()
def test_int_bom_digikey_link_xlsx():
prj = 'links'
ext = 'xlsx'
ctx = context.TestContextSCH('test_int_bom_digikey_link_xlsx', prj, 'int_bom_digikey_link_xlsx', BOM_DIR)
ctx.run()
out = prj + '.' + ext
rows, headers, sh_head = ctx.load_xlsx(out)
assert headers == LINK_HEAD
# Look for reference and quantity columns
ref_column = headers.index(REF_COLUMN_NAME)
dk_column = headers.index('digikey#')
# Check the normal table
check_kibom_test_netlist(rows, ref_column, LINKS_GROUPS, LINKS_EXCLUDE, LINKS_COMPONENTS)
rows2, headers, sh_head = ctx.load_xlsx(out, 2)
assert headers == LINK_HEAD
# Check the DNF table
check_kibom_test_netlist(rows2, ref_column, 1, LINKS_COMPONENTS, LINKS_EXCLUDE)
# Check the datasheet link
parts = get_column(rows+rows2, dk_column, False)
for c in parts:
assert c.strip().startswith('<a href')
assert 'digikey' in c
logging.debug(c + ' OK')
ctx.clean_up()

View File

@ -410,6 +410,7 @@ class TestContext(object):
root = ET.parse(worksheet).getroot()
ns = '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}'
rnum = 1
rfirst = 1
sh_head = []
for r in root.iter(ns+'row'):
rcur = int(r.attrib['r'])
@ -418,6 +419,7 @@ class TestContext(object):
# Discard the sheet header
rows = []
rnum = rcur
rfirst = rcur
this_row = []
for cell in r.iter(ns+'c'):
if 't' in cell.attrib:
@ -434,6 +436,11 @@ class TestContext(object):
this_row.append(value)
rows.append(this_row)
rnum += 1
# Links are "Relationship"s
links = {}
nr = '{http://schemas.openxmlformats.org/officeDocument/2006/relationships}'
for r in root.find(ns+'hyperlinks').iter(ns+'hyperlink'):
links[r.attrib['ref']] = r.attrib[nr+'id']
# Read the strings
strings = self.get_out_path(os.path.join('desc', 'xl', 'sharedStrings.xml'))
strs = [t.text for t in ET.parse(strings).getroot().iter(ns+'t')]
@ -446,6 +453,22 @@ class TestContext(object):
for i, val in enumerate(r):
if isinstance(val, str):
r[i] = strs[int(val)]
# Translate the links
if links:
# Read the relationships
worksheet = self.get_out_path(os.path.join('desc', 'xl', 'worksheets', '_rels', 'sheet'+str(sheet)+'.xml.rels'))
root = ET.parse(worksheet).getroot()
rels = {}
for r in root:
rels[r.attrib['Id']] = r.attrib['Target']
# Convert cells to HTTP links
for k, v in links.items():
# Adapt the coordinate
rnum = int(k[1:])-rfirst
cnum = ord(k[0])-ord('A')
# Get the link
url = rels[v]
rows[rnum][cnum] = '<a href="{}">{}</a>'.format(url, rows[rnum][cnum])
# Separate the headers
headers = rows.pop(0)
return rows, headers, sh_head

View File

@ -0,0 +1,20 @@
# Example KiPlot config file
kiplot:
version: 1
outputs:
- name: 'bom_internal'
comment: "Bill of Materials in HTML format"
type: bom
dir: BoM
options:
xlsx:
datasheet_as_link: 'Part'
output: '%f.%x'
columns:
- References
- Part
- Value
- Quantity Per PCB
- digikey#
- manf#

View File

@ -0,0 +1,20 @@
# Example KiPlot config file
kiplot:
version: 1
outputs:
- name: 'bom_internal'
comment: "Bill of Materials in XLSX format"
type: bom
dir: BoM
options:
xlsx:
digikey_link: 'digikey#'
output: '%f.%x'
columns:
- References
- Part
- Value
- Quantity Per PCB
- digikey#
- manf#