2 import req, dispatch, session, form, resp, proto
4 def wsgiwrap(callable):
5 def wrapper(env, startreq):
6 return dispatch.handleenv(env, startreq, callable)
7 wrapper.__wrapped__ = callable
10 def formparams(callable):
11 spec = inspect.getargspec(callable)
14 data = form.formdata(req)
16 raise resp.httperror(400, "Invalid request", "Form data was incomplete")
17 args = dict(data.items())
20 for arg in list(args):
21 if arg not in spec.args:
23 for i in xrange(len(spec.args) - (len(spec.defaults) if spec.defaults else 0)):
24 if spec.args[i] not in args:
25 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
26 return callable(**args)
27 wrapper.__wrapped__ = callable
30 class funplex(object):
31 def __init__(self, *funs, **nfuns):
33 self.dir.update(((self.unwrap(fun).__name__, fun) for fun in funs))
34 self.dir.update(nfuns)
38 while hasattr(fun, "__wrapped__"):
42 def __call__(self, req):
43 if req.pathinfo == "":
44 raise resp.redirect(req.uriname + "/")
45 if req.pathinfo[:1] != "/":
52 p = p.partition("/")[0]
56 sreq.selfpath = req.pathinfo[1:]
57 return self.dir[p](sreq)
61 self.dir[self.unwrap(fun).__name__] = fun
70 def persession(data=None):
73 sess = session.get(req)
74 if callable not in sess:
76 sess[callable] = callable()
80 sess[callable] = callable(data)
81 return sess[callable].handle(req)
82 wrapper.__wrapped__ = callable
86 class preiter(object):
87 __slots__ = ["bk", "bki", "_next"]
89 def __init__(self, real):
99 if self._next is self.end:
100 raise StopIteration()
103 self._next = next(self.bki)
104 except StopIteration:
105 self._next = self.end
109 if hasattr(self.bk, "close"):
112 def pregen(callable):
113 def wrapper(*args, **kwargs):
114 return preiter(callable(*args, **kwargs))
115 wrapper.__wrapped__ = callable
118 class sessiondata(object):
120 def get(cls, req, create=True):
121 sess = cls.sessdb().get(req)
134 return session.default.val
136 class autodirty(sessiondata):
139 ret = super(autodirty, cls).get(req)
140 if "_is_dirty" not in ret.__dict__:
141 ret.__dict__["_is_dirty"] = False
144 def sessfrozen(self):
145 self.__dict__["_is_dirty"] = False
148 return self._is_dirty
150 def __setattr__(self, name, value):
151 super(autodirty, self).__setattr__(name, value)
152 if "_is_dirty" in self.__dict__:
153 self.__dict__["_is_dirty"] = True
155 def __delattr__(self, name):
156 super(autodirty, self).__delattr__(name, value)
157 if "_is_dirty" in self.__dict__:
158 self.__dict__["_is_dirty"] = True
160 class manudirty(object):
161 def __init__(self, *args, **kwargs):
162 super(manudirty, self).__init__(*args, **kwargs)
165 def sessfrozen(self):
174 class specslot(object):
175 __slots__ = ["nm", "idx", "dirty"]
178 def __init__(self, nm, idx, dirty):
185 # Avoid calling __getattribute__
186 return specdirty.__sslots__.__get__(ins, type(ins))
188 def __get__(self, ins, cls):
189 val = self.slist(ins)[self.idx]
190 if val is specslot.unbound:
191 raise AttributeError("specslot %r is unbound" % self.nm)
194 def __set__(self, ins, val):
195 self.slist(ins)[self.idx] = val
199 def __delete__(self, ins):
200 self.slist(ins)[self.idx] = specslot.unbound
203 class specclass(type):
204 def __init__(self, name, bases, tdict):
205 super(specclass, self).__init__(name, bases, tdict)
208 for cls in self.__mro__:
209 css = cls.__dict__.get("__saveslots__", ())
211 dslots.update(cls.__dict__.get("__dirtyslots__", css))
212 self.__sslots_l__ = list(sslots)
213 self.__sslots_a__ = list(sslots | dslots)
214 for i, slot in enumerate(self.__sslots_a__):
215 setattr(self, slot, specslot(slot, i, slot in dslots))
217 class specdirty(sessiondata):
218 __metaclass__ = specclass
219 __slots__ = ["session", "__sslots__", "_is_dirty"]
221 def __specinit__(self):
225 def __new__(cls, req, sess):
226 self = super(specdirty, cls).__new__(cls)
228 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
230 self._is_dirty = False
233 def __getnewargs__(self):
234 return (None, self.session)
237 self._is_dirty = True
239 def sessfrozen(self):
240 self._is_dirty = False
243 return self._is_dirty
245 def __getstate__(self):
247 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
248 if val is specslot.unbound:
249 ret[nm] = False, None
254 def __setstate__(self, st):
255 ss = specslot.slist(self)
256 for i, nm in enumerate(type(self).__sslots_a__):
257 bound, val = st.pop(nm, (False, None))
259 ss[i] = specslot.unbound
263 def datecheck(req, mtime):
264 if "If-Modified-Since" in req.ihead:
265 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
266 if rtime is not None and rtime >= math.floor(mtime):
267 raise resp.unmodified()
268 req.ohead["Last-Modified"] = proto.httpdate(mtime)