2 import req, dispatch, session, form
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 return callable(**args)
22 def persession(data = None):
25 sess = session.get(req)
26 if callable not in sess:
28 sess[callable] = callable()
32 sess[callable] = callable(data)
33 return sess[callable].handle(req)
37 class preiter(object):
38 __slots__ = ["bk", "bki", "_next"]
40 def __init__(self, real):
50 if self._next is self.end:
54 self._next = next(self.bki)
60 if hasattr(self.bk, "close"):
64 def wrapper(*args, **kwargs):
65 return preiter(callable(*args, **kwargs))
68 class sessiondata(object):
70 def get(cls, req, create = True):
71 sess = cls.sessdb().get(req)
84 return session.default.val
86 class autodirty(sessiondata):
89 ret = super(autodirty, cls).get(req)
90 if "_is_dirty" not in ret.__dict__:
91 ret.__dict__["_is_dirty"] = False
95 self.__dict__["_is_dirty"] = False
100 def __setattr__(self, name, value):
101 super(autodirty, self).__setattr__(name, value)
102 if "_is_dirty" in self.__dict__:
103 self.__dict__["_is_dirty"] = True
105 def __delattr__(self, name):
106 super(autodirty, self).__delattr__(name, value)
107 if "_is_dirty" in self.__dict__:
108 self.__dict__["_is_dirty"] = True
110 class manudirty(object):
111 def __init__(self, *args, **kwargs):
112 super(manudirty, self).__init__(*args, **kwargs)
115 def sessfrozen(self):
124 class specslot(object):
125 __slots__ = ["nm", "idx", "dirty"]
128 def __init__(self, nm, idx, dirty):
135 # Avoid calling __getattribute__
136 return specdirty.__sslots__.__get__(ins, type(ins))
138 def __get__(self, ins, cls):
139 val = self.slist(ins)[self.idx]
140 if val is specslot.unbound:
141 raise AttributeError("specslot %r is unbound" % self.nm)
144 def __set__(self, ins, val):
145 self.slist(ins)[self.idx] = val
149 def __delete__(self, ins):
150 self.slist(ins)[self.idx] = specslot.unbound
153 class specclass(type):
154 def __init__(self, name, bases, tdict):
155 super(specclass, self).__init__(name, bases, tdict)
158 for cls in self.__mro__:
159 css = cls.__dict__.get("__saveslots__", ())
161 dslots.update(cls.__dict__.get("__dirtyslots__", css))
162 self.__sslots_l__ = list(sslots)
163 self.__sslots_a__ = list(sslots | dslots)
164 for i, slot in enumerate(self.__sslots_a__):
165 setattr(self, slot, specslot(slot, i, slot in dslots))
167 class specdirty(sessiondata):
168 __metaclass__ = specclass
169 __slots__ = ["session", "__sslots__", "_is_dirty"]
171 def __specinit__(self):
175 def __new__(cls, req, sess):
176 self = super(specdirty, cls).__new__(cls)
178 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
180 self._is_dirty = False
183 def __getnewargs__(self):
184 return (None, self.session)
187 self._is_dirty = True
189 def sessfrozen(self):
190 self._is_dirty = False
193 return self._is_dirty
195 def __getstate__(self):
197 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
198 if val is specslot.unbound:
199 ret[nm] = False, None
204 def __setstate__(self, st):
205 ss = specslot.slist(self)
206 for i, nm in enumerate(type(self).__sslots_a__):
207 bound, val = st.pop(nm, (False, None))
209 ss[i] = specslot.unbound