This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
doc.py
executable file
·321 lines (241 loc) · 7.13 KB
/
doc.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#! /usr/bin/env python
import argparse as ap
import os
import sys
import re
from string import join
from glob import glob
#re_title_element = re.compile(r'<title>(.*)</title>')
#re_description = re.compile(r'<description>(.*)</description>')
#re_product = re.compile(r'<description>(.*)</description>')
re_title_attribute = re.compile(r'title=["|\'](.*)["|\']')
re_viewfile_link = re.compile(r'<viewfile-link\s+file=["|\'](.*)["|\']\s*/>')
re_link = re.compile(r'<a\s+href=["|\'](.*)["|\']\s*>(.*)</a>')
def die (msg, code) :
print(msg)
sys.exit(code)
def read_tag(line, f, tag):
content = " "
endtag = "</%s>" % tag
starttag = "<%s>" % tag
if endtag in line:
content = line.replace(endtag, " ")
content = content.replace(starttag, " ")
return content.strip()
while line:
if endtag in line:
break
line = f.readline()
if endtag in line:
break
content = content + line
return content
def read_title(line):
if not "title" in line:
return ""
m = re_title_attribute.search(line)
if not m:
return ""
title = m.group(1)
return title
def replace_line(line):
line = line.replace("<var>", "'''''")
line = line.replace("</var>", "'''''")
line = line.replace("<deftable>", "<table>")
line = line.replace("</deftable>", "</table>")
if "<viewfile-link" in line:
m = re_viewfile_link.search(line)
if m:
file_name=m.group(1)
line = line.replace(m.group(0),"<code>%s</code>" % file_name)
if "<a" in line:
m = re_link.search(line)
if m:
href=m.group(1)
body=m.group(2)
line = line.replace(m.group(0),"[%s %s]" % (href,body))
return line
def example_extract(line, content, f):
if "<example" in line:
title = read_title(line)
if verbose: print "in example " + title
content = content + "====%s====\n" % title
content = content + "<pre>\n"
while line:
line = f.readline()
if "</example>" in line:
content = content + "</pre>\n"
return f.readline(), content
line = line.replace("<", "<")
line = line.replace(">", ">")
line = line.replace("</pre>", "</pre>")
content = content + line
if verbose: print "done with example " + title
else:
return line, content
def result_extract(line, content, f):
if "<results" in line:
if verbose: print "in results "
content = content + "====%s====\n" % "results"
content = content + "<pre>\n"
while line:
line = f.readline()
if "</results>" in line:
content = content + "</pre>\n"
return f.readline(), content
content = content + line
else: return line, content
class Document:
def read_file(self, line, f):
if verbose: print "read document"
while line:
line = f.readline()
if "<header>" in line:
if verbose: print "read header"
self.header = Header()
self.header.read_file(line, f)
elif "<body>" in line:
if verbose: print "read body"
self.body = Body()
self.body.read_file(line, f)
class Header:
def __init__(self):
self.description=None
def read_file(self, line, f):
#print "read header"
while line:
line = f.readline()
#print "header out", line
if "<title>" in line:
self.title = read_tag(line, f, "title")
elif "<description>" in line:
self.description = read_tag(line, f, "description")
elif "</header>" in line:
break
#print "last line", line
#print "description", self.description
class Body:
def read_file(self, line, f):
sections = []
while line:
line = f.readline()
line = replace_line(line)
if "<s1" in line:
if verbose: print "reading section 1"
section = Section()
section.read_file(line, f)
sections.append(section)
if verbose: "done reading section 1"
if "<summary>" in line:
if verbose: print "reading summary"
self.summary = read_tag(line, f, "summary")
if verbose: print "done reading summary"
if "</body>" in line:
break
self.sections = sections
class Section:
def read_file(self, line, f):
self.title = read_title(line)
if verbose: print "section title " + self.title
content = ""
while line:
line = f.readline()
line = replace_line(line)
line,content = example_extract(line, content, f)
#print ("is tuple example", type(line))
line,content = result_extract(line, content,f)
#print ("is tuple results", type(line))
if "<s2" in line or "<s3" in line:
title = read_title(line)
if "<s2" in line:
content = content + "===%s===\n" % title
if "<s3" in line:
content = content + "====%s====\n" % title
content = content + "\n"
while line:
line = f.readline()
line = replace_line(line)
line,content = example_extract(line, content, f)
#print ("1", type(line))
line,content = result_extract(line, content,f)
#print ("2", type(line))
if "</s2>" in line:
break
if "</s3>" in line:
break
content = content + line
elif "</s1>" in line:
break
else:
content = content + line
self.content = content
class FileProcessor:
def __init__(self, args):
self.verbose = args.verbose
self.files = args.files
if args.output:
self.output = args.output
def create_wiki_pages (self):
if self.verbose: print ('files', self.files)
for f in self.files:
files = glob(f)
for myfile in files:
self.create_wiki_page(myfile)
def create_wiki_page (self, file):
if self.verbose: print ('file', file)
if not os.path.exists(file):
die ("%s file does not exist" % file, 6)
f = open (file, 'r')
line = "used to read line by line from the file"
try:
while line:
line = f.readline()
#print (line)
if "<document>" in line:
doc = Document()
doc.read_file(line, f)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
finally:
f.close()
global outputfile
if self.output:
if not outputfile:
outputfile = open(self.output, 'w')
def printit(it):
global outputfile
if self.output:
outputfile.write(it + "\n")
else:
print (it)
if verbose: print ("\n" * 5)
if verbose: print ("DOC:", doc.header.title, file)
printit ("=%s=" % doc.header.title)
if doc.header.description:
printit(doc.header.description)
for section in doc.body.sections:
if section.title:
printit("==%s==" % section.title)
printit (section.content)
verbose = False
outputfile = None
def create_wiki_pages(args) :
global verbose
verbose = args.verbose
fp = FileProcessor(args)
fp.create_wiki_pages()
arg_parser = ap.ArgumentParser(description="Convert an xtp into a wiki page", epilog=
"""Generates files for Eclipse and Wiki.""")
arg_parser.add_argument('files', metavar='FILES', nargs="+",
help="Files to process")
arg_parser.add_argument('--verbose', '-v', dest='verbose', action='store_true',
help='Verbose mode')
arg_parser.add_argument('--wiki', '-w', dest='action', action='store_const',
const=create_wiki_pages, default=create_wiki_pages,
help='Create a wiki page based on a xtp test')
arg_parser.add_argument('--output', '-o', dest='output', action='store')
args = arg_parser.parse_args()
args.action(args)
if outputfile:
print "Done"
outputfile.close()