587
|
1 |
#============================================================================
|
|
2 |
#Name : rtfutils.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
|
20 |
import csv
|
|
21 |
import os
|
|
22 |
import PyRTF
|
|
23 |
import StringIO
|
|
24 |
import re
|
|
25 |
import logging
|
|
26 |
|
|
27 |
class RTFUtils(object):
|
|
28 |
""" RTF utility module """
|
|
29 |
def __init__(self, template):
|
|
30 |
""" template would be a RTF file to modify """
|
|
31 |
self.template = template
|
|
32 |
|
|
33 |
self.logger = logging.getLogger('test.relnotes')
|
|
34 |
logging.basicConfig(level=logging.DEBUG)
|
|
35 |
|
|
36 |
def rtftable(self, errorsfilename, outputfilename, tagtoreplace):
|
|
37 |
""" Create a .rtf file from the errors.csv file. """
|
|
38 |
|
|
39 |
errors = file(errorsfilename, 'rb')
|
|
40 |
template = file(self.template, 'rb' )
|
|
41 |
output = file(outputfilename, 'w' )
|
|
42 |
|
|
43 |
self._rtftable(errors, output, tagtoreplace, template)
|
|
44 |
|
|
45 |
errors.close()
|
|
46 |
output.close()
|
|
47 |
template.close()
|
|
48 |
|
|
49 |
def _rtftable(self, errors, output, tagtoreplace, template):
|
|
50 |
PyRTF.Elements.StandardColours.append(PyRTF.PropertySets.Colour('NokiaBlue', 153, 204, 255))
|
|
51 |
|
|
52 |
DR = PyRTF.Renderer()
|
|
53 |
doc = PyRTF.Document()
|
|
54 |
ss = doc.StyleSheet
|
|
55 |
section = PyRTF.Section()
|
|
56 |
doc.Sections.append( section )
|
|
57 |
|
|
58 |
table = PyRTF.Table( PyRTF.TabPS.DEFAULT_WIDTH * 7,
|
|
59 |
PyRTF.TabPS.DEFAULT_WIDTH * 3,
|
|
60 |
PyRTF.TabPS.DEFAULT_WIDTH * 3 )
|
|
61 |
|
|
62 |
reader = csv.reader(errors)
|
|
63 |
|
|
64 |
style = None
|
|
65 |
for row in reader:
|
|
66 |
assert len(row) == 3
|
|
67 |
|
|
68 |
if style == None:
|
|
69 |
style = ss.ParagraphStyles.Heading2
|
|
70 |
else:
|
|
71 |
style = ss.ParagraphStyles.Normal
|
|
72 |
|
|
73 |
# Handle each value from the row
|
|
74 |
rowcell = []
|
|
75 |
|
|
76 |
for value in row:
|
|
77 |
cell = PyRTF.Text( value )
|
|
78 |
rowcell.append(PyRTF.Cell( PyRTF.Paragraph(style, cell) ))
|
|
79 |
table.AddRow( *rowcell )
|
|
80 |
|
|
81 |
section.append( table )
|
|
82 |
string = StringIO.StringIO()
|
|
83 |
DR.Write( doc, string )
|
|
84 |
|
|
85 |
keep = ''
|
|
86 |
for line in string.getvalue().splitlines():
|
|
87 |
if keep != '' or line.startswith('{\\trowd'):
|
|
88 |
keep += line
|
|
89 |
|
|
90 |
#remove last '}'
|
|
91 |
keep = keep[0:-1]
|
|
92 |
|
|
93 |
for line in template:
|
|
94 |
line = line.replace(tagtoreplace, keep)
|
|
95 |
output.write(line)
|
|
96 |
|
|
97 |
def rtfimage(self, image, outputfilename, tagtoreplace):
|
|
98 |
""" Replaces tagtoreplace in a RTF file with a image """
|
|
99 |
|
|
100 |
template = file(self.template, 'rb' )
|
|
101 |
output = file(outputfilename, 'w' )
|
|
102 |
|
|
103 |
self._rtfimage(image, output, tagtoreplace, template)
|
|
104 |
|
|
105 |
output.close()
|
|
106 |
template.close()
|
|
107 |
|
|
108 |
def _rtfimage(self, image, output, tagtoreplace, template):
|
|
109 |
TEMP_FILE = 'image_temp.rtf'
|
|
110 |
|
|
111 |
DR = PyRTF.Renderer()
|
|
112 |
doc = PyRTF.Document()
|
|
113 |
ss = doc.StyleSheet
|
|
114 |
section = PyRTF.Section()
|
|
115 |
doc.Sections.append( section )
|
|
116 |
|
|
117 |
section.append( PyRTF.Image( image ) )
|
|
118 |
|
|
119 |
tempOutput = file( TEMP_FILE, 'w' )
|
|
120 |
DR.Write( doc, tempOutput )
|
|
121 |
|
|
122 |
tempOutput = file( TEMP_FILE, 'rb' )
|
|
123 |
|
|
124 |
keep = ''
|
|
125 |
for line in tempOutput:
|
|
126 |
if keep != '':
|
|
127 |
keep += line
|
|
128 |
elif line.startswith('{\pict'):
|
|
129 |
keep = line
|
|
130 |
|
|
131 |
#remove last '}'
|
|
132 |
keep = keep[0:-1]
|
|
133 |
|
|
134 |
tempOutput.close()
|
|
135 |
|
|
136 |
for line in template:
|
|
137 |
line = line.replace(tagtoreplace, keep)
|
|
138 |
output.write(line)
|
|
139 |
|
|
140 |
os.remove(TEMP_FILE)
|
|
141 |
|
|
142 |
def rtfconvert(self, inputfilename, outputfilename):
|
|
143 |
""" Converts a property file to be RTF link syntax """
|
|
144 |
inputfile = file( inputfilename, 'r' )
|
|
145 |
outputfile = file( outputfilename, 'w' )
|
|
146 |
|
|
147 |
self._rtfconvert(inputfile, outputfile)
|
|
148 |
|
|
149 |
inputfile.close()
|
|
150 |
outputfile.close()
|
|
151 |
|
|
152 |
def _rtfconvert(self, inputfile, outputfile):
|
|
153 |
p = re.compile(r'(.+=)((\\\\|http|\.\\|ftp)(.+))')
|
|
154 |
for line in inputfile:
|
|
155 |
newline = line
|
|
156 |
|
|
157 |
#fix bad links generated in ant
|
|
158 |
if newline.count('\\\\')>0:
|
|
159 |
newline = newline.replace('//','\\')
|
|
160 |
newline = newline.replace('/','\\')
|
|
161 |
|
|
162 |
if "\\n" in newline:
|
|
163 |
newline = newline.replace("\\n", " \\\\line ")
|
|
164 |
else:
|
|
165 |
newline = newline.replace('\\','\\\\\\\\\\\\\\\\')
|
|
166 |
|
|
167 |
|
|
168 |
newline = p.sub('\g<1>{_backslash_field{_backslash_*_backslash_fldinst HYPERLINK \g<2>}}', newline)
|
|
169 |
|
|
170 |
newline = newline.replace('_backslash_', r'\\')
|
|
171 |
|
|
172 |
outputfile.write(newline)
|