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):
12 data = form.formdata(req)
13 spec = inspect.getargspec(callable)
14 args = dict(data.items())
17 for arg in list(args):
18 if arg not in spec.args:
20 for i in xrange(len(spec.args) - len(spec.defaults)):
21 if spec.args[i] not in args:
22 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
23 return callable(**args)
24 wrapper.__wrapped__ = callable
27 def funplex(*funs, **nfuns):
29 while hasattr(fun, "__wrapped__"):
33 dir.update(((unwrap(fun).__name__, fun) for fun in funs))
36 if req.pathinfo == "":
37 raise resp.redirect(req.uriname + "/")
38 if req.pathinfo[:1] != "/":
45 p = p.partition("/")[0]
48 return dir[p](req.shift(bi))
52 def persession(data = None):
55 sess = session.get(req)
56 if callable not in sess:
58 sess[callable] = callable()
62 sess[callable] = callable(data)
63 return sess[callable].handle(req)
64 wrapper.__wrapped__ = callable
68 class preiter(object):
69 __slots__ = ["bk", "bki", "_next"]
71 def __init__(self, real):
81 if self._next is self.end:
85 self._next = next(self.bki)
91 if hasattr(self.bk, "close"):
95 def wrapper(*args, **kwargs):
96 return preiter(callable(*args, **kwargs))
97 wrapper.__wrapped__ = callable
100 class sessiondata(object):
102 def get(cls, req, create = True):
103 sess = cls.sessdb().get(req)
116 return session.default.val
118 class autodirty(sessiondata):
121 ret = super(autodirty, cls).get(req)
122 if "_is_dirty" not in ret.__dict__:
123 ret.__dict__["_is_dirty"] = False
126 def sessfrozen(self):
127 self.__dict__["_is_dirty"] = False
130 return self._is_dirty
132 def __setattr__(self, name, value):
133 super(autodirty, self).__setattr__(name, value)
134 if "_is_dirty" in self.__dict__:
135 self.__dict__["_is_dirty"] = True
137 def __delattr__(self, name):
138 super(autodirty, self).__delattr__(name, value)
139 if "_is_dirty" in self.__dict__:
140 self.__dict__["_is_dirty"] = True
142 class manudirty(object):
143 def __init__(self, *args, **kwargs):
144 super(manudirty, self).__init__(*args, **kwargs)
147 def sessfrozen(self):
156 class specslot(object):
157 __slots__ = ["nm", "idx", "dirty"]
160 def __init__(self, nm, idx, dirty):
167 # Avoid calling __getattribute__
168 return specdirty.__sslots__.__get__(ins, type(ins))
170 def __get__(self, ins, cls):
171 val = self.slist(ins)[self.idx]
172 if val is specslot.unbound:
173 raise AttributeError("specslot %r is unbound" % self.nm)
176 def __set__(self, ins, val):
177 self.slist(ins)[self.idx] = val
181 def __delete__(self, ins):
182 self.slist(ins)[self.idx] = specslot.unbound
185 class specclass(type):
186 def __init__(self, name, bases, tdict):
187 super(specclass, self).__init__(name, bases, tdict)
190 for cls in self.__mro__:
191 css = cls.__dict__.get("__saveslots__", ())
193 dslots.update(cls.__dict__.get("__dirtyslots__", css))
194 self.__sslots_l__ = list(sslots)
195 self.__sslots_a__ = list(sslots | dslots)
196 for i, slot in enumerate(self.__sslots_a__):
197 setattr(self, slot, specslot(slot, i, slot in dslots))
199 class specdirty(sessiondata):
200 __metaclass__ = specclass
201 __slots__ = ["session", "__sslots__", "_is_dirty"]
203 def __specinit__(self):
207 def __new__(cls, req, sess):
208 self = super(specdirty, cls).__new__(cls)
210 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
212 self._is_dirty = False
215 def __getnewargs__(self):
216 return (None, self.session)
219 self._is_dirty = True
221 def sessfrozen(self):
222 self._is_dirty = False
225 return self._is_dirty
227 def __getstate__(self):
229 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
230 if val is specslot.unbound:
231 ret[nm] = False, None
236 def __setstate__(self, st):
237 ss = specslot.slist(self)
238 for i, nm in enumerate(type(self).__sslots_a__):
239 bound, val = st.pop(nm, (False, None))
241 ss[i] = specslot.unbound
245 def datecheck(req, mtime):
246 if "If-Modified-Since" in req.ihead:
247 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
248 if rtime >= math.floor(mtime):
249 raise resp.unmodified()
250 req.ohead["Last-Modified"] = proto.httpdate(mtime)