Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/planemo/reports/build_report.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import base64 | |
2 | |
3 from galaxy.util import strip_control_characters | |
4 from jinja2 import Environment, PackageLoader | |
5 from pkg_resources import resource_string | |
6 | |
7 TITLE = "Test Results (powered by Planemo)" | |
8 | |
9 | |
10 def build_report(structured_data, report_type="html", **kwds): | |
11 """ Use report_{report_type}.tpl to build page for report. | |
12 """ | |
13 environment = dict( | |
14 title=TITLE, | |
15 raw_data=structured_data, | |
16 ) | |
17 environment = __inject_summary(environment) | |
18 __fix_test_ids(environment) | |
19 | |
20 if report_type == 'html': | |
21 # The HTML report format needs a lot of extra, custom data. | |
22 # IMO, this seems to suggest it should be embedded. | |
23 environment['title'] = None | |
24 markdown = template_data(environment, 'report_markdown.tpl') | |
25 environment['title'] = TITLE | |
26 environment['raw_data'] = base64.b64encode(markdown.encode('utf-8')).decode('utf-8') | |
27 environment.update({ | |
28 'custom_style': __style("custom.css"), | |
29 'custom_script': __script("custom"), | |
30 'bootstrap_style': __style("bootstrap.min.css"), | |
31 'jquery_script': __script("jquery.min"), | |
32 'bootstrap_script': __script("bootstrap.min"), | |
33 'markdown_it_script': __script('markdown-it.min'), | |
34 }) | |
35 | |
36 return template_data(environment, 'report_%s.tpl' % report_type) | |
37 | |
38 | |
39 def template_data(environment, template_name, **kwds): | |
40 """Build an arbitrary templated page. | |
41 """ | |
42 env_kwargs = {} | |
43 if template_name == 'report_markdown.tpl': | |
44 env_kwargs['keep_trailing_newline'] = True | |
45 env_kwargs['trim_blocks'] = True | |
46 env = Environment(loader=PackageLoader('planemo', 'reports'), **env_kwargs) | |
47 env.filters['strip_control_characters'] = lambda x: strip_control_characters(x) if x else x | |
48 template = env.get_template(template_name) | |
49 return template.render(**environment) | |
50 | |
51 | |
52 def __fix_test_ids(environment): | |
53 for test in environment['raw_data']['tests']: | |
54 test_data = test.get('data') | |
55 if test_data and test_data.get('tool_id'): | |
56 test['id'] = "%s (Test #%s)" % (test_data['tool_id'], test_data['test_index'] + 1) | |
57 | |
58 | |
59 def __inject_summary(environment): | |
60 total = 0 | |
61 errors = 0 | |
62 failures = 0 | |
63 skips = 0 | |
64 for test in environment['raw_data']['tests']: | |
65 total += 1 | |
66 test_data = test.get('data') | |
67 if test_data: | |
68 status = test_data.get('status') | |
69 if status == 'error': | |
70 errors += 1 | |
71 elif status == 'failure': | |
72 failures += 1 | |
73 elif status == 'skipped': | |
74 skips += 1 | |
75 environment['raw_data']['results'] = { | |
76 'total': total, | |
77 'errors': errors, | |
78 'failures': failures, | |
79 'skips': skips, | |
80 } | |
81 if 'suitename' not in environment: | |
82 environment['raw_data']['suitename'] = TITLE | |
83 return environment | |
84 | |
85 | |
86 def __style(filename): | |
87 resource = __load_resource(filename) | |
88 return "<style>%s</style>" % resource | |
89 | |
90 | |
91 def __script(short_name): | |
92 resource = __load_resource("%s.js" % short_name) | |
93 return "<script>%s</script>" % resource | |
94 | |
95 | |
96 def __load_resource(name): | |
97 return resource_string( | |
98 __name__, name | |
99 ).decode('UTF-8') |