2 import req, dispatch, session, form, resp
4 def wsgiwrap(callable):
5 def wrapper(env, startreq):
6 return dispatch.handleenv(env, startreq, callable)
9 def formparams(callable):
11 data = form.formdata(req)
12 spec = inspect.getargspec(callable)
13 args = dict(data.items())
16 for arg in list(args):
17 if arg not in spec.args:
19 for i in xrange(len(spec.args) - len(spec.defaults)):
20 if spec.args[i] not in args:
21 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
22 return callable(**args)
25 def funplex(*funs, **nfuns):
27 dir.update(((fun.__name__, fun) for fun in funs))
30 if req.pathinfo == "":
31 raise resp.redirect(req.uriname + "/")
32 if req.pathinfo[:1] != "/":
39 p = p.partition("/")[0]
42 return dir[p](req.shift(bi))
46 def persession(data = None):
49 sess = session.get(req)
50 if callable not in sess:
52 sess[callable] = callable()
56 sess[callable] = callable(data)
57 return sess[callable].handle(req)
61 class preiter(object):
62 __slots__ = ["bk", "bki", "_next"]
64 def __init__(self, real):
74 if self._next is self.end:
78 self._next = next(self.bki)
84 if hasattr(self.bk, "close"):
88 def wrapper(*args, **kwargs):
89 return preiter(callable(*args, **kwargs))
92 class sessiondata(object):
94 def get(cls, req, create = True):
95 sess = cls.sessdb().get(req)
108 return session.default.val
110 class autodirty(sessiondata):
113 ret = super(autodirty, cls).get(req)
114 if "_is_dirty" not in ret.__dict__:
115 ret.__dict__["_is_dirty"] = False
118 def sessfrozen(self):
119 self.__dict__["_is_dirty"] = False
122 return self._is_dirty
124 def __setattr__(self, name, value):
125 super(autodirty, self).__setattr__(name, value)
126 if "_is_dirty" in self.__dict__:
127 self.__dict__["_is_dirty"] = True
129 def __delattr__(self, name):
130 super(autodirty, self).__delattr__(name, value)
131 if "_is_dirty" in self.__dict__:
132 self.__dict__["_is_dirty"] = True
134 class manudirty(object):
135 def __init__(self, *args, **kwargs):
136 super(manudirty, self).__init__(*args, **kwargs)
139 def sessfrozen(self):
148 class specslot(object):
149 __slots__ = ["nm", "idx", "dirty"]
152 def __init__(self, nm, idx, dirty):
159 # Avoid calling __getattribute__
160 return specdirty.__sslots__.__get__(ins, type(ins))
162 def __get__(self, ins, cls):
163 val = self.slist(ins)[self.idx]
164 if val is specslot.unbound:
165 raise AttributeError("specslot %r is unbound" % self.nm)
168 def __set__(self, ins, val):
169 self.slist(ins)[self.idx] = val
173 def __delete__(self, ins):
174 self.slist(ins)[self.idx] = specslot.unbound
177 class specclass(type):
178 def __init__(self, name, bases, tdict):
179 super(specclass, self).__init__(name, bases, tdict)
182 for cls in self.__mro__:
183 css = cls.__dict__.get("__saveslots__", ())
185 dslots.update(cls.__dict__.get("__dirtyslots__", css))
186 self.__sslots_l__ = list(sslots)
187 self.__sslots_a__ = list(sslots | dslots)
188 for i, slot in enumerate(self.__sslots_a__):
189 setattr(self, slot, specslot(slot, i, slot in dslots))
191 class specdirty(sessiondata):
192 __metaclass__ = specclass
193 __slots__ = ["session", "__sslots__", "_is_dirty"]
195 def __specinit__(self):
199 def __new__(cls, req, sess):
200 self = super(specdirty, cls).__new__(cls)
202 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
204 self._is_dirty = False
207 def __getnewargs__(self):
208 return (None, self.session)
211 self._is_dirty = True
213 def sessfrozen(self):
214 self._is_dirty = False
217 return self._is_dirty
219 def __getstate__(self):
221 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
222 if val is specslot.unbound:
223 ret[nm] = False, None
228 def __setstate__(self, st):
229 ss = specslot.slist(self)
230 for i, nm in enumerate(type(self).__sslots_a__):
231 bound, val = st.pop(nm, (False, None))
233 ss[i] = specslot.unbound