/
web-0.131.py
1357 lines (1181 loc) · 43.6 KB
/
web-0.131.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
"""web.py: makes web apps (http://webpy.org)"""
__version__ = "0.131"
__license__ = "Affero General Public License, Version 1"
__author__ = "Aaron Swartz <me@aaronsw.com>"
from __future__ import generators
# long term todo:
# - new form system
# - new templating system
# - unit tests?
# todo:
# - get rid of upvars
# - move documentation into docstrings
# - fix that silly nonce stuff in web.update
# - provide an option to use .write()
# - add ip:port support
# - allow people to do $self.id from inside a reparam
# - add sqlite support
# - make storage a subclass of dictionary
# - convert datetimes, floats in WebSafe
# - locks around memoize
# - fix memoize to use cacheify style techniques
# - merge curval query with the insert
# - figure out how to handle squid, etc. for web.ctx.ip
import os, os.path, sys, time, types, traceback
import cgi, re, urllib, urlparse, Cookie, pprint
from threading import currentThread
from tokenize import tokenprog
try: import datetime
except ImportError: pass
try:
from Cheetah.Compiler import Compiler
from Cheetah.Filters import Filter
_hasTemplating = True
except ImportError:
_hasTemplating = False
try:
from DBUtils.PooledDB import PooledDB
_hasPooling = True
except ImportError:
_hasPooling = False
# hack for compatibility with Python 2.3:
if not hasattr(traceback, 'format_exc'):
from cStringIO import StringIO
def format_exc(limit=None):
s = StringIO()
traceback.print_exc(limit, s)
return s.getvalue()
traceback.format_exc = format_exc
## general utils
def _strips(direction, text, remove):
if direction == 'l':
if text.startswith(remove): return text[len(remove):]
elif direction == 'r':
if text.endswith(remove): return text[:-len(remove)]
else:
raise "WrongDirection", "Needs to be r or l."
return text
def rstrips(a, b): return _strips('r', a, b)
def lstrips(a, b): return _strips('l', a, b)
def strips(a, b): return rstrips(lstrips(a,b),b)
def autoassign():
locals = sys._getframe(1).f_locals
self = locals['self']
for (k, v) in locals.iteritems():
if k == 'self': continue
setattr(self, k, v)
class Storage:
def __init__(self, initial=None):
if initial:
for k in initial.keys(): setattr(self, k, initial[k])
def __getattr__(self, k):
if hasattr(self.__dict__, k) or (
k.startswith('__') and k.endswith('__')): # special keyword
return getattr(self.__dict__, k)
raise AttributeError, repr(k)
def __repr__(self): return '<Storage '+repr(self.__dict__)+'>'
storage = Storage
def storify(f, *requireds, **defaults):
stor = Storage()
for k in requireds + tuple(f.keys()):
v = f[k]
if isinstance(k, list): v = v[-1]
if hasattr(v, 'value'): v = v.value
setattr(stor, k, v)
for (k,v) in defaults.iteritems():
result = v
if hasattr(stor, k): result = stor[k]
if v == () and not isinstance(result, tuple): result = (result,)
setattr(stor, k, result)
return stor
class memoize:
def __init__(self, func): self.func = func; self.cache = {}
def __call__(self, *a, **k):
key = (a, tuple(k.items()))
if key not in self.cache: self.cache[key] = self.func(*a, **k)
return self.cache[key]
re_compile = memoize(re.compile) #@@ threadsafe?
class _re_subm_proxy:
def __init__(self): self.match = None
def __call__(self, match): self.match = match; return ''
def re_subm(pat, repl, string):
"""like re.sub, but returns the replacement and the match object"""
r = re_compile(pat)
proxy = _re_subm_proxy()
r.sub(proxy.__call__, string)
return r.sub(repl, string), proxy.match
def group(seq, size):
"""Breaks 'seq' into a generator of lists with length 'size'."""
if not hasattr(seq, 'next'): seq = iter(seq)
while True: yield [seq.next() for i in xrange(size)]
class iterbetter:
def __init__(self, iterator): self.i, self.c = iterator, 0
def __iter__(self):
while 1: yield self.i.next(); self.c += 1
def __getitem__(self, i):
#todo: slices
if i > self.c: raise KeyError, "already passed "+str(i)
try:
while i < self.c: self.i.next(); self.c += 1
# now self.c == i
self.c += 1; return self.i.next()
except StopIteration: raise KeyError, repr(i)
def dictfind(d, elt):
for (k,v) in d.iteritems():
if elt is v: return k
def dictincr(d, e):
d.setdefault(e, 0)
d[e] += 1
return d[e]
def dictadd(a, b):
result = {}
result.update(a)
result.update(b)
return result
sumdicts = dictadd # deprecated
def listget(l, n, v=None):
if len(l)-1 < n: return v
return l[n]
def upvars(n=2):
return dictadd(
sys._getframe(n).f_globals,
sys._getframe(n).f_locals)
class capturestdout:
def __init__(self, func): self.func = func
def __call__(self, *args, **kw):
from cStringIO import StringIO
# Not threadsafe!
out = StringIO()
oldstdout = sys.stdout
sys.stdout = out
try: self.func(*args, **kw)
finally: sys.stdout = oldstdout
return out.getvalue()
class profile:
def __init__(self, func): self.func = func
def __call__(self, *args, **kw):
import hotshot, hotshot.stats, tempfile, time
temp = tempfile.NamedTemporaryFile()
prof = hotshot.Profile(temp.name)
stime = time.time()
result = prof.runcall(self.func, *args)
stime = time.time() - stime
prof.close()
stats = hotshot.stats.load(temp.name)
stats.strip_dirs()
stats.sort_stats('time', 'calls')
x = '\n\ntook '+ str(stime) + ' seconds\n'
x += capturestdout(stats.print_stats)(40)
x += capturestdout(stats.print_callers)()
return result, x
def tryall(context):
context = context.copy() # vars() would update
results = {}
for (k, v) in context.iteritems():
if not hasattr(v, '__call__'): continue
print k+':',
try:
r = v()
dictincr(results, r)
print r
except:
print 'ERROR'
dictincr(results, 'ERROR')
print ' '+'\n '.join(traceback.format_exc().split('\n'))
print '-'*40
print 'results:'
for (k, v) in results.iteritems():
print ' '*2, str(k)+':', v
class threadeddict:
def __init__(self, d): self.__dict__['_threadeddict__d'] = d
def __getattr__(self, a): return getattr(self.__d[currentThread()], a)
def __getitem__(self, i): return self.__d[currentThread()][i]
def __setattr__(self, a, v): return setattr(self.__d[currentThread()], a, v)
def __setitem__(self, i, v): self.__d[currentThread()][i] = v
def __hash__(self): return hash(self.__d[currentThread()])
## url utils
def base(base=''): #when would you use a default base?
url = context.path.lstrip('/')
for i in xrange(url.count('/')): base += '../'
if not base: base = './'
return base
## formatting
try:
from markdown import markdown # http://webpy.org/markdown.py
except ImportError: pass
r_url = re_compile('(?<!\()(http://(\S+))')
def safemarkdown(text):
if text:
text = text.replace('<', '<')
# TODO: automatically get page title?
text = r_url.sub(r'<\1>', text)
text = markdown(text)
return text
## db api
def _interpolate(format):
"""
takes a format string and returns a list of 2-tuples of the form
(boolean, string) where boolean says whether string should be evaled
or not
from http://lfw.org/python/Itpl.py (public domain, Ka-Ping Yee)
"""
def matchorfail(text, pos):
match = tokenprog.match(text, pos)
if match is None:
raise ItplError(text, pos)
return match, match.end()
namechars = "abcdefghijklmnopqrstuvwxyz" \
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
chunks = []
pos = 0
while 1:
dollar = format.find("$", pos)
if dollar < 0: break
nextchar = format[dollar+1]
if nextchar == "{":
chunks.append((0, format[pos:dollar]))
pos, level = dollar+2, 1
while level:
match, pos = matchorfail(format, pos)
tstart, tend = match.regs[3]
token = format[tstart:tend]
if token == "{": level = level+1
elif token == "}": level = level-1
chunks.append((1, format[dollar+2:pos-1]))
elif nextchar in namechars:
chunks.append((0, format[pos:dollar]))
match, pos = matchorfail(format, dollar+1)
while pos < len(format):
if format[pos] == "." and \
pos+1 < len(format) and format[pos+1] in namechars:
match, pos = matchorfail(format, pos+1)
elif format[pos] in "([":
pos, level = pos+1, 1
while level:
match, pos = matchorfail(format, pos)
tstart, tend = match.regs[3]
token = format[tstart:tend]
if token[0] in "([": level = level+1
elif token[0] in ")]": level = level-1
else: break
chunks.append((1, format[dollar+1:pos]))
else:
chunks.append((0, format[pos:dollar+1]))
pos = dollar + 1 + (nextchar == "$")
if pos < len(format): chunks.append((0, format[pos:]))
return chunks
class UnknownParamstyle(Exception): pass
def aparam():
p = ctx.db_module.paramstyle
if p == 'qmark': return '?'
elif p == 'numeric': return ':1'
elif p in ['format', 'pyformat']: return '%s'
raise UnknownParamstyle, p
def reparam(s, d):
vals = []
result = []
for live, chunk in _interpolate(s):
if live:
result.append(aparam())
vals.append(eval(chunk, d))
else: result.append(chunk)
return ''.join(result), vals
class UnknownDB(Exception): pass
def connect(dbn, **kw):
if dbn == "postgres":
try: import psycopg2 as db
except ImportError:
try: import psycopg as db
except ImportError: import pgdb as db
kw['password'] = kw['pw']
del kw['pw']
kw['database'] = kw['db']
del kw['db']
elif dbn == "mysql":
import MySQLdb as db
kw['passwd'] = kw['pw']
del kw['pw']
db.paramstyle = 'pyformat' # it's both, like psycopg
else: raise UnknownDB, dbn
ctx.db_name = dbn
ctx.db_module = db
ctx.db_transaction = False
if _hasPooling:
if 'db' not in globals(): globals()['db'] = PooledDB(dbapi=db, **kw)
ctx.db = globals()['db'].connection()
else:
ctx.db = db.connect(**kw)
ctx.dbq_count = 0
if globals().get('db_printing'):
def db_execute(cur, q, d=None):
ctx.dbq_count += 1
try: outq = q % d
except: outq = q
print>>debug, str(ctx.dbq_count)+':', outq
a = time.time()
out = cur.execute(q, d)
b = time.time()
print>>debug, '(%s)' % round(b-a, 2)
return out
ctx.db_execute = db_execute
else:
ctx.db_execute = lambda cur, q, d=None: cur.execute(q, d)
return ctx.db
def transact():
"""Start a transaction."""
# commit everything up to now, so we don't rollback it later
ctx.db.commit()
ctx.db_transaction = True
def commit():
ctx.db.commit()
ctx.db_transaction = False
def rollback():
ctx.db.rollback()
ctx.db_transaction = False
def query(q, vars=None, processed=False):
if vars is None: vars = {}
d = ctx.db.cursor()
if not processed: q, vars = reparam(q, vars)
ctx.db_execute(d, q, vars)
if d.description:
names = [x[0] for x in d.description]
def iterwrapper():
x = d.fetchone()
while x:
yield Storage(dict(zip(names, x)))
x = d.fetchone()
out = iterbetter(iterwrapper())
out.__len__ = lambda: d.rowcount
out.list = lambda: [Storage(dict(zip(names, x))) for x in d.fetchall()]
else:
out = None
out = d.rowcount
if not ctx.db_transaction: ctx.db.commit()
return out
def sqllist(l):
if isinstance(l, str): return l
else: return ', '.join(l)
def select(tables, vars=None, what='*', where=None, order=None, group=None,
limit=None, offset=None):
if vars is None: vars = {}
values = []
qout = "SELECT "+what+" FROM "+sqllist(tables)
for (sql, val) in (
('WHERE', where),
('ORDER BY', order),
('GROUP BY', group),
('LIMIT', limit),
('OFFSET', offset)):
if val:
nquery, nvalue = reparam(val, vars)
qout += " "+sql+" " + nquery
values.extend(nvalue)
return query(qout, values, processed=True)
def insert(tablename, seqname=None, **values):
d = ctx.db.cursor()
if values:
ctx.db_execute(d, "INSERT INTO %s (%s) VALUES (%s)" % (
tablename,
", ".join(values.keys()),
', '.join([aparam() for x in values]) #@@ use nonce
), values.values())
else:
ctx.db_execute(d, "INSERT INTO %s DEFAULT VALUES" % tablename)
if seqname == False:
out = None
elif ctx.db_name == "postgres":
if seqname is None: seqname = tablename + "_id_seq"
ctx.db_execute(d, "SELECT currval('%s')" % seqname)
out = d.fetchone()[0]
elif ctx.db_name == "mysql":
ctx.db_execute(d, "SELECT last_insert_id()")
out = d.fetchone()[0]
elif ctx.db_name == "sqlite":
# not really the same...
ctx.db_execute(d, "SELECT last_insert_rowid()")
out = d.fetchone()[0]
else:
out = None
if not ctx.db_transaction: ctx.db.commit()
return out
def update(tables, where, vars=None, **values):
if vars is None: vars = {}
if isinstance(where, int):
vars = [where]
where = "id = "+aparam()
else:
where, vars = reparam(where, vars)
d = ctx.db.cursor()
ctx.db_execute(d, "UPDATE %s SET %s WHERE %s" % (
sqllist(tables),
', '.join([k+'='+aparam() for k in values.keys()]),
where),
values.values()+vars)
if not ctx.db_transaction: ctx.db.commit()
return d.rowcount
def delete(table, where, using=None, vars=None):
if vars is None: vars = {}
d = ctx.db.cursor()
where, vars = reparam(where, vars)
q = 'DELETE FROM %s WHERE %s' % (table, where)
if using: q += ' USING '+sqllist(using)
ctx.db_execute(d, q, vars)
if not ctx.db_transaction: ctx.db.commit()
return d.rowcount
## request handlers
def handle(mapping, fvars=None):
for url, ofno in group(mapping, 2):
if isinstance(ofno, tuple): ofn, fna = ofno[0], list(ofno[1:])
else: ofn, fna = ofno, []
fn, result = re_subm('^'+url+'$', ofn, context.path)
if result: # it's a match
if fn.split(' ', 1)[0] == "redirect":
url = fn.split(' ', 1)[1]
if context.method == "GET":
x = context.environ.get('QUERY_STRING', '')
if x: url += '?'+x
return redirect(url)
elif '.' in fn:
x = fn.split('.')
mod, cls = '.'.join(x[:-1]), x[-1]
mod = __import__(mod, globals(), locals(), [""])
cls = getattr(mod, cls)
else:
cls = fn
mod = fvars or upvars()
if isinstance(mod, types.ModuleType): mod = vars(mod)
try: cls = mod[cls]
except KeyError: return notfound()
meth = context.method
if meth == "HEAD":
if not hasattr(cls, meth): meth = "GET"
if not hasattr(cls, meth): return nomethod(cls)
tocall = getattr(cls(), meth)
args = list(result.groups())
for d in re.findall(r'\\(\d+)', ofn):
args.pop(int(d)-1)
return tocall(*([urllib.unquote(x) for x in args]+fna))
return notfound()
def autodelegate(prefix=''):
def internal(self, arg):
func = prefix+arg
if hasattr(self, func): return getattr(self, func)()
else: return notfound()
return internal
## http defaults
def redirect(url, status='301 Moved Permanently'):
newloc = urlparse.urljoin(context.home + context.path, url)
context.status = status
header('Content-Type', 'text/html')
header('Location', newloc)
# seems to add a three-second delay for some reason:
# output('<a href="'+ newloc + '">moved permanently</a>')
def found(url): return redirect(url, '302 Found')
def seeother(url): return redirect(url, '303 See Other')
def tempredirect(url): return redirect(url, '307 Temporary Redirect')
def badrequest():
context.status = '400 Bad Request'
header('Content-Type', 'text/html')
return output('bad request')
def notfound():
context.status = '404 Not Found'
header('Content-Type', 'text/html')
return output('not found')
def nomethod(cls):
context.status = '405 Method Not Allowed'
header('Content-Type', 'text/html')
header("Allow", ', '.join([x for x in ['GET', 'HEAD', 'POST', 'PUT', 'DELETE'] if hasattr(cls, x)]))
return output('method not allowed')
def gone():
context.status = '410 Gone'
header('Content-Type', 'text/html')
return output("gone")
def expires(delta):
try: datetime
except NameError: raise Exception, "this function requires at least python2.3"
if isinstance(delta, (int, long)):
delta = datetime.timedelta(seconds=delta)
o = datetime.datetime.utcnow() + delta
header('Expires', o.strftime("%a, %d %b %Y %T GMT"))
def lastmodified(d):
header('Last-Modified', d.strftime("%a, %d %b %Y %T GMT"))
# adapted from Django <djangoproject.com>
# Copyright (c) 2005, the Lawrence Journal-World
# Used under the modified BSD license:
# http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5
DJANGO_500_PAGE = """#import inspect
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="robots" content="NONE,NOARCHIVE" />
<title>$exception_type at $context.path</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
table { border:1px solid #ccc; border-collapse: collapse; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { text-align:right; color:#666; padding-right:.5em; }
table.vars { margin:5px 0 2px 40px; }
table.vars td, table.req td { font-family:monospace; }
table td.code { width:100%;}
table td.code div { overflow:hidden; }
table.source th { color:#666; }
table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
ul.traceback { list-style-type:none; }
ul.traceback li.frame { margin-bottom:1em; }
div.context { margin: 10px 0; }
div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
div.context ol li { font-family:monospace; white-space:pre; color:#666; cursor:pointer; }
div.context ol.context-line li { color:black; background-color:#ccc; }
div.context ol.context-line li span { float: right; }
div.commands { margin-left: 40px; }
div.commands a { color:black; text-decoration:none; }
#summary { background: #ffc; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#template, #template-not-exist { background:#f6f6f6; }
#template-not-exist ul { margin: 0 0 0 20px; }
#traceback { background:#eee; }
#requestinfo { background:#f6f6f6; padding-left:120px; }
#summary table { border:none; background:transparent; }
#requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
#requestinfo h3 { margin-bottom:-1em; }
.error { background: #ffc; }
.specific { color:#cc3300; font-weight:bold; }
</style>
<script type="text/javascript">
//<!--
function getElementsByClassName(oElm, strTagName, strClassName){
// Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
var arrElements = (strTagName == "*" && document.all)? document.all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-");
var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
function hideAll(elems) {
for (var e = 0; e < elems.length; e++) {
elems[e].style.display = 'none';
}
}
window.onload = function() {
hideAll(getElementsByClassName(document, 'table', 'vars'));
hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
hideAll(getElementsByClassName(document, 'ol', 'post-context'));
}
function toggle() {
for (var i = 0; i < arguments.length; i++) {
var e = document.getElementById(arguments[i]);
if (e) {
e.style.display = e.style.display == 'none' ? 'block' : 'none';
}
}
return false;
}
function varToggle(link, id) {
toggle('v' + id);
var s = link.getElementsByTagName('span')[0];
var uarr = String.fromCharCode(0x25b6);
var darr = String.fromCharCode(0x25bc);
s.innerHTML = s.innerHTML == uarr ? darr : uarr;
return false;
}
//-->
</script>
</head>
<body>
<div id="summary">
<h1>$exception_type at $context.path</h1>
<h2>$exception_value</h2>
<table><tr>
<th>Python</th>
<td>$lastframe.filename in $lastframe.function, line $lastframe.lineno</td>
</tr><tr>
<th>Web</th>
<td>$context.method $context.home$context.path</td>
</tr></table>
</div>
<div id="traceback">
<h2>Traceback <span>(innermost first)</span></h2>
<ul class="traceback">
#for frame in $frames
<li class="frame">
<code>$frame.filename</code> in <code>$frame.function</code>
#if $frame.context_line
<div class="context" id="c$frame.id">
#if $frame.pre_context
<ol start="$frame.pre_context_lineno" class="pre-context" id="pre$frame.id">#for line in $frame.pre_context#<li onclick="toggle('pre$frame.id', 'post$frame.id')">$line</li>#end for#</ol>
#end if
<ol start="$frame.lineno" class="context-line"><li onclick="toggle('pre$frame.id', 'post$frame.id')">$frame.context_line <span>...</span></li></ol>
#if $frame.post_context
<ol start='$(frame.lineno+1)' class="post-context" id="post$frame.id">#for line in $frame.post_context#<li onclick="toggle('pre$frame.id', 'post$frame.id')">$line</li>#end for#</ol>
#end if
</div>
#end if
#if $frame.vars
<div class="commands">
<a href='#' onclick="return varToggle(this, '$frame.id')"><span>▶</span> Local vars</a>## $inspect.formatargvalues(*inspect.getargvalues(frame['tb'].tb_frame))
</div>
<table class="vars" id="v$frame.id">
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#set frameitems = $frame.vars
#silent frameitems.sort(lambda x,y: cmp(x[0], y[0]))
#for (key, val) in frameitems
<tr>
<td>$key</td>
<td class="code"><div>$prettify(val)</div></td>
</tr>
#end for
</tbody>
</table>
#end if
</li>
#end for
</ul>
</div>
<div id="requestinfo">
#if $context_.output or $context_.headers
<h2>Response so far</h2>
<h3>HEADERS</h3>
#if $context.headers
<p class="req"><code>
#for (k, v) in $context_.headers
$k: $v<br />
#end for
</code></p>
#else
<p>No headers.</p>
#end if
<h3>BODY</h3>
<p class="req" style="padding-bottom: 2em"><code>
$context_.output
</code></p>
#end if
<h2>Request information</h2>
<h3>INPUT</h3>
#if $input_
<table class="req">
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#set myitems = $input_.items()
#silent myitems.sort(lambda x,y: cmp(x[0], y[0]))
#for (key, val) in myitems
<tr>
<td>$key</td>
<td class="code"><div>$val</div></td>
</tr>
#end for
</tbody>
</table>
#else
<p>No input data.</p>
#end if
<h3 id="cookie-info">COOKIES</h3>
#if $cookies_
<table class="req">
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#for (key, val) in $cookies_.items()
<tr>
<td>$key</td>
<td class="code"><div>$val</div></td>
</tr>
#end for
</tbody>
</table>
#else
<p>No cookie data</p>
#end if
<h3 id="meta-info">META</h3>
<table class="req">
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#set myitems = $context_.items()
#silent myitems.sort(lambda x,y: cmp(x[0], y[0]))
#for (key, val) in $myitems
#if not $key.startswith('_') and $key not in ['env', 'output', 'headers', 'environ', 'status', 'db_execute']
<tr>
<td>$key</td>
<td class="code"><div>$prettify($val)</div></td>
</tr>
#end if
#end for
</tbody>
</table>
<h3 id="meta-info">ENVIRONMENT</h3>
<table class="req">
<thead>
<tr>
<th>Variable</th>
<th>Value</th>
</tr>
</thead>
<tbody>
#set myitems = $context_.environ.items()
#silent myitems.sort(lambda x,y: cmp(x[0], y[0]))
#for (key, val) in $myitems
<tr>
<td>$key</td>
<td class="code"><div>$prettify($val)</div></td>
</tr>
#end for
</tbody>
</table>
</div>
<div id="explanation">
<p>
You're seeing this error because you have <code>web.internalerror</code>
set to <code>web.debugerror</code>. Change that if you want a different one.
</p>
</div>
</body>
</html>"""
def djangoerror():
def _get_lines_from_file(filename, lineno, context_lines):
"""
Returns context_lines before and after lineno from file.
Returns (pre_context_lineno, pre_context, context_line, post_context).
"""
try:
source = open(filename).readlines()
lower_bound = max(0, lineno - context_lines)
upper_bound = lineno + context_lines
pre_context = [line.strip('\n') for line in source[lower_bound:lineno]]
context_line = source[lineno].strip('\n')
post_context = [line.strip('\n') for line in source[lineno+1:upper_bound]]
return lower_bound, pre_context, context_line, post_context
except (OSError, IOError):
return None, [], None, []
exception_type, exception_value, tb = sys.exc_info()
frames = []
while tb is not None:
filename = tb.tb_frame.f_code.co_filename
function = tb.tb_frame.f_code.co_name
lineno = tb.tb_lineno - 1
pre_context_lineno, pre_context, context_line, post_context = _get_lines_from_file(filename, lineno, 7)
frames.append({
'tb': tb,
'filename': filename,
'function': function,
'lineno': lineno,
'vars': tb.tb_frame.f_locals.items(),
'id': id(tb),
'pre_context': pre_context,
'context_line': context_line,
'post_context': post_context,
'pre_context_lineno': pre_context_lineno,
})
tb = tb.tb_next
lastframe = frames[-1]
frames.reverse()
urljoin = urlparse.urljoin
input_ = input()
cookies_ = cookies()
context_ = context
def prettify(x):
try: out = pprint.pformat(x)
except Exception, e: out = '[could not display: <'+e.__class__.__name__+': '+str(e)+'>]'
return out
return render(DJANGO_500_PAGE, asTemplate=True, isString=True)
def internalerror():
context.status = "500 Internal Server Error"
context.headers = [('Content-Type', 'text/html')]
context.output = "internal server error"
def debugerror():
# need to do django first, so it can get the old stuff
if _hasTemplating:
out = str(djangoerror())
else:
# Cheetah isn't installed
out = """<p>You've set web.py to use the fancier debugerror error messages,
but these messages require you install the Cheetah template
system. For more information, see
<a href="http://webpy.org/">the web.py website</a>.</p>
<p>In the meantime, here's a plain old error message:</p>
<pre>%s</pre>
<p>(If it says something about 'Compiler', then it's probably
because you're trying to use templates and you haven't
installed Cheetah. See above.)</p>
""" % htmlquote(traceback.format_exc())
context.status = "500 Internal Server Error"
context.headers = [('Content-Type', 'text/html')]
context.output = out
## rendering
r_include = re_compile(r'(?!\\)#include \"(.*?)\"($|#)', re.M)
def __compiletemplate(template, base=None, isString=False):
if isString: text = template
else: text = open('templates/'+template).read()
# implement #include at compile-time
def do_include(match):
text = open('templates/'+match.groups()[0]).read()
return text
while r_include.findall(text): text = r_include.sub(do_include, text)
execspace = _compiletemplate.bases.copy()
c = Compiler(source=text, mainClassName='GenTemplate')
c.addImportedVarNames(execspace.keys())
exec str(c) in execspace
if base: _compiletemplate.bases[base] = execspace['GenTemplate']
return execspace['GenTemplate']
_compiletemplate = memoize(__compiletemplate)
_compiletemplate.bases = {}
def htmlquote(s):
s = s.replace("&", "&") # Must be done first!
s = s.replace("<", "<")
s = s.replace(">", ">")
s = s.replace("'", "'")
s = s.replace('"', """)
return s
urlquote = urllib.quote
if _hasTemplating:
class WebSafe(Filter):
def filter(selv, val, **kw):
if val is None: return ''
return htmlquote(str(val))
def render(template, terms=None, asTemplate=False, base=None, isString=False):
# terms=['var1', 'var2'] means grab those variables
if isinstance(terms, list):
new = {}; old = upvars()
for k in terms: new[k] = old[k]
terms = new