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 |
#===============================================================================
|
588
|
19 |
""" rtf utilis"""
|
|
20 |
|
628
|
21 |
# pylint: disable=R0201
|
587
|
22 |
|
|
23 |
import csv
|
|
24 |
import os
|
|
25 |
import PyRTF
|
|
26 |
import StringIO
|
|
27 |
import re
|
|
28 |
import logging
|
|
29 |
|
|
30 |
class RTFUtils(object):
|
|
31 |
""" RTF utility module """
|
|
32 |
def __init__(self, template):
|
|
33 |
""" template would be a RTF file to modify """
|
|
34 |
self.template = template
|
|
35 |
|
|
36 |
self.logger = logging.getLogger('test.relnotes')
|
|
37 |
logging.basicConfig(level=logging.DEBUG)
|
|
38 |
|
|
39 |
def rtftable(self, errorsfilename, outputfilename, tagtoreplace):
|
|
40 |
""" Create a .rtf file from the errors.csv file. """
|
|
41 |
|
|
42 |
errors = file(errorsfilename, 'rb')
|
|
43 |
template = file(self.template, 'rb' )
|
|
44 |
output = file(outputfilename, 'w' )
|
|
45 |
|
628
|
46 |
self.rtftable_file(errors, output, tagtoreplace, template)
|
587
|
47 |
|
|
48 |
errors.close()
|
|
49 |
output.close()
|
|
50 |
template.close()
|
|
51 |
|
628
|
52 |
def rtftable_file(self, errors, output, tagtoreplace, template):
|
588
|
53 |
"""rtf table"""
|
|
54 |
PyRTF.Elements.StandardColours.append(PyRTF.PropertySets.Colour('NokiaBlue', 153, 204, 255))
|
587
|
55 |
|
588
|
56 |
d_r = PyRTF.Renderer()
|
587
|
57 |
doc = PyRTF.Document()
|
588
|
58 |
s_s = doc.StyleSheet
|
587
|
59 |
section = PyRTF.Section()
|
|
60 |
doc.Sections.append( section )
|
|
61 |
|
|
62 |
table = PyRTF.Table( PyRTF.TabPS.DEFAULT_WIDTH * 7,
|
|
63 |
PyRTF.TabPS.DEFAULT_WIDTH * 3,
|
|
64 |
PyRTF.TabPS.DEFAULT_WIDTH * 3 )
|
|
65 |
|
|
66 |
reader = csv.reader(errors)
|
|
67 |
|
|
68 |
style = None
|
|
69 |
for row in reader:
|
|
70 |
assert len(row) == 3
|
|
71 |
|
|
72 |
if style == None:
|
588
|
73 |
style = s_s.ParagraphStyles.Heading2
|
587
|
74 |
else:
|
588
|
75 |
style = s_s.ParagraphStyles.Normal
|
587
|
76 |
|
|
77 |
# Handle each value from the row
|
|
78 |
rowcell = []
|
|
79 |
|
588
|
80 |
for value in row:
|
587
|
81 |
cell = PyRTF.Text( value )
|
|
82 |
rowcell.append(PyRTF.Cell( PyRTF.Paragraph(style, cell) ))
|
|
83 |
table.AddRow( *rowcell )
|
|
84 |
|
|
85 |
section.append( table )
|
|
86 |
string = StringIO.StringIO()
|
588
|
87 |
d_r.Write( doc, string )
|
587
|
88 |
|
|
89 |
keep = ''
|
|
90 |
for line in string.getvalue().splitlines():
|
|
91 |
if keep != '' or line.startswith('{\\trowd'):
|
|
92 |
keep += line
|
|
93 |
|
|
94 |
#remove last '}'
|
|
95 |
keep = keep[0:-1]
|
|
96 |
|
|
97 |
for line in template:
|
|
98 |
line = line.replace(tagtoreplace, keep)
|
|
99 |
output.write(line)
|
|
100 |
|
|
101 |
def rtfimage(self, image, outputfilename, tagtoreplace):
|
|
102 |
""" Replaces tagtoreplace in a RTF file with a image """
|
|
103 |
|
|
104 |
template = file(self.template, 'rb' )
|
|
105 |
output = file(outputfilename, 'w' )
|
|
106 |
|
628
|
107 |
self.rtfimage_file(image, output, tagtoreplace, template)
|
587
|
108 |
|
|
109 |
output.close()
|
|
110 |
template.close()
|
|
111 |
|
628
|
112 |
def rtfimage_file(self, image, output, tagtoreplace, template):
|
588
|
113 |
"""rtf image"""
|
|
114 |
temp_file = 'image_temp.rtf'
|
587
|
115 |
|
588
|
116 |
d_r = PyRTF.Renderer()
|
587
|
117 |
doc = PyRTF.Document()
|
588
|
118 |
_ = doc.StyleSheet
|
587
|
119 |
section = PyRTF.Section()
|
|
120 |
doc.Sections.append( section )
|
|
121 |
|
588
|
122 |
section.append( PyRTF.Image( image ) )
|
587
|
123 |
|
588
|
124 |
tempOutput = file( temp_file, 'w' )
|
|
125 |
d_r.Write( doc, tempOutput )
|
587
|
126 |
|
588
|
127 |
tempOutput = file( temp_file, 'rb' )
|
587
|
128 |
|
|
129 |
keep = ''
|
|
130 |
for line in tempOutput:
|
|
131 |
if keep != '':
|
|
132 |
keep += line
|
|
133 |
elif line.startswith('{\pict'):
|
|
134 |
keep = line
|
|
135 |
|
|
136 |
#remove last '}'
|
|
137 |
keep = keep[0:-1]
|
|
138 |
|
|
139 |
tempOutput.close()
|
|
140 |
|
|
141 |
for line in template:
|
|
142 |
line = line.replace(tagtoreplace, keep)
|
|
143 |
output.write(line)
|
|
144 |
|
588
|
145 |
os.remove(temp_file)
|
587
|
146 |
|
|
147 |
def rtfconvert(self, inputfilename, outputfilename):
|
|
148 |
""" Converts a property file to be RTF link syntax """
|
|
149 |
inputfile = file( inputfilename, 'r' )
|
|
150 |
outputfile = file( outputfilename, 'w' )
|
|
151 |
|
628
|
152 |
self.rtfconvert_file(inputfile, outputfile)
|
587
|
153 |
|
|
154 |
inputfile.close()
|
|
155 |
outputfile.close()
|
|
156 |
|
628
|
157 |
def rtfconvert_file(self, inputfile, outputfile):
|
588
|
158 |
"""rtf convert"""
|
|
159 |
ppp = re.compile(r'(.+=)((\\\\|http|\.\\|ftp)(.+))')
|
587
|
160 |
for line in inputfile:
|
|
161 |
newline = line
|
|
162 |
|
|
163 |
#fix bad links generated in ant
|
|
164 |
if newline.count('\\\\')>0:
|
|
165 |
newline = newline.replace('//','\\')
|
|
166 |
newline = newline.replace('/','\\')
|
|
167 |
|
|
168 |
if "\\n" in newline:
|
|
169 |
newline = newline.replace("\\n", " \\\\line ")
|
|
170 |
else:
|
|
171 |
newline = newline.replace('\\','\\\\\\\\\\\\\\\\')
|
|
172 |
|
|
173 |
|
588
|
174 |
newline = ppp.sub('\g<1>{_backslash_field{_backslash_*_backslash_fldinst HYPERLINK \g<2>}}', newline)
|
587
|
175 |
|
|
176 |
newline = newline.replace('_backslash_', r'\\')
|
|
177 |
|
|
178 |
outputfile.write(newline)
|