1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import csv
21 import os
22 import PyRTF
23 import StringIO
24 import re
25 import logging
26
28
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
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
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
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
153 p = re.compile(r'(.+=)((\\\\|http|\.\\|ftp)(.+))')
154 for line in inputfile:
155 newline = line
156
157
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)
173