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 persession(data = None):
37 sess = session.get(req)
38 if callable not in sess:
40 sess[callable] = callable()
44 sess[callable] = callable(data)
45 return sess[callable].handle(req)
49 class preiter(object):
50 __slots__ = ["bk", "bki", "_next"]
52 def __init__(self, real):
62 if self._next is self.end:
66 self._next = next(self.bki)
72 if hasattr(self.bk, "close"):
76 def wrapper(*args, **kwargs):
77 return preiter(callable(*args, **kwargs))
80 class sessiondata(object):
82 def get(cls, req, create = True):
83 sess = cls.sessdb().get(req)
96 return session.default.val
98 class autodirty(sessiondata):
101 ret = super().get(req)
102 if "_is_dirty" not in ret.__dict__:
103 ret.__dict__["_is_dirty"] = False
106 def sessfrozen(self):
107 self.__dict__["_is_dirty"] = False
110 return self._is_dirty
112 def __setattr__(self, name, value):
113 super().__setattr__(name, value)
114 if "_is_dirty" in self.__dict__:
115 self.__dict__["_is_dirty"] = True
117 def __delattr__(self, name):
118 super().__delattr__(name, value)
119 if "_is_dirty" in self.__dict__:
120 self.__dict__["_is_dirty"] = True
122 class manudirty(object):
123 def __init__(self, *args, **kwargs):
124 super().__init__(*args, **kwargs)
127 def sessfrozen(self):
136 class specslot(object):
137 __slots__ = ["nm", "idx", "dirty"]
140 def __init__(self, nm, idx, dirty):
147 # Avoid calling __getattribute__
148 return specdirty.__sslots__.__get__(ins, type(ins))
150 def __get__(self, ins, cls):
151 val = self.slist(ins)[self.idx]
152 if val is specslot.unbound:
153 raise AttributeError("specslot %r is unbound" % self.nm)
156 def __set__(self, ins, val):
157 self.slist(ins)[self.idx] = val
161 def __delete__(self, ins):
162 self.slist(ins)[self.idx] = specslot.unbound
165 class specclass(type):
166 def __init__(self, name, bases, tdict):
167 super().__init__(name, bases, tdict)
170 for cls in self.__mro__:
171 css = cls.__dict__.get("__saveslots__", ())
173 dslots.update(cls.__dict__.get("__dirtyslots__", css))
174 self.__sslots_l__ = list(sslots)
175 self.__sslots_a__ = list(sslots | dslots)
176 for i, slot in enumerate(self.__sslots_a__):
177 setattr(self, slot, specslot(slot, i, slot in dslots))
179 class specdirty(sessiondata, metaclass=specclass):
180 __slots__ = ["session", "__sslots__", "_is_dirty"]
182 def __specinit__(self):
186 def __new__(cls, req, sess):
187 self = super().__new__(cls)
189 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
191 self._is_dirty = False
194 def __getnewargs__(self):
195 return (None, self.session)
198 self._is_dirty = True
200 def sessfrozen(self):
201 self._is_dirty = False
204 return self._is_dirty
206 def __getstate__(self):
208 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
209 if val is specslot.unbound:
210 ret[nm] = False, None
215 def __setstate__(self, st):
216 ss = specslot.slist(self)
217 for i, nm in enumerate(type(self).__sslots_a__):
218 bound, val = st.pop(nm, (False, None))
220 ss[i] = specslot.unbound