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 persession(data = None):
28 sess = session.get(req)
29 if callable not in sess:
31 sess[callable] = callable()
35 sess[callable] = callable(data)
36 return sess[callable].handle(req)
40 class preiter(object):
41 __slots__ = ["bk", "bki", "_next"]
43 def __init__(self, real):
53 if self._next is self.end:
57 self._next = next(self.bki)
63 if hasattr(self.bk, "close"):
67 def wrapper(*args, **kwargs):
68 return preiter(callable(*args, **kwargs))
71 class sessiondata(object):
73 def get(cls, req, create = True):
74 sess = cls.sessdb().get(req)
87 return session.default.val
89 class autodirty(sessiondata):
92 ret = super(autodirty, cls).get(req)
93 if "_is_dirty" not in ret.__dict__:
94 ret.__dict__["_is_dirty"] = False
98 self.__dict__["_is_dirty"] = False
101 return self._is_dirty
103 def __setattr__(self, name, value):
104 super(autodirty, self).__setattr__(name, value)
105 if "_is_dirty" in self.__dict__:
106 self.__dict__["_is_dirty"] = True
108 def __delattr__(self, name):
109 super(autodirty, self).__delattr__(name, value)
110 if "_is_dirty" in self.__dict__:
111 self.__dict__["_is_dirty"] = True
113 class manudirty(object):
114 def __init__(self, *args, **kwargs):
115 super(manudirty, self).__init__(*args, **kwargs)
118 def sessfrozen(self):
127 class specslot(object):
128 __slots__ = ["nm", "idx", "dirty"]
131 def __init__(self, nm, idx, dirty):
138 # Avoid calling __getattribute__
139 return specdirty.__sslots__.__get__(ins, type(ins))
141 def __get__(self, ins, cls):
142 val = self.slist(ins)[self.idx]
143 if val is specslot.unbound:
144 raise AttributeError("specslot %r is unbound" % self.nm)
147 def __set__(self, ins, val):
148 self.slist(ins)[self.idx] = val
152 def __delete__(self, ins):
153 self.slist(ins)[self.idx] = specslot.unbound
156 class specclass(type):
157 def __init__(self, name, bases, tdict):
158 super(specclass, self).__init__(name, bases, tdict)
161 for cls in self.__mro__:
162 css = cls.__dict__.get("__saveslots__", ())
164 dslots.update(cls.__dict__.get("__dirtyslots__", css))
165 self.__sslots_l__ = list(sslots)
166 self.__sslots_a__ = list(sslots | dslots)
167 for i, slot in enumerate(self.__sslots_a__):
168 setattr(self, slot, specslot(slot, i, slot in dslots))
170 class specdirty(sessiondata):
171 __metaclass__ = specclass
172 __slots__ = ["session", "__sslots__", "_is_dirty"]
174 def __specinit__(self):
178 def __new__(cls, req, sess):
179 self = super(specdirty, cls).__new__(cls)
181 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
183 self._is_dirty = False
186 def __getnewargs__(self):
187 return (None, self.session)
190 self._is_dirty = True
192 def sessfrozen(self):
193 self._is_dirty = False
196 return self._is_dirty
198 def __getstate__(self):
200 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
201 if val is specslot.unbound:
202 ret[nm] = False, None
207 def __setstate__(self, st):
208 ss = specslot.slist(self)
209 for i, nm in enumerate(type(self).__sslots_a__):
210 bound, val = st.pop(nm, (False, None))
212 ss[i] = specslot.unbound