-
Notifications
You must be signed in to change notification settings - Fork 0
/
praxis_info.py
381 lines (323 loc) · 12.3 KB
/
praxis_info.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import urllib
import re
import os, os.path
from BeautifulSoup import BeautifulSoup
class PraxisInfo:
__slots__ = ["number",
"file",
"pubmon",
"pubday",
"pubyear",
"title",
"ptitle",
"blurb",
"pblurb",
"exer",
"soln",
"extra",
"codepad",
"theme"]
def __str__(self):
ret = ""
for slot in self.__slots__:
if hasattr(self, slot):
ret += slot + "\t" + str(getattr(self, slot)) + "\n"
return ret
def __lt__(self, other):
comps = ["number", "pubyear", "pubmon", "pubday"]
for c in comps:
if getattr(self, c) < getattr(other, c):
return True
if getattr(self, c) > getattr(other, c):
return False
return NotImplemented
def __le__(self, other):
if self < other:
return True
if self == other:
return True
return False
def __eq__(self, other):
for slot in self.__slots__:
if slot == "theme":
continue
if hasattr(self, slot) and hasattr(other, slot):
if getattr(self, slot) != getattr(other, slot):
return False
return True
def __ne__(self, other):
return not self == other
def __gt__(self, other):
return not self <= other
def __ge__(self, other):
return not self < other
def add_info(self, other):
for slot in self.__slots__:
if slot == "theme":
themes = []
if hasattr(self, "theme"):
for th in self.theme.split("|"):
themes.append(th)
if hasattr(other, "theme"):
for th in other.theme.split("|"):
themes.append(th)
self.theme = "|".join(themes)
elif hasattr(other, slot):
if hasattr(self, slot):
if getattr(self, slot) != getattr(other, slot):
print self
print ""
print other
print ""
print slot
assert False
else:
setattr(self, slot, getattr(other, slot))
head_sep = "----+----1----+----2----+----3----+----4----+----5----+----6"
int_attrs = ["number", "pubmon", "pubday", "pubyear"]
mon_dict = {"Jan": 1, "Feb": 2, "Mar": 3, "Apr": 4, "May": 5, "Jun": 6,
"Jul": 7, "Aug": 8, "Sep": 9, "Oct": 10, "Nov": 11, "Dec": 12}
def tidy_string(string):
reps = [("’", "'"),
("&", "&"),
("è", "e"),
("'", "'"),
("−", "-"), ("&;minus", "-"),
("π", "pi"),
("<em>", ""),
("</em>", ""),
("×", "."),
("‘", "'"),
("“", "\""),
("”", "\""),
(""", "\""),
("≤", "<"),
(">", ">"),
("…", "...")]
for r in reps:
string = string.replace(*r)
return string
def parse_chrono(infos = None):
"""Parse the chronological listing of exercises and add/update the infos"""
page = urllib.urlopen("http://programmingpraxis.com/contents/chron/")
contents = page.read()
page.close()
pattern = r"""
<tr>\n
<td>(\d+?)</td>\n
<td>(\d\d?)\s([A-Z][a-z][a-z])\s(\d\d\d\d)</td>\n
<td><a\shref="(.*?)">(.*?)</a>:\s(.*?)</td>\n
<td><a.*?</a>\s<a\shref="(.*?)">solution</a>\s<a\shref="http://programmingpraxis.codepad.org/(.*?)">codepad</a></td>\n
</tr>
"""
exercises = re.findall(pattern, contents, re.DOTALL | re.VERBOSE)
if infos == None:
infos = []
for ex in exercises:
info = PraxisInfo()
info.number = int(ex[0])
info.pubday = int(ex[1])
info.pubmon = mon_dict[ex[2]]
info.pubyear = int(ex[3])
info.file = ex[4].split("/")[4]
if len(ex[4].split("/")) == 7:
info.exer = ex[4].split("/")[5]
else:
info.exer = "1"
info.title = tidy_string(ex[5])
info.blurb = tidy_string(ex[6])
info.soln = ex[7].split("/")[-2]
info.codepad = ex[8]
found = -1
for i, inf in enumerate(infos):
if info == inf:
found = i
break
if found == -1:
infos.append(info)
else:
infos[found].add_info(info)
return infos
def parse_theme(infos = None):
"""Parse the thematic listing of exercises and add/update the infos"""
page = urllib.urlopen("http://programmingpraxis.com/contents/themes/")
contents = page.read()
page.close()
soup = BeautifulSoup(contents)
table = str(soup.find("table"))
table = table.split("<tr>")
curr_theme = None
for t in table:
theme_pattern = r'\s*<td\scolspan="6"><a\sname="(?P<theme>.*?)">.*'
theme_match = re.match(theme_pattern, t, re.DOTALL | re.X)
if theme_match:
curr_theme = theme_match.group("theme")
continue
info_pattern = """\s*
<td> </td>\s
<td>(?P<number>\d+)</td>\s
<td>(?P<pubday>\d\d?)\s
(?P<pubmon>[A-Z][a-z][a-z])\s
(?P<pubyear>\d\d\d\d)</td>\s
<td><a\shref="(?P<addr>.*?)">
(?P<title>.*?)</a>:\s(?P<blurb>.*?)</td>\s
<td><a.*?</a>\s<a\shref="(?P<soln_addr>.*?)">solution</a>\s<a\shref="http://programmingpraxis.codepad.org/(?P<codepad>.*?)">codepad</a></td>\n
(.*?)</tr>
"""
info_match = re.match(info_pattern, t, re.DOTALL | re.X)
fields = ["number", "pubday", "pubmon", "pubyear",
"title", "blurb", "codepad"]
if info_match:
info = PraxisInfo()
for field in fields:
if field == "pubmon":
info.pubmon = mon_dict[info_match.group("pubmon")]
elif field in int_attrs:
setattr(info, field, int(info_match.group(field)))
else:
setattr(info, field, tidy_string(info_match.group(field)))
assert curr_theme
info.theme = curr_theme
addr = info_match.group("addr")
info.file = addr.split("/")[4]
if len(addr.split("/")) == 7:
info.exer = addr.split("/")[5]
else:
info.exer = "1"
soln = info_match.group("soln_addr")
info.soln = soln.split("/")[-2]
#Fix up a broken one
if info.file == "rpn-calculator":
info.number = 1
found = -1
for i,inf in enumerate(infos):
if info == inf:
found = i
break
if found == -1:
infos.append(info)
else:
infos[i].add_info(info)
return infos
def parse_praxis_info():
"""Read a set of PraxisInfos from the praxis.info file"""
infos = []
with open("praxis.info") as fin:
line = ""
while line != head_sep:
line = fin.readline().strip()
line = fin.readline()
while line:
info = PraxisInfo()
line = fin.readline()
while line and line != "\n":
line = line.strip().split("\t")
if line[0] in int_attrs:
setattr(info, line[0], int(line[1]))
else:
setattr(info, line[0], line[1])
line = fin.readline()
infos.append(info)
return infos
def write_praxis_info(infos):
"""Write out the praxis.info file from a set of infos"""
with open("praxis.info", "w") as fout:
fout.write("praxis.info\n")
fout.write("\n")
fout.write("number exercise number\n")
fout.write("file base name of files\n")
fout.write("pubmon month of publication\n")
fout.write("pubday day of publication\n")
fout.write("pubyear year of publication\n")
fout.write("title formatted title\n")
fout.write("ptitle plain title\n")
fout.write("blurb formatted blurb\n")
fout.write("pblurb plain blurb\n")
fout.write("exer exercise sub-page number\n")
fout.write("soln solution sub-page number\n")
fout.write("extra extra info sub-page number\n")
fout.write("codepad eight-character codepad index\n")
fout.write("theme category in which exercise appears\n")
fout.write("\n")
fout.write("name/value pairs on a line separated by tabs,\n")
fout.write("with records separated by blank lines\n")
fout.write("\n")
fout.write("the \"number\" field must appear first, others\n")
fout.write("may be in any order, and are optional\n")
fout.write("\n")
fout.write("----+----1----+----2----+----3----+----4----+----5----+----6\n")
fout.write("\n")
for info in infos:
fout.write("%s" % str(info))
fout.write("\n")
def strip_exercise(contents):
soup = BeautifulSoup(contents)
title = soup.find('title')
entry_title = soup.find('div', {"class": "entrytitle"})
entry = soup.find('div', {"class": "entrybody"})
entry = str(entry).split("<div class=\"wpadvert\"")[0]
entry = entry.split("<div id=\"wpcom_below_post\"")[0] + "</div>"
page = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'
page += '<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">\n'
page += '<head>\n'
page += str(title) + '\n'
page += '</head>\n'
page += '<body>\n'
page += str(entry_title) + '\n'
page += entry + '\n'
page += '</body>\n'
page += '</html>\n'
return page
def download_exercises(infos, force=False):
for i, info in enumerate(infos):
print "Downloading", i+1, "/", len(infos), ":", info.file
outpath = os.path.join("exercises", info.file + ".html")
if os.path.exists(outpath) and not force:
continue
url = "http://programmingpraxis.com/"
url += str(info.pubyear) + "/"
url += str(info.pubmon) + "/"
url += str(info.pubday) + "/"
url += info.file + "/"
page = urllib.urlopen(url)
contents = page.read()
page.close()
contents = strip_exercise(contents)
with open(outpath, "w") as fout:
fout.write(contents)
for i in range(2, int(info.soln)):
extra_url = url + str(i) + "/"
page = urllib.urlopen(extra_url)
contents = page.read()
page.close()
contents = strip_exercise(contents)
outpath = os.path.join("exercises", info.file + "-" + str(i) + ".html")
with open(outpath, "w") as fout:
fout.write(contents)
def download_solutions(infos, force=False):
for i, info in enumerate(infos):
print "Downloading", i+1, "/", len(infos), ":", info.file
outpath = os.path.join("solutions", info.file + ".html")
if os.path.exists(outpath) and not force:
continue
url = "http://programmingpraxis.com/"
url += str(info.pubyear) + "/"
url += str(info.pubmon) + "/"
url += str(info.pubday) + "/"
url += info.file + "/"
url += info.soln + "/"
page = urllib.urlopen(url)
contents = page.read()
page.close()
contents = strip_exercise(contents)
with open(outpath, "w") as fout:
fout.write(contents)
def update_praxis_info():
infos = parse_praxis_info()
infos = parse_chrono(infos)
infos = parse_theme(infos)
write_praxis_info(infos)
def update_exercises():
infos = parse_praxis_info()
download_exercises(infos, True)
download_solutions(infos)