Merge "Add Factor Testcase RX_Cache_Size"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / controller / reporters / report / pdf / pdfcreator.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 from vstf.controller.reporters.report.pdf.pdftemplate import PdfVswitch
12 from vstf.controller.reporters.report.pdf.story import TitleStory, SpaceStory, ImageStory, TableStory, \
13     LinePlotStory, Story, TableOfContentsStory, PageBreakStory, ParagraphStory, BarChartStory
14 import vstf.common.candy_text as candy
15 from vstf.controller.reporters.report.provider.pdf_provider import PdfProvider
16 from vstf.controller.settings.template_settings import TemplateSettings
17
18 import os
19 import logging
20
21 LOG = logging.getLogger(__name__)
22
23
24 class PdfCreator(object):
25
26     def __init__(self, provider):
27         self._provider = provider
28         self._story = []
29         self._pdf = None
30
31     def create_pdf(self):
32         theme = self._provider.get_theme
33         self._pdf = PdfVswitch(theme["title"],
34                                theme["logo"],
35                                theme["header"],
36                                theme["footer"],
37                                theme["note"],
38                                theme["style"])
39
40     def save_pdf(self, ofile):
41         self._pdf.generate(self._story, ofile)
42
43     def add_coverpage(self):
44         story = Story()
45         story = PageBreakStory(story)
46         self._story += story.storylist
47
48     def add_contents(self):
49         if self._provider.ifcontents:
50             story = Story()
51             story = TableOfContentsStory(story)
52             self._story += story.storylist
53
54     def create_story(self):
55         self.add_coverpage()
56         self.add_contents()
57         self.add_context()
58
59     def create(self, ofile):
60         self.create_pdf()
61         self.create_story()
62         self.save_pdf(ofile)
63
64     def add_context(self):
65         context = self._provider.get_context
66         self._story += self._raw_context(context)
67
68     def _raw_context(self, context, ci=0, si=0, ui=0, level=-1):
69         _story = []
70         for key, value in sorted(context.items()):
71             LOG.info(key)
72             LOG.info(value)
73             _sn, _node, _style = candy.text2tuple(key)
74             if _node in candy.dom:
75                 if _node == candy.chapter:
76                     ci = _style
77                 elif _node == candy.section:
78                     si = _style
79                 else:
80                     ui = _style
81                 _story += self._raw_context(value, ci, si, ui, level + 1)
82
83             else:
84                 story = Story()
85                 LOG.info("node: %s  %s" % (_node, candy.title))
86                 if _node == candy.title:
87                     assert value
88                     if level in range(len(candy.dom)):
89                         if level == 0:
90                             value[0] = "Chapter %s %s" % (ci, value[0])
91                             story = PageBreakStory(story)
92                         elif level == 1:
93                             value[0] = "%s.%s %s" % (ci, si, value[0])
94                         else:
95                             value[0] = "%s.%s.%s %s" % (ci, si, ui, value[0])
96                         LOG.info(value)
97                         story = TitleStory(story, data=value, style=_style)
98                 elif _node == candy.table:
99                     story = TableStory(story, data=value, style=_style)
100                 elif _node == candy.figure:
101                     story = ImageStory(story, data=value, style=_style)
102                 elif _node == candy.paragraph:
103                     story = ParagraphStory(story, data=value, style=_style)
104                 elif _node == candy.plot:
105                     story = LinePlotStory(story, data=value, style=_style)
106                 elif _node == candy.chart:
107                     story = BarChartStory(story, data=value, style=_style)
108                 elif _node == candy.space:
109                     assert isinstance(value, int)
110                     for i in range(value):
111                         story = SpaceStory(story)
112                 _story += story.storylist
113         return _story
114
115
116 def main():
117     from vstf.common.log import setup_logging
118     setup_logging(
119         level=logging.DEBUG,
120         log_file="/var/log/pdf-creator.log",
121         clevel=logging.INFO)
122
123     out_file = "vstf_report.pdf"
124
125     info = TemplateSettings()
126     provider = PdfProvider(info.settings)
127     reporter = PdfCreator(provider)
128     reporter.create(out_file)
129
130 if __name__ == '__main__':
131     main()