# Copyright 2024 Volvo Car Corporation
# Licensed under Apache 2.0.
"""Module for html report generation."""
from string import Template
class HtmlReport:
"""Generate template html report. Extend this class to add content.
TODO: Refactor common parts from derived report classes to this class.
"""
__html_head = """
$title
$title
"""
__html_end = """
"""
def __init__(self, title):
"""Initialize report title.
Args:
title (str): The report title
"""
super().__init__()
self._title = title
def _gen_header(self):
"""Generate html header."""
return Template(self.__html_head).substitute(title=self._title)
@staticmethod
def gen_contents():
"""Generate report contents.
Override this method to add report contents.
Contents should start with a header.
"""
return 'Template report
'
def _gen_end(self):
"""Generate the end of the body and html document."""
return self.__html_end
def generate_report_string(self):
"""Generate a html report as string."""
html = []
html += self._gen_header()
html += self.gen_contents()
html += self._gen_end()
return ''.join(html)
def generate_report_file(self, filename):
"""Generate a html report and save to file."""
with open(filename, 'w', encoding="utf-8") as fhndl:
fhndl.write(self.generate_report_string())