summaryrefslogtreecommitdiffstats
path: root/testsuites/vstf/vstf_scripts/vstf/controller/reporters/report/pdf/pdftemplate.py
blob: 7e287814bc433a7bc00c78fae4f756a6fd2b5b4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import time

from reportlab.platypus.doctemplate import SimpleDocTemplate
from reportlab.platypus import PageBreak
from vstf.controller.reporters.report.pdf.styles import TemplateStyle, ps_head_lv1, ps_head_lv2, ps_head_lv3
import vstf.common.constants as cst
from functools import reduce


class BaseDocTemplate(SimpleDocTemplate):

    def __init__(self, filename, **kw):
        self.allowSplitting = 0
        SimpleDocTemplate.__init__(self, filename, **kw)

    def afterFlowable(self, flowable):
        """Registers TOC entries."""
        if flowable.__class__.__name__ == 'Paragraph':
            text = flowable.getPlainText()
            style = flowable.style.name
            if style == ps_head_lv1.name:
                self.notify('TOCEntry', (0, text, self.page - 1))
            elif style == ps_head_lv2.name:
                self.notify('TOCEntry', (1, text, self.page - 1))
            elif style == ps_head_lv3.name:
                self.notify('TOCEntry', (2, text, self.page - 1))


class PdfTemplate(object):

    def __init__(self, title, logo, header, footer, note=[], style="default"):
        self._style = TemplateStyle(name=style)
        self._title = title
        self._logo = logo[0]
        #self._header = header[0]
        self._footer = footer
        self._note = note
        info = " Generated on %s " % time.strftime(
            cst.TIME_FORMAT2, time.localtime())
        self._note += [info]

    def myFirstPage(self, canvas, doc):
        raise NotImplementedError("abstract StoryDecorator")

    def myLaterPages(self, canvas, doc):
        raise NotImplementedError("abstract StoryDecorator")

    def generate(self, story, output):
        sizes = (self._style.page_wight, self._style.page_height)
        doc = BaseDocTemplate(output, pagesize=sizes)
        #    doc.build(story, onFirstPage=self.myFirstPage, onLaterPages=self.myLaterPages)
        doc.multiBuild(
            story,
            onFirstPage=self.myFirstPage,
            onLaterPages=self.myLaterPages)


class PdfVswitch(PdfTemplate):

    def myFirstPage(self, canvas, doc):
        canvas.saveState()
        title_lines = len(self._title)
        line_size = [self._style.title_size] * title_lines
        line_size.append(0)

        canvas.drawImage(
            self._logo,
            (self._style.page_wight -
             self._style.logo_width) /
            2.0,
            self._style.page_height /
            2.0 +
            (
                1 +
                self._style.title_leading) *
            reduce(
                lambda x,
                y: x +
                y,
                line_size),
            self._style.logo_width,
            self._style.logo_height)
        for i in range(title_lines):
            canvas.setFont(self._style.title_font, line_size[i])
            canvas.drawCentredString(
                self._style.page_wight /
                2.0,
                self._style.page_height /
                2.0 +
                (
                    1 +
                    self._style.title_leading) *
                reduce(
                    lambda x,
                    y: x +
                    y,
                    line_size[
                        i +
                        1:]),
                self._title[i])
        size = self._style.body_size
        canvas.setFont(self._style.body_font, size)
        note_line = len(self._note)

        for i in range(note_line):
            print self._note[i]
            canvas.drawCentredString(self._style.page_wight /
                                     2.0, self._style.page_height /
                                     5.0 +
                                     (1 +
                                      self._style.body_leading) *
                                     size *
                                     (note_line -
                                         i -
                                         1), self._note[i])
        size = self._style.body_size - 2
        canvas.setFont(self._style.body_font, size)
        canvas.drawCentredString(self._style.page_wight /
                                 2.0, self._style.page_bottom /
                                 2.0 +
                                 (1 +
                                  self._style.body_leading) *
                                 size, self._footer[0])
        canvas.restoreState()

    def myLaterPages(self, canvas, doc):
        canvas.saveState()
        canvas.setLineWidth(self._style.line_width)
        canvas.line(self._style.page_left,
                    self._style.page_height - self._style.page_top,
                    self._style.page_wight - self._style.page_right,
                    self._style.page_height - self._style.page_top
                    )
        size = self._style.body_size - 2
        canvas.setFont(self._style.body_font, size)
        canvas.drawCentredString(
            self._style.page_wight / 2.0, self._style.page_bottom - 24, "%s%s Page %2d " %
            (self._footer[0], " " * 8, doc.page - 1))
        canvas.restoreState()