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