JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / reporters / report / pdf / story.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 __doc__ = """
11 Story Decorator contains ImageStory, HeaderStory, PageBreakStory,
12 TableStory, LinePlotStory, TitleStory, ParagraphStory
13 """
14 import sys
15 import os
16 from reportlab.platypus import PageBreak
17 from reportlab.lib import colors
18 from reportlab.platypus.tableofcontents import TableOfContents
19 from styles import *
20 from element import *
21
22
23 class Story(object):
24     def __init__(self):
25         self._storylist = []
26
27     @property
28     def storylist(self):
29         return self._storylist
30
31
32 class StoryDecorator(Story):
33     def __init__(self, story, data=None, style=None):
34         self._story = story
35         self._data = data
36         self._style = style
37         print self._data
38         self.new_story()
39
40     #       print self._story.storylist
41     @property
42     def storylist(self):
43         return self._story.storylist
44
45     def new_story(self):
46         raise NotImplementedError("abstract StoryDecorator")
47
48
49 class ImageStory(StoryDecorator):
50     def new_story(self):
51         print "Image Story"
52         for filename in self._data:
53             if os.path.exists(filename) == False:
54                 print "not find %s" % filename
55                 continue
56             if 'Traffic-types' in filename:
57                 style = is_traffic
58                 image_height = style.image_height
59                 image_width = style.image_width
60                 image_hAlign = style.image_hAlign
61                 image_vAlign = style.image_vAlign
62                 self._story.storylist.append(
63                     eImage(filename, image_width, image_height, hAlign=image_hAlign, vAlign=image_vAlign))
64             else:
65                 style = is_default
66                 image_height = style.image_height
67                 image_width = style.image_width
68                 image_hAlign = style.image_hAlign
69                 image_vAlign = style.image_vAlign
70                 #    self._story.storylist.append(eGraphicsTable([[' ' * 5, eImage(filename, image_width, image_height, hAlign=image_hAlign, vAlign=image_vAlign)]], ts_left).table)
71                 self._story.storylist.append(
72                     eImage(filename, image_width, image_height, hAlign=image_hAlign, vAlign=image_vAlign))
73
74
75 class HeaderStory(StoryDecorator):
76     def new_story(self):
77         print "header story"
78         self._story.storylist.append(PageBreak())
79
80
81 class PageBreakStory(StoryDecorator):
82     def new_story(self):
83         print "PageBreak story"
84         self._story.storylist.append(PageBreak())
85
86
87 class TableOfContentsStory(StoryDecorator):
88     def new_story(self):
89         print "TableOfContents story"
90         self._data = [" ", " ", "Table Of Contents", ""]
91         style = ps_head_lv4
92         self._story.storylist.append(eParagraph(self._data, style).para)
93         toc = TableOfContents()
94         toc.levelStyles = [ps_head_lv7, ps_head_lv8, ps_head_lv9]
95         self._story.storylist.append(toc)
96
97
98 class SpaceStory(StoryDecorator):
99     def new_story(self):
100         style = ps_space
101         self._story.storylist.append(eParagraph([" ", " "], style).para)
102
103
104 class TableStory(StoryDecorator):
105     def new_story(self):
106         print "table story"
107         style = ts_default
108         if self._style == 1:
109             self._story.storylist.append(eDataTable(self._data, style).table)
110         elif self._style ==2:
111             style = ts_left
112             self._story.storylist.append(eCommonTable(self._data, style).table)
113         elif self._style == 3:
114             self._story.storylist.append(eConfigTable(self._data, style).table)
115         elif self._style == 4:
116             self._story.storylist.append(eOptionsTable(self._data, style).table)
117         elif self._style == 5:
118             self._story.storylist.append(eProfileTable(self._data, style).table)
119         elif self._style == 6:
120             self._story.storylist.append(eSummaryTable(self._data, style).table)
121         elif self._style == 7:
122             self._story.storylist.append(eScenarioTable(self._data, style).table)
123         elif self._style == 8:
124             self._story.storylist.append(eGitInfoTable(self._data, style).table)
125
126
127 class LinePlotStory(StoryDecorator):
128     def new_story(self):
129         print "LinePlot"
130         style = lps_default
131         if not self._data:
132             print "data error "
133             return
134         data = eGraphicsTable([[eLinePlot(self._data, style).draw]]).table
135         if data:
136             self._story.storylist.append(data)
137
138
139 class LineChartStory(StoryDecorator):
140     def new_story(self):
141         print "LineChartStory: "
142         style = lcs_default
143         if not self._data:
144             print "data error "
145             return
146         data = eGraphicsTable([[eHorizontalLineChart(self._data, style).draw]]).table
147         if data:
148             self._story.storylist.append(data)
149
150
151 class BarChartStory(StoryDecorator):
152     def new_story(self):
153         print "BarChartStory: "
154         style = bcs_default
155         if not self._data:
156             print "data error "
157             return
158
159         data = eGraphicsTable([[eBarChartColumn(self._data, style).draw]]).table
160         if data:
161             self._story.storylist.append(data)
162
163
164 class ParagraphStory(StoryDecorator):
165     def new_story(self):
166         print "Paragraph Story"
167         style = ps_body
168         if not self._data:
169             print "data error "
170             return
171         data = eParagraph(self._data, style).para
172         if data:
173             self._story.storylist.append(data)
174
175
176 class TitleStory(StoryDecorator):
177     def new_story(self):
178         print "Paragraph Story"
179         if self._style - 1 in range(9):
180             style = eval("ps_head_lv" + "%d" % self._style)
181         else:
182             style = ps_body
183         # print style
184         # print self._data
185
186         self._story.storylist.append(eParagraph(self._data, style).para)