From 7880d28cc89f58e3d76b0d0f68fb1e33a29fdc80 Mon Sep 17 00:00:00 2001 From: Caio Carrara Date: Sun, 9 Dec 2018 21:35:35 -0200 Subject: [PATCH] html: Replaces pystache by jinja to template definition Since the pystache project seems to be dead, without recent updates and has not support planned for Python 3.7+, this change replace it by jinja template engine. Signed-off-by: Caio Carrara --- .../html/avocado_result_html/__init__.py | 75 +- .../resources/templates/report.mustache | 873 ------------------ .../templates/{results.j2 => results.html} | 29 +- optional_plugins/html/setup.py | 2 +- selftests/functional/test_basic.py | 4 +- 5 files changed, 30 insertions(+), 953 deletions(-) delete mode 100644 optional_plugins/html/avocado_result_html/resources/templates/report.mustache rename optional_plugins/html/avocado_result_html/templates/{results.j2 => results.html} (88%) diff --git a/optional_plugins/html/avocado_result_html/__init__.py b/optional_plugins/html/avocado_result_html/__init__.py index 29b037b5..933e7f84 100644 --- a/optional_plugins/html/avocado_result_html/__init__.py +++ b/optional_plugins/html/avocado_result_html/__init__.py @@ -23,8 +23,7 @@ import subprocess import sys import time -import pkg_resources -import pystache +import jinja2 as jinja from avocado.core import exit_codes from avocado.core.output import LOG_UI @@ -43,24 +42,6 @@ class ReportModel(object): self.html_output = html_output self.html_output_dir = os.path.abspath(os.path.dirname(html_output)) - def update(self, **kwargs): - """ - Hook for updates not supported - """ - - def get(self, key, default): - value = getattr(self, key, default) - if callable(value): - return value() - else: - return value - - def job_unique_id(self): - return self.result.job_unique_id - - def tests_total_time(self): - return "%.2f" % self.result.tests_total_time - def results_dir(self, relative_links=True): results_dir = os.path.abspath(os.path.dirname( self.result.logfile)) @@ -72,25 +53,6 @@ class ReportModel(object): def results_dir_basename(self): return os.path.basename(self.results_dir(False)) - def tests_total(self): - return self.result.tests_total - - def passed(self): - return self.result.passed - - def warned(self): - return self.result.warned - - def rate(self): - total = float(self.result.tests_total - self.result.skipped - - self.result.cancelled) - succeeded = float(self.result.passed + self.result.warned) - if total > 0: - pr = 100 * (succeeded / total) - else: - pr = 0 - return "%.2f" % pr - def _get_sysinfo(self, sysinfo_file): sysinfo_path = os.path.join(self.results_dir(False), 'sysinfo', 'pre', sysinfo_file) @@ -101,6 +63,7 @@ class ReportModel(object): sysinfo_contents = "Error reading %s: %s" % (sysinfo_path, details) return sysinfo_contents + @property def hostname(self): return self._get_sysinfo('hostname').strip() @@ -200,12 +163,15 @@ class ReportModel(object): s_id += 1 return sysinfo_list + @property def sysinfo_pre(self): return self._sysinfo_phase('pre') + @property def sysinfo_profile(self): return self._sysinfo_phase('profile') + @property def sysinfo_post(self): return self._sysinfo_phase('post') @@ -234,31 +200,12 @@ class HTMLResult(Result): preexec_fn=setsid) def _render(self, result, output_path): - context = ReportModel(result=result, html_output=output_path) - template = pkg_resources.resource_string( - 'avocado_result_html', - 'resources/templates/report.mustache') - - # pylint: disable=E0611 - try: - if hasattr(pystache, 'Renderer'): - renderer = pystache.Renderer('utf-8', 'utf-8') - report_contents = renderer.render(template, context) - else: - from pystache import view - v = view.View(template, context) - report_contents = v.render('utf8') - except UnicodeDecodeError as details: - # FIXME: Remove me when UnicodeDecodeError problem is fixed - LOG_UI.critical("\n%s", ("-" * 80)) - LOG_UI.critical("HTML failed to render the template: %s\n\n", - template) - LOG_UI.critical("-" * 80) - LOG_UI.critical("%s:\n\n", details) - LOG_UI.critical("%r", getattr(details, "object", - "object not found")) - LOG_UI.critical("-" * 80) - raise + env = jinja.Environment( + loader=jinja.PackageLoader('avocado_result_html'), + autoescape=jinja.select_autoescape(['html', 'xml']), + ) + template = env.get_template('results.html') + report_contents = template.render({'data': ReportModel(result, output_path)}) with codecs.open(output_path, 'w', 'utf-8') as report_file: report_file.write(report_contents) diff --git a/optional_plugins/html/avocado_result_html/resources/templates/report.mustache b/optional_plugins/html/avocado_result_html/resources/templates/report.mustache deleted file mode 100644 index 7cee7d8f..00000000 --- a/optional_plugins/html/avocado_result_html/resources/templates/report.mustache +++ /dev/null @@ -1,873 +0,0 @@ - - - - - - - Avocado Job Report - - - - - - - - - - - -
- - -
-
- -
-

Summary

-
- - - - - - - - - - - - - - - - -
ID{{job_unique_id}}
Host{{hostname}}
Results Dir{{results_dir_basename}}
Cumulative test time{{tests_total_time}} s
StatsFrom {{tests_total}} tests executed, {{passed}} passed and {{warned}} warned - success rate of {{rate}}% (excluding SKIP and CANCEL)
-
-
- - - - - - - - - - - - - - {{#tests}} - - - - - - - - - - - {{/tests}} -
Start TimeUIDTest NameVariantStatusTime (sec)InfoDebug Log
{{time_start}}{{uid}}{{name}}{{variant}}{{status}}{{time}}{{& fail_reason}}{{logfile_basename}}
- - -
-
-

Sysinfo

-
-
- - -
-
- -
-
-
- {{#sysinfo_pre}} -
- -
-
-
{{contents}}
- {{& err}} -
-
-
- {{/sysinfo_pre}} -
-
-
-
-
- - - -
-
- -
-
-
- {{#sysinfo_post}} -
- -
-
-
{{contents}}
-
-
-
- {{/sysinfo_post}} -
-
-
-
-
- - - -
-
- -
-
-
- {{#sysinfo_profile}} -
- -
-
-
{{contents}}
-
-
-
- {{/sysinfo_profile}} -
-
-
-
-
- - -
-
- - -
- - - diff --git a/optional_plugins/html/avocado_result_html/templates/results.j2 b/optional_plugins/html/avocado_result_html/templates/results.html similarity index 88% rename from optional_plugins/html/avocado_result_html/templates/results.j2 rename to optional_plugins/html/avocado_result_html/templates/results.html index c1c1a0b0..ed4cf5af 100644 --- a/optional_plugins/html/avocado_result_html/templates/results.j2 +++ b/optional_plugins/html/avocado_result_html/templates/results.html @@ -29,23 +29,26 @@ - + - + - + - + - +
ID{{ job_unique_id }}{{ data.result.job_unique_id }}
Host{{ hostname }}{{ data.hostname }}
Results Dir{{ results_dir_basename }}{{ data.results_dir_basename() }}
Cumulative test time{{ tests_total_time }} s{{ '%.2f'|format(data.result.tests_total_time) }} s
StatsFrom {{ tests_total }} tests executed, {{ passed }} passed and {{ warned }} warned - success rate of {{ rate }}% (excluding SKIP and CANCEL)From {{ data.result.tests_total }} tests executed, + {{ data.result.passed }} passed and + {{ data.result.warned }} warned - success rate of + {{ '%.2f'|format(data.result.rate) }}% (excluding SKIP and CANCEL)
@@ -63,16 +66,16 @@ Debug Log - {% for test in tests %} - + {% for test in data.tests %} + {{ test.time_start }} {{ test.uid }} {{ test.name }} {{ test.variant }} {{ test.status }} {{ test.time }} - {{ test.fail_reason }} - {{ logfile_basename }} + {{ test.fail_reason|safe }} + {{ test.logfile_basename }} {% endfor %} @@ -92,7 +95,7 @@
- {% for sysinfo in sysinfo_pre %} + {% for sysinfo in data.sysinfo_pre %}
@@ -123,7 +126,7 @@
- {% for sysinfo in sysinfo_post %} + {% for sysinfo in data.sysinfo_post %}