2 from . import req, dispatch, session, form, resp
4 def wsgiwrap(callable):
5 def wrapper(env, startreq):
6 return dispatch.handleenv(env, startreq, callable)
9 def stringwrap(charset):
11 def wrapper(*args, **kwargs):
12 bk = callable(*args, **kwargs)
14 yield string.encode(charset)
18 def formparams(callable):
20 data = form.formdata(req)
21 spec = inspect.getargspec(callable)
22 args = dict(data.items())
25 for arg in list(args):
26 if arg not in spec.args:
28 for i in range(len(spec.args) - len(spec.defaults)):
29 if spec.args[i] not in args:
30 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
31 return callable(**args)
34 def funplex(*funs, **nfuns):
36 dir.update(((fun.__name__, fun) for fun in funs))
39 if req.pathinfo == "":
40 raise resp.redirect(req.uriname + "/")
41 if req.pathinfo[:1] != "/":
48 p = p.partition("/")[0]
51 return dir[p](req.shift(bi))
55 def persession(data = None):
58 sess = session.get(req)
59 if callable not in sess:
61 sess[callable] = callable()
65 sess[callable] = callable(data)
66 return sess[callable].handle(req)
70 class preiter(object):
71 __slots__ = ["bk", "bki", "_next"]
73 def __init__(self, real):
83 if self._next is self.end:
87 self._next = next(self.bki)
93 if hasattr(self.bk, "close"):
97 def wrapper(*args, **kwargs):
98 return preiter(callable(*args, **kwargs))
101 class sessiondata(object):
103 def get(cls, req, create = True):
104 sess = cls.sessdb().get(req)
117 return session.default.val
119 class autodirty(sessiondata):
122 ret = super().get(req)
123 if "_is_dirty" not in ret.__dict__:
124 ret.__dict__["_is_dirty"] = False
127 def sessfrozen(self):
128 self.__dict__["_is_dirty"] = False
131 return self._is_dirty
133 def __setattr__(self, name, value):
134 super().__setattr__(name, value)
135 if "_is_dirty" in self.__dict__:
136 self.__dict__["_is_dirty"] = True
138 def __delattr__(self, name):
139 super().__delattr__(name, value)
140 if "_is_dirty" in self.__dict__:
141 self.__dict__["_is_dirty"] = True
143 class manudirty(object):
144 def __init__(self, *args, **kwargs):
145 super().__init__(*args, **kwargs)
148 def sessfrozen(self):
157 class specslot(object):
158 __slots__ = ["nm", "idx", "dirty"]
161 def __init__(self, nm, idx, dirty):
168 # Avoid calling __getattribute__
169 return specdirty.__sslots__.__get__(ins, type(ins))
171 def __get__(self, ins, cls):
172 val = self.slist(ins)[self.idx]
173 if val is specslot.unbound:
174 raise AttributeError("specslot %r is unbound" % self.nm)
177 def __set__(self, ins, val):
178 self.slist(ins)[self.idx] = val
182 def __delete__(self, ins):
183 self.slist(ins)[self.idx] = specslot.unbound
186 class specclass(type):
187 def __init__(self, name, bases, tdict):
188 super().__init__(name, bases, tdict)
191 for cls in self.__mro__:
192 css = cls.__dict__.get("__saveslots__", ())
194 dslots.update(cls.__dict__.get("__dirtyslots__", css))
195 self.__sslots_l__ = list(sslots)
196 self.__sslots_a__ = list(sslots | dslots)
197 for i, slot in enumerate(self.__sslots_a__):
198 setattr(self, slot, specslot(slot, i, slot in dslots))
200 class specdirty(sessiondata, metaclass=specclass):
201 __slots__ = ["session", "__sslots__", "_is_dirty"]
203 def __specinit__(self):
207 def __new__(cls, req, sess):
208 self = super().__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