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 dir.update(((fun.__name__, fun) for fun in funs))
32 if req.pathinfo == "":
33 raise resp.redirect(req.uriname + "/")
34 if req.pathinfo[:1] != "/":
41 p = p.partition("/")[0]
44 return dir[p](req.shift(bi))
48 def persession(data = None):
51 sess = session.get(req)
52 if callable not in sess:
54 sess[callable] = callable()
58 sess[callable] = callable(data)
59 return sess[callable].handle(req)
60 wrapper.__wrapped__ = callable
64 class preiter(object):
65 __slots__ = ["bk", "bki", "_next"]
67 def __init__(self, real):
77 if self._next is self.end:
81 self._next = next(self.bki)
87 if hasattr(self.bk, "close"):
91 def wrapper(*args, **kwargs):
92 return preiter(callable(*args, **kwargs))
93 wrapper.__wrapped__ = callable
96 class sessiondata(object):
98 def get(cls, req, create = True):
99 sess = cls.sessdb().get(req)
112 return session.default.val
114 class autodirty(sessiondata):
117 ret = super(autodirty, cls).get(req)
118 if "_is_dirty" not in ret.__dict__:
119 ret.__dict__["_is_dirty"] = False
122 def sessfrozen(self):
123 self.__dict__["_is_dirty"] = False
126 return self._is_dirty
128 def __setattr__(self, name, value):
129 super(autodirty, self).__setattr__(name, value)
130 if "_is_dirty" in self.__dict__:
131 self.__dict__["_is_dirty"] = True
133 def __delattr__(self, name):
134 super(autodirty, self).__delattr__(name, value)
135 if "_is_dirty" in self.__dict__:
136 self.__dict__["_is_dirty"] = True
138 class manudirty(object):
139 def __init__(self, *args, **kwargs):
140 super(manudirty, self).__init__(*args, **kwargs)
143 def sessfrozen(self):
152 class specslot(object):
153 __slots__ = ["nm", "idx", "dirty"]
156 def __init__(self, nm, idx, dirty):
163 # Avoid calling __getattribute__
164 return specdirty.__sslots__.__get__(ins, type(ins))
166 def __get__(self, ins, cls):
167 val = self.slist(ins)[self.idx]
168 if val is specslot.unbound:
169 raise AttributeError("specslot %r is unbound" % self.nm)
172 def __set__(self, ins, val):
173 self.slist(ins)[self.idx] = val
177 def __delete__(self, ins):
178 self.slist(ins)[self.idx] = specslot.unbound
181 class specclass(type):
182 def __init__(self, name, bases, tdict):
183 super(specclass, self).__init__(name, bases, tdict)
186 for cls in self.__mro__:
187 css = cls.__dict__.get("__saveslots__", ())
189 dslots.update(cls.__dict__.get("__dirtyslots__", css))
190 self.__sslots_l__ = list(sslots)
191 self.__sslots_a__ = list(sslots | dslots)
192 for i, slot in enumerate(self.__sslots_a__):
193 setattr(self, slot, specslot(slot, i, slot in dslots))
195 class specdirty(sessiondata):
196 __metaclass__ = specclass
197 __slots__ = ["session", "__sslots__", "_is_dirty"]
199 def __specinit__(self):
203 def __new__(cls, req, sess):
204 self = super(specdirty, cls).__new__(cls)
206 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
208 self._is_dirty = False
211 def __getnewargs__(self):
212 return (None, self.session)
215 self._is_dirty = True
217 def sessfrozen(self):
218 self._is_dirty = False
221 return self._is_dirty
223 def __getstate__(self):
225 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
226 if val is specslot.unbound:
227 ret[nm] = False, None
232 def __setstate__(self, st):
233 ss = specslot.slist(self)
234 for i, nm in enumerate(type(self).__sslots_a__):
235 bound, val = st.pop(nm, (False, None))
237 ss[i] = specslot.unbound
241 def datecheck(req, mtime):
242 if "If-Modified-Since" in req.ihead:
243 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
244 if rtime >= math.floor(mtime):
245 raise resp.unmodified()
246 req.ohead["Last-Modified"] = proto.httpdate(mtime)