JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / reporters / report / html / htmlcreator.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10
11 import logging
12
13 import vstf.common.candy_text as candy
14 from vstf.controller.reporters.report.provider.html_provider import HtmlProvider
15 from vstf.controller.settings.template_settings import TemplateSettings
16 from vstf.controller.settings.html_settings import HtmlSettings
17 from vstf.controller.reporters.report.html.html_base import HtmlBase, pyhtm
18
19 LOG = logging.getLogger(__name__)
20
21
22 class HtmlCreator(HtmlBase):
23     def create_story(self):
24         self.add_context()
25
26     def add_context(self):
27         context = self._provider.get_context
28         self._raw_context(context)
29
30     def _raw_context(self, context, ci=0, si=0, ui=0, level=-1):
31         _story = []
32         for key, value in sorted(context.items()):
33             LOG.info(key)
34             LOG.info(value)
35             _sn, _node, _style = candy.text2tuple(key)
36             if _node in candy.dom:
37                 if _node == candy.chapter:
38                     ci = _style
39                 elif _node == candy.section:
40                     si = _style
41                 else:
42                     ui = _style
43                 self._raw_context(value, ci, si, ui, level + 1)
44
45             else:
46                 LOG.info("node: %s  %s" % (_node, candy.title))
47                 if _node == candy.title:
48                     assert value
49                     if level in range(len(candy.dom)):
50                         if level == 0:
51                             value[0] = "Chapter %s %s" % (ci, value[0])
52                             for title in value:
53                                 self._page << pyhtm.H2(title)
54                         elif level == 1:
55                             value[0] = "%s.%s %s" % (ci, si, value[0])
56                             for title in value:
57                                 self._page << pyhtm.H3(title)
58                         else:
59                             value[0] = "%s.%s.%s %s" % (ci, si, ui, value[0])
60                             for title in value:
61                                 self._page << pyhtm.H3(title)
62
63                 elif _node == candy.table:
64                     self.add_table(value)
65                 elif _node == candy.paragraph:
66                     for para in value:
67                         para = pyhtm.space(2) + para
68                         self._page << pyhtm.P(para)
69
70
71 def unit_test():
72     from vstf.common.log import setup_logging
73     setup_logging(level=logging.DEBUG, log_file="/var/log/html-creator.log", clevel=logging.INFO)
74
75     out_file = "vstf_report.html"
76
77     info = TemplateSettings()
78     html_settings = HtmlSettings()
79     provider = HtmlProvider(info.settings, html_settings.settings)
80     reporter = HtmlCreator(provider)
81     reporter.create(out_file)
82
83 if __name__ == '__main__':
84     unit_test()