1
2
3
4
5
6
7
8
9
10
11
12
13 """
14 Rename PEPs html and generate index page "pep_index.html".
15
16 Read folder contens (*.html files) and if they like PEP html then
17 rename they to "PEP 0000 -- Title". Escape all not filename symbols ("*?:/\\").
18 """
19
20
21
22 import XPyLIB
23 import re
24 import sys
25 import glob
26 import getopt
27 import os
28 import string
29
30
31 import XPyLIB.xlogging as _xlog
32 from functools import wraps
33 _log=_xlog.getLogger('XPyTools.scripts.pep_parser')
34 _log.disabled=1
35 _log_u=_xlog.IndentFormatter.lvlup
36 _log_d=_xlog.IndentFormatter.lvldown
37 _log_r=_xlog.IndentFormatter.lvlreset
38 _log_w=_xlog.IndentFormatter.lvlwrap
43 return wrapper
44
45
46 _log.debug('logging enabled')
47
48
49
50 -def fail(fn='', errstr='Error.'):
51 """
52 Fail reporting.
53 """
54 print 'FAIL: file="%s". %s\n' % (fn, errstr)
55
57 """
58 Output help info.
59 """
60 print r'''
61 Usage:
62 i, indir= - input dir for pep *.html files.
63 o, outdir= - output dir, copy renamed pep files and create index.html file.
64 h,?, help - output this help info.
65 '''
66
68 """
69 Pars peps.
70 """
71
72
73 opt = 'i:o:h?'
74 lopt = ['indir=', 'outdir=', 'help']
75
76 try:
77 opts, args = getopt.getopt(sys.argv[1:], opt, lopt)
78 except getopt.GetoptError, err:
79
80 print str(err)
81 usage()
82 sys.exit(2)
83
84 indir = os.getcwdu()
85 outdir = os.path.join(indir, 'peps')
86
87 for o,v in opts:
88 if o in ('?','-h','--help'):
89 usage()
90 sys.exit(0)
91 elif o in ('-i', '--indir'):
92 indir=v
93 elif o in ('-o', '--outdir'):
94 outdir=v
95 else:
96 assert False, "unhandled option"
97
98 if not os.path.exists(indir):
99 print 'Input dir "%s" not exist.' % (indir,)
100 sys.exit(2)
101
102 if not os.path.exists(outdir):
103 try:
104 os.makedirs(outdir)
105 except:
106 print 'Output dir "%s" not exist, and can\'t be created.' % (outdir,)
107 sys.exit(2)
108
109 re_pep = re.compile(r'<tr["=\w -]*>\s*<th["=\w \-]*>PEP:[ ]*</th>\s*<td["=\w \-]*>(?P<pepid> *\d+ *)</td>\s*</tr>\s*<tr["=\w \-]*><th["=\w \-]*>Title:[ ]*</th>\s*<td["=\w \-]*>(?P<peptitle>[\S ]+)</td>\s*</tr>')
110
111 tt=string.maketrans(':\\/*+?\'"&<>',' ')
112
113 _log.debug('pattern=%s' % (os.path.join(indir,'*.htm'),) )
114 fl = glob.glob(os.path.join(indir,'*.htm'))
115 _log.debug('fl=%s' % (str(fl),) )
116
117 idx = []
118
119
120 for fn in fl:
121 _log.debug( 'try %s\n' % (fn,) )
122 try:
123 f=open(fn,'r')
124 ln=f.read()
125 except Exception, err:
126 fail( fn, 'READ EXCEPTION=%s.' % (str(err),) )
127 if f:
128 f.close()
129 f=None
130 continue
131 f.close()
132 f=None
133
134 s = re_pep.search(ln)
135 if not s:
136 _log.debug('fn search fail\n')
137 continue
138 _log.debug('fn search ok\n')
139 d = s.groupdict()
140 if not ( d.has_key('pepid') and d.has_key('peptitle') ):
141 fail(fn, 'No PEP id and/or title.\n')
142 try:
143 id=int(d['pepid'])
144 except:
145 fail( fn, 'Wrong PEP id="%s".\n' % (d['pepid'],) )
146 continue
147 title=d['peptitle'].strip()
148 tn=title.translate(tt)
149 name='PEP %d -- %s.html' % (id, tn )
150 p=os.path.join(outdir, name)
151 idx.append((id, title, name))
152 _log.debug('try write path: %s\n' % (p,) )
153 try:
154 f=open(p,'w')
155 f.write(ln)
156 _log.debug('\nwritten=%s\n' % (ln[:256],))
157 except Exception, err:
158 fail( fn, 'WRITE EXCEPTION=%s.' % (str(err),) )
159 if f:
160 f.close()
161 f=None
162 continue
163 f.close()
164 f=None
165
166
167
168 _log.debug('generate index.html\n')
169
170 ois=r'''<html>
171 <head>
172 <title> PEPs index</title>
173 </head>
174
175 <body>
176 <table border=1><thead><th>Id</th><th>Title</th></thead>
177 <tbody>'''
178
179 oie=r'''</tbody></table>
180 </body>
181 </html>
182 '''
183
184 idx=sorted(idx, lambda x,y: cmp(x[0],y[0]) )
185 htm=ois
186 for i in idx:
187 htm+='<tr>\n<td><a href="%s">%d</a></td>\n<td>%s</td>\n</tr>\n' % (i[2],i[0],i[1])
188
189 htm+=oie
190
191 try:
192 f=open(os.path.join(outdir, 'index.html'),'w')
193 f.write(htm)
194 except Exception, err:
195 print 'WRITE INDEX EXCEPTION=%s.' % (str(err),)
196 if f:
197 f.close()
198
199
200
201
202
203
204
205
207 """
208 """
209
210 pass
211
212
213
214 if __name__=='__main__':
215 try:
216 do()
217 except Exception, err:
218 print 'ERROR=%s.' % (str(err),)
219 else:
220 print '\nWork complete.\n'
221