Skip to content

Commit

Permalink
WireViz 0.3: Add "metadata.title" attribute to input YAML, if unset
Browse files Browse the repository at this point in the history
Instead of propagating the "title" using `file_out`, inject a default
title into the ingress YAML payload. This makes `file_out` non-mandatory
again.
  • Loading branch information
amotl committed Oct 22, 2021
1 parent 9149ce2 commit 3a51704
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
17 changes: 16 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class TestRenderRegular:
def data_valid(self):
return {"yml_file": (io.BytesIO(b"Bob -> Alice : hello"), "test.yml")}

@property
def data_valid_with_title(self):
return {"yml_file": (io.BytesIO(b"metadata:\n title: Foobar\nBob -> Alice : hello"), "test.yml")}

@property
def data_empty(self):
return {"yml_file": (io.BytesIO(b""), "empty.yml")}
Expand Down Expand Up @@ -62,16 +66,27 @@ def test_html(self, client):
headers={"Accept": "text/html"},
)
assert response.status_code == 200

assert response.headers["Content-Type"] == "text/html; charset=utf-8"
assert response.headers["Content-Disposition"] == "attachment; filename=test.html"
assert b"<!DOCTYPE html>" in response.data
assert b"""<meta name="generator" content="WireViz""" in response.data
assert b"<title>wiring-42</title>" in response.data
assert b"<title>WireViz diagram and BOM</title>" in response.data
assert b"<h2>Diagram</h2>" in response.data
assert b"<svg" in response.data
assert b"<h2>Bill of Materials</h2>" in response.data
assert b"<table" in response.data

def test_html_with_title(self, client):
response = client.post(
url_for("wireviz-web._render_regular"),
data=self.data_valid_with_title,
headers={"Accept": "text/html"},
)
assert response.status_code == 200

assert b"<title>Foobar</title>" in response.data

def test_bom_text(self, client):
response = client.post(
url_for("wireviz-web._render_regular"),
Expand Down
17 changes: 11 additions & 6 deletions wireviz_web/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import json
import logging
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryDirectory
from tempfile import NamedTemporaryFile

import yaml
from flask import Response, send_file
from werkzeug.exceptions import BadRequest, NotAcceptable
from wireviz import wireviz
Expand Down Expand Up @@ -112,11 +113,15 @@ def wireviz_render(input_yaml: str, output_mimetype: str, output_filename: str)

# Parse WireViz YAML.
try:
# WireViz >= 0.3 needs the `file_out` parameter.
# This will apparently be deleted by WireViz?
with TemporaryDirectory() as tmpdir:
file_out = Path(tmpdir) / "wiring-42"
harness: Harness = wireviz.parse(yaml_input=input_yaml, file_out=file_out, return_types="harness")
# Set title, if not present.
default_title = "WireViz diagram and BOM"
yaml_data = yaml.safe_load(input_yaml)
if "title" not in yaml_data.get("metadata", {}):
yaml_data.setdefault("metadata", {})
yaml_data["metadata"]["title"] = default_title
input_yaml = yaml.dump(yaml_data)

harness: Harness = wireviz.parse(yaml_input=input_yaml, return_types="harness")
except Exception as ex:
message = f"Unable to parse WireViz YAML format: {ex}"
logger.exception(message)
Expand Down

0 comments on commit 3a51704

Please sign in to comment.