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)
13 data = form.formdata(req)
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) if spec.defaults else 0)):
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 class funplex(object):
28 def __init__(self, *funs, **nfuns):
30 self.dir.update(((self.unwrap(fun).__name__, fun) for fun in funs))
31 self.dir.update(nfuns)
35 while hasattr(fun, "__wrapped__"):
39 def __call__(self, req):
40 if req.pathinfo == "":
41 raise resp.redirect(req.uriname + "/")
42 if req.pathinfo[:1] != "/":
49 p = p.partition("/")[0]
52 return self.dir[p](req.shift(bi))
56 self.dir[self.unwrap(fun).__name__] = fun
65 def persession(data=None):
68 sess = session.get(req)
69 if callable not in sess:
71 sess[callable] = callable()
75 sess[callable] = callable(data)
76 return sess[callable].handle(req)
77 wrapper.__wrapped__ = callable
81 class preiter(object):
82 __slots__ = ["bk", "bki", "_next"]
84 def __init__(self, real):
94 if self._next is self.end:
98 self._next = next(self.bki)
100 self._next = self.end
104 if hasattr(self.bk, "close"):
107 def pregen(callable):
108 def wrapper(*args, **kwargs):
109 return preiter(callable(*args, **kwargs))
110 wrapper.__wrapped__ = callable
113 class sessiondata(object):
115 def get(cls, req, create=True):
116 sess = cls.sessdb().get(req)
129 return session.default.val
131 class autodirty(sessiondata):
134 ret = super(autodirty, cls).get(req)
135 if "_is_dirty" not in ret.__dict__:
136 ret.__dict__["_is_dirty"] = False
139 def sessfrozen(self):
140 self.__dict__["_is_dirty"] = False
143 return self._is_dirty
145 def __setattr__(self, name, value):
146 super(autodirty, self).__setattr__(name, value)
147 if "_is_dirty" in self.__dict__:
148 self.__dict__["_is_dirty"] = True
150 def __delattr__(self, name):
151 super(autodirty, self).__delattr__(name, value)
152 if "_is_dirty" in self.__dict__:
153 self.__dict__["_is_dirty"] = True
155 class manudirty(object):
156 def __init__(self, *args, **kwargs):
157 super(manudirty, self).__init__(*args, **kwargs)
160 def sessfrozen(self):
169 class specslot(object):
170 __slots__ = ["nm", "idx", "dirty"]
173 def __init__(self, nm, idx, dirty):
180 # Avoid calling __getattribute__
181 return specdirty.__sslots__.__get__(ins, type(ins))
183 def __get__(self, ins, cls):
184 val = self.slist(ins)[self.idx]
185 if val is specslot.unbound:
186 raise AttributeError("specslot %r is unbound" % self.nm)
189 def __set__(self, ins, val):
190 self.slist(ins)[self.idx] = val
194 def __delete__(self, ins):
195 self.slist(ins)[self.idx] = specslot.unbound
198 class specclass(type):
199 def __init__(self, name, bases, tdict):
200 super(specclass, self).__init__(name, bases, tdict)
203 for cls in self.__mro__:
204 css = cls.__dict__.get("__saveslots__", ())
206 dslots.update(cls.__dict__.get("__dirtyslots__", css))
207 self.__sslots_l__ = list(sslots)
208 self.__sslots_a__ = list(sslots | dslots)
209 for i, slot in enumerate(self.__sslots_a__):
210 setattr(self, slot, specslot(slot, i, slot in dslots))
212 class specdirty(sessiondata):
213 __metaclass__ = specclass
214 __slots__ = ["session", "__sslots__", "_is_dirty"]
216 def __specinit__(self):
220 def __new__(cls, req, sess):
221 self = super(specdirty, cls).__new__(cls)
223 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
225 self._is_dirty = False
228 def __getnewargs__(self):
229 return (None, self.session)
232 self._is_dirty = True
234 def sessfrozen(self):
235 self._is_dirty = False
238 return self._is_dirty
240 def __getstate__(self):
242 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
243 if val is specslot.unbound:
244 ret[nm] = False, None
249 def __setstate__(self, st):
250 ss = specslot.slist(self)
251 for i, nm in enumerate(type(self).__sslots_a__):
252 bound, val = st.pop(nm, (False, None))
254 ss[i] = specslot.unbound
258 def datecheck(req, mtime):
259 if "If-Modified-Since" in req.ihead:
260 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
261 if rtime >= math.floor(mtime):
262 raise resp.unmodified()
263 req.ohead["Last-Modified"] = proto.httpdate(mtime)