Merge pull request #4 from laurierloi/index-table

Index table
This commit is contained in:
Laurier Loiselle 2023-04-19 17:33:31 -04:00 committed by GitHub
commit f77654f876
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 184 additions and 116 deletions

View File

@ -53,6 +53,7 @@
<!-- BOM TEMPLATE --!>
<div id="bom">
<b> BOM </b>
<table>
{% if not bom_reversed %}
{{ bom_header_gen() }}

69
src/wireviz/templates/index_table.html generated Normal file
View File

@ -0,0 +1,69 @@
<style>
.A4 #index_table { /* index_table on top of title block */
position: absolute;
top: 20mm;
left: 0;
width:180mm;
}
.A3 #index_table, .A2 #index_table { /* index_table to the left of title block */
position: absolute;
{% if index_table_updated_position %}
{{ index_table_updated_position }}
{% else %}
top: {{ 20 + (bom_rows + 2) * 4.25 + 10}}mm;
left: 10mm;
{% endif %}
}
#index_table tr {
height: 4.25mm;
}
#index_table tr:nth-child(odd) {
background-color: #EEEEEE
}
#index_table table, th, td {
border: 1px solid black;
border-collapse: collapse;
text-align: left;
}
#index_table .index_table_col_id {
text-align: center;
font-weight: bold;
}
#index_table .index_table_col_qty {
text-align: right;
}
</style>
<!-- index_table HEADER GENERATION MACRO --!>
{% macro index_table_header_gen() -%}
<tr>
{% for item in index_table_header %}
<td><b> {{ item }} </b></td>
{% endfor %}
</tr>
{%- endmacro %}
<!-- index_table TEMPLATE --!>
<div id="index_table">
<b> INDEX TABLE </b>
<table>
{{ index_table_header_gen() }}
{% for row in index_table_content %}
<tr>
{% for item in row %}
<td> {{ item }} </td>
{% endfor %}
</tr>
{% endfor %}
</table >
</div>

View File

@ -14,4 +14,8 @@
{% if show_bom %}
{{ bom }}
{% endif %}
{% if show_index_table %}
{{ index_table }}
{% endif %}
{% endblock %}

View File

@ -63,23 +63,21 @@ def build_generated(groupkeys):
with (path / readme).open("w") as out:
out.write(f'# {groups[key]["title"]}\n\n')
# collect and iterate input YAML files
yaml_files = [f for f in collect_filenames("Building", key, input_extensions)]
try:
res = cli([
"--formats", "ghpstb", # no pdf for now
"--prepend", yaml_files[0].parent / "metadata.yml",
*[str(f) for f in yaml_files],
])
except BaseException as e:
if str(e) != "0" and not isinstance(
e, (click.ClickException, SystemExit)
):
raise
for yaml_file in collect_filenames("Building", key, input_extensions):
try:
res = cli([
"--formats",
"ghpstPb",
str(yaml_file),
"--prepend",
yaml_file.parent / "metadata.yml"
])
except BaseException as e:
if str(e) != "0" and not isinstance(
e, (click.ClickException, SystemExit)
):
raise
if build_readme:
if build_readme:
for yaml_file in yaml_files:
i = "".join(filter(str.isdigit, yaml_file.stem))
with (path / readme).open("a") as out:
@ -113,42 +111,6 @@ def clean_generated(groupkeys):
print(f' rm "{filename}"')
filename.unlink()
def compare_generated(groupkeys, branch="", include_graphviz_output=False):
if branch:
branch = f" {branch.strip()}"
compare_extensions = (
generated_extensions
if include_graphviz_output
else extensions_not_containing_graphviz_output
)
for key in groupkeys:
# collect and compare files
for filename in collect_filenames("Comparing", key, compare_extensions):
cmd = f'git --no-pager diff{branch} -- "{filename}"'
print(f" {cmd}")
os.system(cmd)
def restore_generated(groupkeys, branch=""):
if branch:
branch = f" {branch.strip()}"
for key in groupkeys:
# collect input YAML files
filename_list = collect_filenames("Restoring", key, input_extensions)
# collect files to restore
filename_list = [
fn.with_suffix(ext) for fn in filename_list for ext in generated_extensions
]
if readme in groups[key]:
filename_list.append(groups[key]["path"] / readme)
# restore files
for filename in filename_list:
cmd = f'git checkout{branch} -- "{filename}"'
print(f" {cmd}")
os.system(cmd)
def parse_args():
parser = argparse.ArgumentParser(
description=f"{APP_NAME} Example Manager",
@ -167,19 +129,6 @@ def parse_args():
default="build",
help="what to do with the generated files (default: build)",
)
parser.add_argument(
"-c",
"--compare-graphviz-output",
action="store_true",
help="the Graphviz output is also compared (default: False)",
)
parser.add_argument(
"-b",
"--branch",
action="store",
default="",
help="branch or commit to compare with or restore from",
)
parser.add_argument(
"-g",
"--groups",
@ -197,10 +146,6 @@ def main():
build_generated(args.groups)
elif args.action == "clean":
clean_generated(args.groups)
elif args.action == "compare" or args.action == "diff":
compare_generated(args.groups, args.branch, args.compare_graphviz_output)
elif args.action == "restore":
restore_generated(args.groups, args.branch)
if __name__ == "__main__":

View File

@ -14,8 +14,7 @@ import yaml
import wireviz.wireviz as wv
from wireviz import APP_NAME, __version__
from wireviz.wv_bom import bom_list
from wireviz.wv_output import generate_pdf_output, generate_html_output, generate_shared_bom
from wireviz.wv_output import generate_pdf_output, generate_html_output, generate_shared_bom, generate_titlepage
from wireviz.wv_dataclasses import AUTOGENERATED_PREFIX, Metadata, Options
format_codes = {
@ -126,23 +125,29 @@ def cli(files, formats, prepend, output_dir, output_name, version, use_qty_multi
# determine output formats
output_formats = {format_codes[f] for f in formats if f in format_codes}
harness_output_formats = output_formats.copy()
create_titlepage = False
# Only generate the global pdf if there's multiple files
if len(files) > 1 and 'pdf' in harness_output_formats:
harness_output_formats.remove('pdf')
create_titlepage = True
titlepage_path = (_output_dir / "titlepage").with_suffix(".html")
harness = None
shared_bom = {}
sheet_current = 1
if create_titlepage:
sheet_current += 1
output_names = []
extra_metadata = {}
extra_metadata["output_dir"] = _output_dir
extra_metadata["output_names"] = []
extra_metadata["sheet_total"] = len(files)
extra_metadata["sheet_current"] = 1
# Only generate the global pdf if there's multiple files
create_titlepage = False
if extra_metadata["sheet_total"] > 1:
create_titlepage = True
extra_metadata["titlepage"] = Path("titlepage")
extra_metadata["sheet_current"] += 1
extra_metadata["sheet_total"] += 1
if 'pdf' in harness_output_formats:
harness_output_formats.remove('pdf')
# run WireVIz on each input file
for _file in files:
_output_name = _file.stem if not output_name else output_name
output_names.append(_output_dir / _output_name)
extra_metadata["output_names"].append(_output_name)
print("Input file: ", _file)
print(
@ -150,13 +155,7 @@ def cli(files, formats, prepend, output_dir, output_name, version, use_qty_multi
f"{_output_dir / _output_name}.[{'|'.join(output_formats)}]",
)
extra_metadata = {}
extra_metadata["sheet_name"] = _output_name.upper()
extra_metadata["sheet_total"] = len(files)
if create_titlepage:
extra_metadata["sheet_total"] += 1
extra_metadata["sheet_current"] = sheet_current
sheet_current += 1
file_dir = _file.parent
@ -171,6 +170,7 @@ def cli(files, formats, prepend, output_dir, output_name, version, use_qty_multi
)
shared_bom = ret["shared_bom"]
shared_bom_base = None
if "shared_bom" in output_formats:
shared_bom_base, shared_bomlist = generate_shared_bom(
@ -181,37 +181,22 @@ def cli(files, formats, prepend, output_dir, output_name, version, use_qty_multi
multiplier_file_name=multiplier_file_name,
)
# TODO: instead have a parse function that create a titlepage, not a harness
if ('html' in output_formats) and create_titlepage:
# TODO: yaml data parsing shared
yaml_data_str = "\n".join(f.open("r").read() for f in prepend)
yaml_data = yaml.safe_load(yaml_data_str)
titlepage_metadata = {
**yaml_data.get("metadata", {}),
**extra_metadata,
"sheet_current": 1,
"sheet_name": "titlepage",
"output_name": "titlepage",
"bom_updated_position": "top: 20mm; left: 10mm",
"notes_width": "200mm",
}
titlepage_metadata['template']['name'] = 'titlepage'
titlepage_options = {
**yaml_data.get("options", {}),
"show_bom": True,
"show_notes": True,
}
generate_html_output(
titlepage_path,
bom = bom_list(shared_bom),
metadata = Metadata(**titlepage_metadata), # TBD what we need to add here
options = Options(**titlepage_options),
)
if 'pdf' in output_formats and 'html' in output_formats and len(output_names) > 1:
if create_titlepage:
output_names.insert(0, titlepage_path)
generate_titlepage(yaml_data, extra_metadata, shared_bom)
generate_pdf_output(output_names)
if 'pdf' in output_formats:
extra_metadata["titlepage"] = extra_metadata["titlepage"].with_suffix('_for_pdf')
if create_titlepage:
extra_metadata["output_names"].insert(0, extra_metadata["titlepage"].with_suffix('.html'))
generate_titlepage(yaml_data, extra_metadata, shared_bom, for_pdf=True)
if 'pdf' in output_formats:
generate_pdf_output([_output_dir / p for p in extra_metadata["output_names"]])
print() # blank line after execution

View File

@ -423,6 +423,7 @@ class BomEntry:
class Options:
fontname: PlainText = "arial"
show_bom: bool = True
show_index_table: bool = False
show_notes: bool = True
notes_on_right: bool = True
notes_width: str = "100mm"

View File

@ -156,6 +156,7 @@ def generate_html_output(
"bgcolor": options.bgcolor.html,
"show_bom": options.show_bom,
"show_notes": options.show_notes,
"show_index_table": options.show_index_table,
"notes_on_right": options.notes_on_right,
"notes_width": options.notes_width,
"diagram": svgdata,
@ -207,8 +208,70 @@ def generate_html_output(
if "notes" in replacements:
replacements["notes"] = get_template("notes.html").render(replacements)
# prepare index_table
replacements["index_table"] = get_template("index_table.html").render(replacements)
# generate page template
page_rendered = get_template(template_name, ".html").render(replacements)
# save generated file
filename.with_suffix(".html").open("w").write(page_rendered)
def generate_titlepage(yaml_data, extra_metadata, shared_bom, for_pdf=False):
print('Generating titlepage')
index_table_content = []
index_table_content.append((
1, extra_metadata['titlepage'], ''
))
for index, page_name in enumerate(extra_metadata['output_names']):
index_table_content.append((
index+2, page_name, ''
))
if not for_pdf:
index_table_content = [(
p[0],
f"<a href={Path(p[1]).with_suffix('.html')}>{p[1]}</a>",
p[2],
) for p in index_table_content]
#if create_titlepage:
# extra_metadata["index_table_content"].append([
# sheet_current,
# f"<a href={Path(_output_name).with_suffix('.html')}>{extra_metadata['sheet_name']}</a>",
# "",
# ])
#index_table_content.insert(0, [
# 1,
# f"<a href={Path('titlepage').with_suffix('.html')}>Title Page</a>",
# ''
#])
titlepage_metadata = {
**yaml_data.get("metadata", {}),
**extra_metadata,
"sheet_current": 1,
"sheet_name": "titlepage",
"output_name": "titlepage",
"index_table_header": ["Sheet", "Harness", "Notes"],
"index_table_content": index_table_content,
"bom_updated_position": "top: 20mm; left: 10mm",
"notes_width": "200mm",
}
titlepage_metadata['template']['name'] = 'titlepage'
titlepage_options = {
"show_bom": True,
"show_index_table": True,
"show_notes": True,
**yaml_data.get("options", {}),
}
generate_html_output(
extra_metadata['output_dir']/ extra_metadata['titlepage'],
bom = bom_list(shared_bom),
metadata = Metadata(**titlepage_metadata), # TBD what we need to add here
options = Options(**titlepage_options),
)