Skip to content

Commit ff5eb3e

Browse files
committed
Clean up logging
1 parent 9c65a2e commit ff5eb3e

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

InteractiveHtmlBom/generate_interactive_bom.py

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
font_parser = FontParser()
1818

1919

20+
def loginfo(*args):
21+
if is_cli:
22+
logging.info(*args)
23+
24+
25+
def logerror(msg):
26+
if is_cli:
27+
logging.error(msg)
28+
else:
29+
wx.MessageBox(msg)
30+
31+
2032
def generate_bom(pcb, filter_layer=None):
2133
"""
2234
Generate BOM from pcb layout.
@@ -117,7 +129,7 @@ def parse_draw_segment(d):
117129
pcbnew.S_POLYGON: "polygon",
118130
}.get(d.GetShape(), "")
119131
if shape == "":
120-
logging.info("Unsupported shape %s, skipping", d.GetShape())
132+
loginfo("Unsupported shape %s, skipping", d.GetShape())
121133
return None
122134
start = normalize(d.GetStart())
123135
end = normalize(d.GetEnd())
@@ -153,7 +165,7 @@ def parse_draw_segment(d):
153165
if hasattr(d, "GetPolyShape"):
154166
polygons = parse_poly_set(d.GetPolyShape())
155167
else:
156-
logging.info("Polygons not supported for KiCad 4, skipping")
168+
loginfo("Polygons not supported for KiCad 4, skipping")
157169
return None
158170
angle = 0
159171
if d.GetParentModule() is not None:
@@ -230,7 +242,7 @@ def parse_drawing(d):
230242
elif d.GetClass() in ["PTEXT", "MTEXT"]:
231243
return parse_text(d)
232244
else:
233-
logging.info("Unsupported drawing class %s, skipping", d.GetClass())
245+
loginfo("Unsupported drawing class %s, skipping", d.GetClass())
234246
return None
235247

236248

@@ -303,7 +315,7 @@ def parse_pad(pad):
303315
shape_lookup[pcbnew.PAD_SHAPE_CUSTOM] = "custom"
304316
shape = shape_lookup.get(pad.GetShape(), "")
305317
if shape == "":
306-
logging.info("Unsupported pad shape %s, skipping.",
318+
loginfo("Unsupported pad shape %s, skipping.",
307319
pad.GetShape())
308320
return None
309321
pad_dict = {
@@ -407,7 +419,7 @@ def get_file_content(file_name):
407419
with open(os.path.join(os.path.dirname(__file__), file_name), "r") as f:
408420
return f.read()
409421

410-
logging.info("Dumping pcb json data")
422+
loginfo("Dumping pcb json data")
411423
bom_file_name = os.path.join(dir, "ibom.html")
412424
if not os.path.isdir(os.path.dirname(bom_file_name)):
413425
os.makedirs(os.path.dirname(bom_file_name))
@@ -420,18 +432,14 @@ def get_file_content(file_name):
420432
html = html.replace('///IBOMJS///', get_file_content('ibom.js'))
421433
with open(bom_file_name, "wt") as bom:
422434
bom.write(html)
423-
logging.info("Created file %s", bom_file_name)
435+
loginfo("Created file %s", bom_file_name)
424436
return bom_file_name
425437

426438

427439
def main(pcb, launch_browser=True):
428440
pcb_file_name = pcb.GetFileName()
429441
if not pcb_file_name:
430-
msg = 'Please save the board file before generating BOM.'
431-
if is_cli:
432-
logging.error(msg)
433-
else:
434-
wx.MessageBox(msg)
442+
logerror('Please save the board file before generating BOM.')
435443
return
436444

437445
bom_file_dir = os.path.join(os.path.dirname(pcb_file_name), "bom")
@@ -449,13 +457,9 @@ def main(pcb, launch_browser=True):
449457
title = os.path.splitext(title)[0]
450458
edges, bbox = parse_edges(pcb)
451459
if bbox is None:
452-
msg = 'Please draw pcb outline on the edges ' \
453-
'layer on sheet or any module before ' \
454-
'generating BOM.'
455-
if is_cli:
456-
logging.error(msg)
457-
else:
458-
wx.MessageBox(msg)
460+
logerror('Please draw pcb outline on the edges ' \
461+
'layer on sheet or any module before ' \
462+
'generating BOM.')
459463
return
460464
bbox = {
461465
"minx": bbox.GetPosition().x * 1e-6,
@@ -487,7 +491,7 @@ def main(pcb, launch_browser=True):
487491
bom_file = generate_file(bom_file_dir, pcbdata)
488492

489493
if launch_browser:
490-
logging.info("Opening file in browser")
494+
loginfo("Opening file in browser")
491495
open_file(bom_file)
492496

493497

0 commit comments

Comments
 (0)