-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.py
executable file
·253 lines (211 loc) · 8.01 KB
/
index.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
#This program will generate a pretty HTML file containing the notes and stuff for each chapter and exercise
import os, re, datetime, time, math, string, operator
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from mod_python import apache, psp
def duration(timestamp):
years = math.floor(timestamp / (60*60*24*365))
timestamp%=60*60*24*365
weeks=math.floor(timestamp / (60*60*24*7))
timestamp%=60*60*24*7
days=math.floor(timestamp / (60*60*24))
timestamp%=60*60*24
hrs=math.floor(timestamp / (60*60))
timestamp%=60*60
mins=math.floor(timestamp / 60)
secs=timestamp % 60;
out = ""
if (years >= 1):
out += str(int(years)) + " year" + plural(years) + " "
if (weeks >= 1):
out += str(int(weeks)) + " week" + plural(weeks) + " "
if (days >= 1):
out += str(int(days)) + " day" + plural(days) + " "
if (hrs >= 1):
out += str(int(hrs)) + " hour" + plural(hrs) + " "
if (mins >= 1):
out += str(int(mins)) + " minute" + plural(mins) + " "
if (secs >= 1):
out += str(int(secs)) + " second" + plural(secs)
return out
def plural(num):
if num != 1:
return "s"
else:
return ""
def getfiles(path):
exercises = []
os.chdir(path)
for entry in os.listdir(path):
exercises.append(entry)
return exercises
def getchapters():
path = "/var/www/html/python/files/Chapter Exercises/"
chaps = []
os.chdir(path)
for entry in os.listdir(path):
if os.path.isdir(entry):
chaps.append(entry)
chaps.sort()
return chaps
def getBookPrograms():
path = "/var/www/html/python/files/Book Programs/"
chaps = []
os.chdir(path)
for entry in os.listdir(path):
if os.path.isdir(entry):
chaps.append(entry)
chaps.sort()
return chaps
def generateSideBarContent():
sidebar = []
chaps = []
for chap in getchapters():
chaps.append({"url": "chapter?chap=" + chap, "text": chap})
links = []
links.append({"url": "/python/static/references/2.5/index.html", "text": "Quick Reference (2.5) (HTML)"})
links.append({"url": "/python/static/references/2.4/qr24.pdf", "text": "Quick Reference (2.4) (PDF)"})
links.append({"url": "http://effbot.org/tkinterbook/", "text": "TKinter Reference (HTML)"})
sidebar.append({"title":"Chapters", "itemlist":chaps})
sidebar.append({"title":"Links", "itemlist":links})
return sidebar
def displayPage(req, contentblocks, pagetitle = "Python Project 2007", sidebarblocks = generateSideBarContent(), show_code_css = False):
curmicroseconds = datetime.datetime.now().microsecond
req.content_type = 'text/html'
t = psp.PSP(req, filename="templates/page.tmpl")
t.run(vars = {"pagetitle": pagetitle, "sidebarblocks": sidebarblocks, "contentblocks": contentblocks, "show_code_css": show_code_css, "elapsedseconds": (math.pow(10, -6) * (datetime.datetime.now().microsecond-curmicroseconds))})
return
def contentBlock(title = "", content = ""):
return {"title": title, "content": content}
def chapter(req, chap = ''):
if chap in getchapters():
chapcontent = showChapter(req, chap)
displayPage(req, [contentBlock(chap, chapcontent)])
else:
displayPage(req, [contentBlock("Error!", "There are no chapters available")])
return
def index(req):
displayPage(req, [contentBlock("Welcome!", "Welcome to the python project 2007. You can view the book examples and chapter exercise archives by accessing the tabs near the top of the page. <br /> To get the clipboard functions to work, set signed.applets.codebase_principal_support = true in firefox's about:config")])
return
def showChapter(req, chapter):
relativepath = "/python/files/Chapter Exercises/" + str(chapter) + "/"
fullpath = "/var/www/html"
filelocation = fullpath + relativepath
exercisefiles = getfiles(filelocation);
exercises = []
exercisefiles.sort()
for file in exercisefiles:
if file[:8] != "exercise":
continue;
curfile = open("./"+file)
contents = curfile.read()
vals = {}
for match in re.finditer(r"(?im)^#\s*(\w+):(.*)$", contents):
vals[match.group(1).strip().lower()] = match.group(2).strip()
if vals.has_key("exercise"):
exercise = "<a href='view?chap="+chapter.lower().strip("chapter ")+"&exercise="+file.strip("exercise.py")+"'>"+vals["exercise"]+"</a>"
else:
exercise = "Unknown"
if vals.has_key("start") & vals.has_key("end"):
timestring = "%I:%M:%S %p %m/%d/%Y"
dur = duration(time.mktime(time.strptime(vals["end"], timestring))- time.mktime(time.strptime(vals["start"], timestring)));
else:
dur = "Unknown"
if vals.has_key("rating"):
if vals['rating'] == "1":
ratingcolor = "#FF687C"
elif vals['rating'] == "2":
ratingcolor = "#FF8C33"
elif vals['rating'] == "3":
ratingcolor = "#FFF04D"
elif vals['rating'] == "4":
ratingcolor = "#B2FF3E"
rating = vals['rating']
else:
ratingcolor = "#FFFFFF"
rating = "Unknown"
if vals.has_key("note"):
note = vals['note']
else:
note = "Unknown"
exercises.append({"exercise":exercise, "duration":dur, "rating":rating, "ratingcolor":ratingcolor, "note":note})
t2 = psp.PSP(req, filename="templates/chaptergrid.tmpl", vars={"exercises":exercises})
return t2
def view(req, chap='', exercise=''):
title = "Viewing Chapter "+ chap + ", Exercise " + str(int(exercise))
content = ""
if chap.count(".") != 0:
title = "ERROR!"
content = "You cannot use periods in the arguments!"
return
if exercise.count(".") != 0:
title = "ERROR!"
content = "You cannot use periods in the arguments!"
return
fullpath = "/var/www/html"
relativepath = "/python/files/Chapter Exercises/Chapter " + chap + "/exercise" + exercise + ".py"
path = fullpath + relativepath
if os.path.isfile(path):
curfile = open(path)
code = curfile.read()
formatter = HtmlFormatter()
content = highlight(code, PythonLexer(), formatter)
content = "<div style='overflow:auto; font-size:10pt;'>" + content + "</div><a href='"+relativepath+"'>View exercise"+exercise+".py</a>"
else:
title = "ERROR!"
content = "Error! Invalid File"
displayPage(req, [contentBlock(title, content)], title, generateSideBarContent(), True)
return
def SyntaxStyleSheet(req):
#Displays the css data for Pygments' syntax highlighting
req.content_type = "text/css"
req.write(HtmlFormatter().get_style_defs('.highlight'))
return
def CurTimestamp(req):
req.content_type = "text/html"
req.write(time.strftime("%I:%M:%S %p %m/%d/%Y", time.localtime()))
return
def newTemplate(req, chap="0", exer="0"):
req.content_type = "text/html"
if (chap.find(".") != -1 or exer.find(".") != -1):
req.write("No hacking")
return
ret = "\r\n"
chapter = str(int(chap))
exercise = str(int(exer))
templatestring = "# Chapter:\t\t"+chapter+ret
templatestring = templatestring + "# Exercise:\t\t"+exercise+ret
templatestring = templatestring + "# Start:\t\t"+time.strftime("%I:%M:%S %p %m/%d/%Y", time.localtime())+ret
templatestring = templatestring + "# End:\t\t\t___________"+ret
templatestring = templatestring + "# Rating:\t\t___________"+ret
templatestring = templatestring + "# Note:\t\t\t___________"+ret
templatestring = templatestring + ret
filedir = "/var/www/html/python/files/Chapter Exercises/Chapter "+chapter+"/"
filename = "exercise"+exercise.zfill(2)+".py"
fullpath = filedir+filename
if (not os.path.isdir(filedir)):
req.write("Invalid File Path")
return
if (os.path.exists(fullpath)):
req.write("File already exists for Chapter "+chapter+", Exercise "+exercise)
else:
f = open(fullpath, "w")
f.write(templatestring)
f.close()
req.write("Template for Chapter "+chapter+", Exercise "+exercise+" Created")
return
def BookExercises(req):
bookprogramspath = "/var/www/html/python/files/Book Programs/"
webpath = "/python/files/Book Programs/"
contentblocks = []
for dir in getBookPrograms():
files = getfiles(bookprogramspath + dir)
prettychaptername = "Chapter " + dir.strip("chapter")
chapternum = int(dir.strip("chapter"))
outstr = ""
for file in files:
outstr = outstr + ("<a href=\""+webpath + dir + "/" + file + "\">"+file+"</a><br />")
contentblocks.insert(chapternum, (contentBlock(prettychaptername, outstr)))
displayPage(req, contentblocks)
return