Generalize metadata filling for nested dicts

useful for authors and revisions
This commit is contained in:
Daniel Rojas 2021-03-27 15:45:48 +01:00
parent b3e47e148a
commit 6a7f1fdcd8
2 changed files with 51 additions and 69 deletions

View File

@ -203,70 +203,70 @@
<div id="titleblock">
<table>
<tr>
<td class="revno"><!-- %rev_8_number% --></td>
<td class="changelog"><!-- %rev_8_changelog% --></td>
<td class="date"><!-- %rev_8_date% --></td>
<td class="name"><!-- %rev_8_name% --></td>
<td class="revno"><!-- %revisions_8% --></td>
<td class="changelog"><!-- %revisions_8_changelog% --></td>
<td class="date"><!-- %revisions_8_date% --></td>
<td class="name"><!-- %revisions_8_name% --></td>
<td class="process"></td>
<td class="date">Date</td>
<td class="name">Name</td>
<td class="title" colspan="3" rowspan="5"><!-- %title% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_7_number% --></td>
<td><!-- %rev_7_changelog% --></td>
<td><!-- %rev_7_date% --></td>
<td><!-- %rev_7_name% --></td>
<td><!-- %process_1_title% --></td>
<td><!-- %process_1_date% --></td>
<td><!-- %process_1_name% --></td>
<td class="revno"><!-- %revisions_7% --></td>
<td><!-- %revisions_7_changelog% --></td>
<td><!-- %revisions_7_date% --></td>
<td><!-- %revisions_7_name% --></td>
<td><!-- %authors_1% --></td>
<td><!-- %authors_1_date% --></td>
<td><!-- %authors_1_name% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_6_number% --></td>
<td><!-- %rev_6_changelog% --></td>
<td><!-- %rev_6_date% --></td>
<td><!-- %rev_6_name% --></td>
<td><!-- %process_2_title% --></td>
<td><!-- %process_2_date% --></td>
<td><!-- %process_2_name% --></td>
<td class="revno"><!-- %revisions_6% --></td>
<td><!-- %revisions_6_changelog% --></td>
<td><!-- %revisions_6_date% --></td>
<td><!-- %revisions_6_name% --></td>
<td><!-- %authors_2% --></td>
<td><!-- %authors_2_date% --></td>
<td><!-- %authors_2_name% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_5_number% --></td>
<td><!-- %rev_5_changelog% --></td>
<td><!-- %rev_5_date% --></td>
<td><!-- %rev_5_name% --></td>
<td><!-- %process_3_title% --></td>
<td><!-- %process_3_date% --></td>
<td><!-- %process_3_name% --></td>
<td class="revno"><!-- %revisions_5% --></td>
<td><!-- %revisions_5_changelog% --></td>
<td><!-- %revisions_5_date% --></td>
<td><!-- %revisions_5_name% --></td>
<td><!-- %authors_3% --></td>
<td><!-- %authors_3_date% --></td>
<td><!-- %authors_3_name% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_4_number% --></td>
<td><!-- %rev_4_changelog% --></td>
<td><!-- %rev_4_date% --></td>
<td><!-- %rev_4_name% --></td>
<td class="revno"><!-- %revisions_4% --></td>
<td><!-- %revisions_4_changelog% --></td>
<td><!-- %revisions_4_date% --></td>
<td><!-- %revisions_4_name% --></td>
<td colspan="2"></td>
<td></td>
</tr>
<tr>
<td class="revno"><!-- %rev_3_number% --></td>
<td><!-- %rev_3_changelog% --></td>
<td><!-- %rev_3_date% --></td>
<td><!-- %rev_3_name% --></td>
<td class="revno"><!-- %revisions_3% --></td>
<td><!-- %revisions_3_changelog% --></td>
<td><!-- %revisions_3_date% --></td>
<td><!-- %revisions_3_name% --></td>
<td class="company" colspan="3" rowspan="3"><!-- %company% --></td>
<td class="partno" colspan="2" rowspan="3"><!-- %pn% --></td>
<td class="sheetno" rowspan="2"><!-- %sheet_current% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_2_number% --></td>
<td><!-- %rev_2_changelog% --></td>
<td><!-- %rev_2_date% --></td>
<td><!-- %rev_2_name% --></td>
<td class="revno"><!-- %revisions_2% --></td>
<td><!-- %revisions_2_changelog% --></td>
<td><!-- %revisions_2_date% --></td>
<td><!-- %revisions_2_name% --></td>
</tr>
<tr>
<td class="revno"><!-- %rev_1_number% --></td>
<td><!-- %rev_1_changelog% --></td>
<td><!-- %rev_1_date% --></td>
<td><!-- %rev_1_name% --></td>
<td class="revno"><!-- %revisions_1% --></td>
<td><!-- %revisions_1_changelog% --></td>
<td><!-- %revisions_1_date% --></td>
<td><!-- %revisions_1_name% --></td>
<td class="sheetno"><!-- %sheet_total% --></td>
</tr>
<tr>

View File

@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
from pathlib import Path
from typing import List, Union
from typing import List, Dict, Union
import re
from wireviz import __version__, APP_NAME, APP_URL, wv_colors
@ -62,28 +62,6 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]],
if metadata:
html = html.replace('<!-- %generator% -->', f'{APP_NAME} {__version__} - {APP_URL}')
# fill out authors
for i, (k, v) in enumerate(metadata.get('authors', {}).items(), 1):
title = k
name = v['name']
date = v['date'].strftime('%Y-%m-%d')
html = html.replace(f'<!-- %process_{i}_title% -->', title)
html = html.replace(f'<!-- %process_{i}_name% -->', name)
html = html.replace(f'<!-- %process_{i}_date% -->', date)
# fill out revisions
for i, (k, v) in enumerate(metadata.get('revisions', {}).items(), 1):
# TODO: for more than 8 revisions, keep only the 8 most recent ones
number = k
changelog = v['changelog']
name = v['name']
date = v['date'].strftime('%Y-%m-%d')
html = html.replace(f'<!-- %rev_{i}_number% -->', '{:02d}'.format(number))
html = html.replace(f'<!-- %rev_{i}_changelog% -->', changelog)
html = html.replace(f'<!-- %rev_{i}_name% -->', name)
html = html.replace(f'<!-- %rev_{i}_date% -->', date)
html = html.replace(f'"sheetsize_default"', '"{}"'.format(metadata.get('template',{}).get('sheetsize', ''))) # include quotes so no replacement happens within <style> definition
# TODO: handle multi-page documents
@ -91,11 +69,15 @@ def generate_html_output(filename: Union[str, Path], bom_list: List[List[str]],
html = html.replace('<!-- %sheet_total% -->', 'of 1')
# fill out other generic metadata
for k, v in metadata.items():
if isinstance(v, (str, int, float)):
html = html.replace(f'<!-- %{k}% -->', v)
# TODO: Support smart handling of lists and dicts?
for item, value in metadata.items():
if isinstance(value, (str, int, float)):
html = html.replace(f'<!-- %{item}% -->', value)
elif isinstance(value, Dict): # useful for authors, revisions
for index, (category, entry) in enumerate(value.items()):
if isinstance(entry, Dict):
html = html.replace(f'<!-- %{item}_{index+1}% -->', str(category))
for entry_key, entry_value in entry.items():
html = html.replace(f'<!-- %{item}_{index+1}_{entry_key}% -->', str(entry_value))
with open(f'{filename}.html','w') as file: