Change Metadata to a dict as requested by the owner

And a few other changes requested in the same review.

Co-authored-by: Daniel Rojas <github@danielrojas.net>
This commit is contained in:
KV 2021-03-27 01:10:21 +01:00
parent d5c69116ea
commit 15d7374399
3 changed files with 17 additions and 16 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import Optional, List, Tuple, Union
from typing import Dict, List, Optional, Tuple, Union
from dataclasses import dataclass, field, InitVar
from pathlib import Path
@ -31,12 +31,10 @@ Wire = Union[int, PlainText] # Wire number or Literal['s'] for shield
NoneOrMorePinIndices = Union[PinIndex, Tuple[PinIndex, ...], None] # None, one, or a tuple of zero-based pin indices
OneOrMoreWires = Union[Wire, Tuple[Wire, ...]] # One or a tuple of wires
@dataclass
class Metadata:
title: PlainText
description: Optional[MultilineHypertext] = None
notes: Optional[MultilineHypertext] = None
# Metadata can contain whatever is needed by the HTML generation/template.
MetadataKeys = PlainText # Literal['title', 'description', 'notes', ...]
class Metadata(dict):
pass
@dataclass

View File

@ -35,11 +35,12 @@ def parse(yaml_input: str, file_out: (str, Path) = None, return_types: (None, st
yaml_data = yaml.safe_load(yaml_input)
# 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', {})),
metadata = Metadata(**yaml_data.get('metadata', {})),
options = Options(**yaml_data.get('options', {})),
)
if 'title' not in harness.metadata:
harness.metadata['title'] = Path(file_out).stem
# add items
sections = ['connectors', 'cables', 'connections']

View File

@ -15,13 +15,14 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]],
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>{metadata.title}</title>\n')
file.write(f' <title>{metadata["title"]}</title>\n')
file.write(f'</head><body style="font-family:{options.fontname};background-color:'
f'{wv_colors.translate_color(options.bgcolor, "HEX")}">\n')
file.write(f'<h1>{metadata.title}</h1>\n')
if metadata.description:
file.write(f'<p>{metadata.description}</p>\n')
file.write(f'<h1>{metadata["title"]}</h1>\n')
description = metadata.get('description')
if description:
file.write(f'<p>{description}</p>\n')
file.write('<h2>Diagram</h2>\n')
with open_file_read(f'{filename}.svg') as svg:
file.write(re.sub(
@ -47,7 +48,8 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]],
file.write(' </tr>\n')
file.write('</table>\n')
if metadata.notes:
file.write(f'<h2>Notes</h2>\n<p>{metadata.notes}</p>\n')
notes = metadata.get('notes')
if notes:
file.write(f'<h2>Notes</h2>\n<p>{notes}</p>\n')
file.write('</body></html>\n')