2 from . import dispatch, proto, env
6 __all__ = ["skeleton", "skelfor", "setskel", "usererror"]
8 class skeleton(object):
9 def page(self, req, title, *content):
10 return xhtml.forreq(req, h.html(self.head(req, title), h.body(*content)))
12 def head(self, req, title):
13 return xhtml.head(title=title)
15 def error(self, req, message, *detail):
16 return self.page(req, message, h.h1(message), h.p(*detail))
18 def message(self, req, message, *detail):
19 return self.page(req, message, h.h1(message), h.p(*detail))
21 defskel = env.var(skeleton())
26 return req.item(getskel)[0]
27 def setskel(req, skel):
28 req.item(getskel)[0] = skel
30 class usererror(dispatch.restart):
31 def __init__(self, message, *detail):
33 self.message = message
36 def handle(self, req):
37 return skelfor(req).error(req, self.message, *self.detail)
39 class message(dispatch.restart):
40 def __init__(self, message, *detail):
42 self.message = message
45 def handle(self, req):
46 return skelfor(req).message(req, self.message, *self.detail)
48 class httperror(usererror):
49 def __init__(self, status, message=None, detail=None):
51 message = proto.statusinfo[status][0]
53 detail = (proto.statusinfo[status][1],)
54 super().__init__(message, *detail)
57 def handle(self, req):
58 req.status(self.status, self.message)
59 return super().handle(req)
61 class notfound(httperror):
63 return super().__init__(404)
65 class redirect(dispatch.restart):
66 bases = {"url": proto.requrl,
67 "script": proto.scripturl,
68 "site": proto.siteurl}
70 def __init__(self, url, status=303, base="url"):
77 def handle(self, req):
78 req.status(self.status, "Redirect")
79 req.ohead["Location"] = proto.appendurl(self.bases[self.base](req), self.url)
80 req.ohead["Content-Length"] = 0
83 class unmodified(dispatch.restart):
84 def handle(self, req):
85 req.status(304, "Not Modified")
86 req.ohead["Content-Length"] = "0"
89 class fileiter(object):
90 def __init__(self, fp):
99 data = self.fp.read(16384)
103 raise StopIteration()
107 if self.fp is not None:
111 class fileresp(dispatch.restart):
112 def __init__(self, fp, ctype):
116 def handle(self, req):
117 req.ohead["Content-Type"] = self.ctype
118 if hasattr(self.fp, "fileno"):
119 sz = os.fstat(self.fp.fileno()).st_size
121 req.ohead["Content-Length"] = str(sz)
122 return fileiter(self.fp)