From 7134b6841fa1f6755726b26c24a80c74e33a2972 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sun, 17 Oct 2021 20:01:05 +0200 Subject: [PATCH] Generate gauge string inside Cable object --- src/wireviz/DataClasses.py | 18 +++++++++++++++++- src/wireviz/Harness.py | 14 +------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index b7d3f71..11c7dec 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -6,7 +6,7 @@ from pathlib import Path from typing import Dict, List, Optional, Tuple, Union from wireviz.wv_colors import COLOR_CODES, Color, ColorMode, Colors, ColorScheme -from wireviz.wv_helper import aspect_ratio, int2tuple +from wireviz.wv_helper import aspect_ratio, awg_equiv, int2tuple, mm2_equiv # Each type alias have their legal values described in comments - validation might be implemented in the future PlainText = str # Text not containing HTML tags nor newlines @@ -281,6 +281,22 @@ class Cable: def is_autogenerated(self): return self.name.startswith(AUTOGENERATED_PREFIX) + @property + def gauge_str(self): + if not self.gauge: + return None + actual_gauge = f"{self.gauge} {self.gauge_unit}" + equivalent_gauge = "" + if self.show_equiv: + # Only convert units we actually know about, i.e. currently + # mm2 and awg --- other units _are_ technically allowed, + # and passed through as-is. + if self.gauge_unit == "mm\u00B2": + equivalent_gauge = f" ({awg_equiv(self.gauge)} AWG)" + elif self.gauge_unit.upper() == "AWG": + equivalent_gauge = f" ({mm2_equiv(self.gauge)} mm\u00B2)" + return f"{actual_gauge}{equivalent_gauge}" + def __post_init__(self) -> None: if isinstance(self.image, dict): diff --git a/src/wireviz/Harness.py b/src/wireviz/Harness.py index 9630aa7..bd138e8 100644 --- a/src/wireviz/Harness.py +++ b/src/wireviz/Harness.py @@ -45,10 +45,8 @@ from wireviz.wv_gv_html import ( remove_links, ) from wireviz.wv_helper import ( - awg_equiv, flatten2d, is_arrow, - mm2_equiv, open_file_read, open_file_write, tuplelist2tsv, @@ -203,16 +201,6 @@ class Harness: html = [] - awg_fmt = "" - if cable.show_equiv: - # Only convert units we actually know about, i.e. currently - # mm2 and awg --- other units _are_ technically allowed, - # and passed through as-is. - if cable.gauge_unit == "mm\u00B2": - awg_fmt = f" ({awg_equiv(cable.gauge)} AWG)" - elif cable.gauge_unit.upper() == "AWG": - awg_fmt = f" ({mm2_equiv(cable.gauge)} mm\u00B2)" - # fmt: off rows = [[f'{html_bgcolor(cable.bgcolor_title)}{remove_links(cable.name)}' if cable.show_name else None], @@ -226,7 +214,7 @@ class Harness: cable.spn if not isinstance(cable.spn, list) else None))], [html_line_breaks(cable.type), f'{cable.wirecount}x' if cable.show_wirecount else None, - f'{cable.gauge} {cable.gauge_unit}{awg_fmt}' if cable.gauge else None, + cable.gauge_str, '+ S' if cable.shield else None, f'{cable.length} {cable.length_unit}' if cable.length > 0 else None, translate_color(cable.color, self.options.color_mode) if cable.color else None,