Use SP for all wrw.resp.
[wrw.git] / wrw / resp.py
CommitLineData
1f61bf31 1import dispatch, proto, env
ef04ec87
FT
2from sp import xhtml
3h = xhtml.cons()
dc7e5d54
FT
4
5__all__ = ["skeleton", "skelfor", "setskel", "usererror"]
6
7class skeleton(object):
ef04ec87
FT
8 def page(self, title, *content):
9 return h.html(self.head(title), h.body(*content))
dc7e5d54 10
c3b08e88 11 def head(self, title):
ef04ec87 12 return xhtml.head(title=title)
dc7e5d54 13
ef04ec87
FT
14 def error(self, message, *detail):
15 return self.page(message, h.h1(message), h.p(*detail))
dc7e5d54 16
ef04ec87
FT
17 def message(self, message, *detail):
18 return self.page(message, h.h1(message), h.p(*detail))
dc7e5d54 19
1f61bf31 20defskel = env.var(skeleton())
dc7e5d54
FT
21
22def getskel(req):
1f61bf31 23 return [defskel.val]
dc7e5d54
FT
24def skelfor(req):
25 return req.item(getskel)[0]
26def setskel(req, skel):
27 req.item(getskel)[0] = skel
28
29class usererror(dispatch.restart):
ef04ec87 30 def __init__(self, message, *detail):
dc7e5d54
FT
31 super(usererror, self).__init__()
32 self.message = message
33 self.detail = detail
34
35 def handle(self, req):
ef04ec87 36 return xhtml.forreq(req, skelfor(req).error(self.message, *self.detail))
dc7e5d54
FT
37
38class message(dispatch.restart):
ef04ec87 39 def __init__(self, msg, *detail):
dc7e5d54 40 super(message, self).__init__()
03e32061 41 self.message = msg
dc7e5d54
FT
42 self.detail = detail
43
44 def handle(self, req):
ef04ec87 45 return xhtml.forreq(req, skelfor(req).error(self.message, *self.detail))
dc7e5d54
FT
46
47class httperror(usererror):
48 def __init__(self, status, message = None, detail = None):
49 if message is None:
50 message = proto.statusinfo[status][0]
51 if detail is None:
ef04ec87
FT
52 detail = (proto.statusinfo[status][1],)
53 super(httperror, self).__init__(message, *detail)
dc7e5d54
FT
54 self.status = status
55
56 def handle(self, req):
57 req.status(self.status, self.message)
c3b08e88 58 return super(httperror, self).handle(req)
dc7e5d54
FT
59
60class notfound(httperror):
61 def __init__(self):
c3b08e88 62 return super(notfound, self).__init__(404)
4bd573f9
FT
63
64class redirect(dispatch.restart):
65 def __init__(self, url, status = 303):
66 super(redirect, self).__init__()
67 self.url = url
68 self.status = status
69
70 def handle(self, req):
71 req.status(self.status, "Redirect")
72 req.ohead["Location"] = proto.appendurl(proto.requrl(req), self.url)
73 return []