File: render.py

package info (click to toggle)
junit2html 31.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 576 kB
  • sloc: xml: 3,208; python: 1,023; makefile: 6; sh: 5
file content (63 lines) | stat: -rw-r--r-- 1,953 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
Render junit reports as HTML
"""
from typing import TYPE_CHECKING

if TYPE_CHECKING: # pragma: no cover
    from .matrix import ReportMatrix
    from .parser import Junit
    from os import PathLike
    from typing import Union, Sequence, Optional

from jinja2 import Environment, PackageLoader, select_autoescape, FileSystemLoader


class HTMLReport(object):
    title: str = ""
    report: "Optional[Junit]" = None
    show_toc: bool = True

    def __init__(self, show_toc: bool=True):
        self.show_toc = show_toc

    def load(self, report: "Junit", title: str="JUnit2HTML Report"):
        self.report = report
        self.title = title

    def __iter__(self):
        if self.report is None:
            raise Exception("A report must be loaded through `load(...)` first.")

        return self.report.__iter__()

    def __str__(self) -> str:
        env = Environment(
            loader=PackageLoader("junit2htmlreport", "templates"),
            autoescape=select_autoescape(["html"])
        )

        template = env.get_template("report.html")
        return template.render(report=self, title=self.title, show_toc=self.show_toc)


class HTMLMatrix(object):
    title: str = "JUnit Matrix"
    matrix: "ReportMatrix"
    template: "Optional[Union[str,PathLike[str],Sequence[Union[str,PathLike[str]]]]]"

    def __init__(self, matrix: "ReportMatrix", template:"Optional[Union[str,PathLike[str],Sequence[Union[str,PathLike[str]]]]]"=None):
        self.matrix = matrix
        self.template = template

    def __str__(self) -> str:
        if self.template:
            loader = FileSystemLoader(self.template)
        else:
            loader = PackageLoader("junit2htmlreport", "templates")
        env = Environment(
            loader=loader,
            autoescape=select_autoescape(["html"])
        )

        template = env.get_template("matrix.html")
        return template.render(matrix=self.matrix, title=self.title)