2 from . import req, dispatch, session, form
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 return callable(**args)
31 def persession(data = None):
34 sess = session.get(req)
35 if callable not in sess:
37 sess[callable] = callable()
41 sess[callable] = callable(data)
42 return sess[callable].handle(req)
46 class preiter(object):
47 __slots__ = ["bk", "bki", "_next"]
49 def __init__(self, real):
59 if self._next is self.end:
63 self._next = next(self.bki)
69 if hasattr(self.bk, "close"):
73 def wrapper(*args, **kwargs):
74 return preiter(callable(*args, **kwargs))
77 class sessiondata(object):
79 def get(cls, req, create = True):
80 sess = cls.sessdb().get(req)
93 return session.default.val
95 class autodirty(sessiondata):
98 ret = super().get(req)
99 if "_is_dirty" not in ret.__dict__:
100 ret.__dict__["_is_dirty"] = False
103 def sessfrozen(self):
104 self.__dict__["_is_dirty"] = False
107 return self._is_dirty
109 def __setattr__(self, name, value):
110 super().__setattr__(name, value)
111 if "_is_dirty" in self.__dict__:
112 self.__dict__["_is_dirty"] = True
114 def __delattr__(self, name):
115 super().__delattr__(name, value)
116 if "_is_dirty" in self.__dict__:
117 self.__dict__["_is_dirty"] = True
119 class manudirty(object):
120 def __init__(self, *args, **kwargs):
121 super().__init__(*args, **kwargs)
124 def sessfrozen(self):
133 class specslot(object):
134 __slots__ = ["nm", "idx", "dirty"]
137 def __init__(self, nm, idx, dirty):
144 # Avoid calling __getattribute__
145 return specdirty.__sslots__.__get__(ins, type(ins))
147 def __get__(self, ins, cls):
148 val = self.slist(ins)[self.idx]
149 if val is specslot.unbound:
150 raise AttributeError("specslot %r is unbound" % self.nm)
153 def __set__(self, ins, val):
154 self.slist(ins)[self.idx] = val
158 def __delete__(self, ins):
159 self.slist(ins)[self.idx] = specslot.unbound
162 class specclass(type):
163 def __init__(self, name, bases, tdict):
164 super().__init__(name, bases, tdict)
167 for cls in self.__mro__:
168 css = cls.__dict__.get("__saveslots__", ())
170 dslots.update(cls.__dict__.get("__dirtyslots__", css))
171 self.__sslots_l__ = list(sslots)
172 self.__sslots_a__ = list(sslots | dslots)
173 for i, slot in enumerate(self.__sslots_a__):
174 setattr(self, slot, specslot(slot, i, slot in dslots))
176 class specdirty(sessiondata, metaclass=specclass):
177 __slots__ = ["session", "__sslots__", "_is_dirty"]
179 def __specinit__(self):
183 def __new__(cls, req, sess):
184 self = super().__new__(cls)
186 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
188 self._is_dirty = False
191 def __getnewargs__(self):
192 return (None, self.session)
195 self._is_dirty = True
197 def sessfrozen(self):
198 self._is_dirty = False
201 return self._is_dirty
203 def __getstate__(self):
205 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
206 if val is specslot.unbound:
207 ret[nm] = False, None
212 def __setstate__(self, st):
213 ss = specslot.slist(self)
214 for i, nm in enumerate(type(self).__sslots_a__):
215 bound, val = st.pop(nm, (False, None))
217 ss[i] = specslot.unbound