From 15d7374399530e9c5ab53bcb895d5b85ed8eb653 Mon Sep 17 00:00:00 2001 From: KV Date: Sat, 27 Mar 2021 01:10:21 +0100 Subject: [PATCH] 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 --- src/wireviz/DataClasses.py | 12 +++++------- src/wireviz/wireviz.py | 7 ++++--- src/wireviz/wv_html.py | 14 ++++++++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/wireviz/DataClasses.py b/src/wireviz/DataClasses.py index 1d34ebf..950cf23 100644 --- a/src/wireviz/DataClasses.py +++ b/src/wireviz/DataClasses.py @@ -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 diff --git a/src/wireviz/wireviz.py b/src/wireviz/wireviz.py index 27496b4..6ba77ee 100755 --- a/src/wireviz/wireviz.py +++ b/src/wireviz/wireviz.py @@ -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'] diff --git a/src/wireviz/wv_html.py b/src/wireviz/wv_html.py index 94c8d62..cc55af8 100644 --- a/src/wireviz/wv_html.py +++ b/src/wireviz/wv_html.py @@ -15,13 +15,14 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]], file.write('\n') file.write(' \n') file.write(f' \n') - file.write(f' {metadata.title}\n') + file.write(f' {metadata["title"]}\n') file.write(f'\n') - file.write(f'

{metadata.title}

\n') - if metadata.description: - file.write(f'

{metadata.description}

\n') + file.write(f'

{metadata["title"]}

\n') + description = metadata.get('description') + if description: + file.write(f'

{description}

\n') file.write('

Diagram

\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(' \n') file.write('\n') - if metadata.notes: - file.write(f'

Notes

\n

{metadata.notes}

\n') + notes = metadata.get('notes') + if notes: + file.write(f'

Notes

\n

{notes}

\n') file.write('\n')