Skip to content

Commit

Permalink
Add basic metadata and options to Harness
Browse files Browse the repository at this point in the history
Resolves the basic part of wireviz#158.
  • Loading branch information
kvid committed Aug 23, 2021
1 parent a368072 commit 4ccafa5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/wireviz/DataClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
CableMultiplier = PlainText # = Literal['wirecount', 'terminations', 'length', 'total_length']
ImageScale = PlainText # = Literal['false', 'true', 'width', 'height', 'both']
Color = PlainText # Two-letter color name = Literal[wv_colors._color_hex.keys()]
ColorMode = PlainText # = Literal['full', 'FULL', 'hex', 'HEX', 'short', 'SHORT', 'ger', 'GER']
ColorScheme = PlainText # Color scheme name = Literal[wv_colors.COLOR_CODES.keys()]

# Type combinations
Expand All @@ -30,6 +31,19 @@
OneOrMoreWires = Union[Wire, Tuple[Wire, ...]] # One or a tuple of wires


@dataclass
class Metadata:
title: PlainText
description: Optional[MultilineHypertext] = None
notes: Optional[MultilineHypertext] = None


@dataclass
class Options:
color_mode: ColorMode = 'SHORT'
mini_bom_mode: bool = True


@dataclass
class Image:
gv_dir: InitVar[Path] # Directory of .gv file injected as context during parsing
Expand Down
14 changes: 8 additions & 6 deletions src/wireviz/Harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from graphviz import Graph
from collections import Counter
from typing import List, Union
from dataclasses import dataclass
from pathlib import Path
from itertools import zip_longest
import re

from wireviz import wv_colors, __version__, APP_NAME, APP_URL
from wireviz.DataClasses import Connector, Cable
from wireviz.DataClasses import Metadata, Options, Connector, Cable
from wireviz.wv_colors import get_color_hex
from wireviz.wv_gv_html import nested_html_table, html_colorbar, html_image, \
html_caption, remove_links, html_line_breaks
Expand All @@ -20,11 +21,12 @@
open_file_read, open_file_write


@dataclass
class Harness:
metadata: Metadata
options: Options

def __init__(self):
self.color_mode = 'SHORT'
self.mini_bom_mode = True
def __post_init__(self):
self.connectors = {}
self.cables = {}
self._bom = [] # Internal Cache for generated bom
Expand Down Expand Up @@ -229,7 +231,7 @@ def create_graph(self) -> Graph:
wireinfo = []
if cable.show_wirenumbers:
wireinfo.append(str(i))
colorstr = wv_colors.translate_color(connection_color, self.color_mode)
colorstr = wv_colors.translate_color(connection_color, self.options.color_mode)
if colorstr:
wireinfo.append(colorstr)
if cable.wirelabels:
Expand Down Expand Up @@ -365,7 +367,7 @@ def output(self, filename: (str, Path), view: bool = False, cleanup: bool = True
with open_file_write(f'{filename}.bom.tsv') as file:
file.write(tuplelist2tsv(bomlist))
# HTML output
generate_html_output(filename, bomlist)
generate_html_output(filename, bomlist, self.metadata)

def bom(self):
if not self._bom:
Expand Down
7 changes: 6 additions & 1 deletion src/wireviz/wireviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

from wireviz import __version__
from wireviz.DataClasses import Metadata, Options
from wireviz.Harness import Harness
from wireviz.wv_helper import expand, open_file_read

Expand All @@ -34,7 +35,11 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st

yaml_data = yaml.safe_load(yaml_input)

harness = Harness()
# Assign default metadata.title here to avoid needing file_out in Metadata.__post_init__().
harness = Harness(
Metadata(**{'title': Path(file_out).stem, **yaml_data.get('metadata', {})}),
Options(**yaml_data.get('options', {})),
)

# add items
sections = ['connectors', 'cables', 'connections']
Expand Down
2 changes: 1 addition & 1 deletion src/wireviz/wv_bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_additional_component_table(harness: "Harness", component: Union[Connecto
'qty': part.qty * component.get_qty_multiplier(part.qty_multiplier),
'unit': part.unit,
}
if harness.mini_bom_mode:
if harness.options.mini_bom_mode:
id = get_bom_index(harness.bom(), bom_entry_key({**asdict(part), 'description': part.description}))
rows.append(component_table_entry(f'#{id} ({part.type.rstrip()})', **common_args))
else:
Expand Down
18 changes: 13 additions & 5 deletions src/wireviz/wv_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
# -*- coding: utf-8 -*-

from pathlib import Path
from typing import List, Union
import re

from wireviz import __version__, APP_NAME, APP_URL
from wireviz.DataClasses import Metadata
from wireviz.wv_helper import flatten2d, open_file_read, open_file_write

def generate_html_output(filename: (str, Path), bom_list):
def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]], metadata: Metadata):
with open_file_write(f'{filename}.html') as file:
file.write('<!DOCTYPE html>\n')
file.write('<html lang="en"><head>\n')
file.write(' <meta charset="UTF-8">\n')
file.write(f' <meta name="generator" content="{APP_NAME} {__version__} - {APP_URL}">\n')
file.write(f' <title>{APP_NAME} Diagram and BOM</title>\n')
file.write(f' <title>{metadata.title}</title>\n')
file.write('</head><body style="font-family:Arial">\n')

file.write('<h1>Diagram</h1>')
file.write(f'<h1>{metadata.title}</h1>\n')
if metadata.description:
file.write(f'<p>{metadata.description}</p>\n')
file.write('<h2>Diagram</h2>\n')
with open_file_read(f'{filename}.svg') as svg:
file.write(re.sub(
'^<[?]xml [^?>]*[?]>[^<]*<!DOCTYPE [^>]*>',
Expand All @@ -25,7 +30,7 @@ def generate_html_output(filename: (str, Path), bom_list):
for svgdata in svg:
file.write(svgdata)

file.write('<h1>Bill of Materials</h1>')
file.write('<h2>Bill of Materials</h2>\n')
listy = flatten2d(bom_list)
file.write('<table style="border:1px solid #000000; font-size: 14pt; border-spacing: 0px">')
file.write('<tr>')
Expand All @@ -39,6 +44,9 @@ def generate_html_output(filename: (str, Path), bom_list):
align = 'text-align:right; ' if listy[0][i] == 'Qty' else ''
file.write(f'<td style="{align}border:1px solid #000000; padding: 4px">{item_str}</td>')
file.write('</tr>')
file.write('</table>')
file.write('</table>\n')

if metadata.notes:
file.write(f'<h2>Notes</h2>\n<p>{metadata.notes}</p>\n')

file.write('</body></html>')

0 comments on commit 4ccafa5

Please sign in to comment.