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 sessiondata(object):
48 def get(cls, req, create = True):
49 sess = cls.sessdb().get(req)
62 return session.default.val
64 class autodirty(sessiondata):
67 ret = super().get(req)
68 if "_is_dirty" not in ret.__dict__:
69 ret.__dict__["_is_dirty"] = False
73 self.__dict__["_is_dirty"] = False
78 def __setattr__(self, name, value):
79 super().__setattr__(name, value)
80 if "_is_dirty" in self.__dict__:
81 self.__dict__["_is_dirty"] = True
83 def __delattr__(self, name):
84 super().__delattr__(name, value)
85 if "_is_dirty" in self.__dict__:
86 self.__dict__["_is_dirty"] = True
88 class manudirty(object):
89 def __init__(self, *args, **kwargs):
90 super().__init__(*args, **kwargs)
102 class specslot(object):
103 __slots__ = ["nm", "idx", "dirty"]
106 def __init__(self, nm, idx, dirty):
113 # Avoid calling __getattribute__
114 return specdirty.__sslots__.__get__(ins, type(ins))
116 def __get__(self, ins, cls):
117 val = self.slist(ins)[self.idx]
118 if val is specslot.unbound:
119 raise AttributeError("specslot %r is unbound" % self.nm)
122 def __set__(self, ins, val):
123 self.slist(ins)[self.idx] = val
127 def __delete__(self, ins):
128 self.slist(ins)[self.idx] = specslot.unbound
131 class specclass(type):
132 def __init__(self, name, bases, tdict):
133 super().__init__(name, bases, tdict)
136 for cls in self.__mro__:
137 css = cls.__dict__.get("__saveslots__", ())
139 dslots.update(cls.__dict__.get("__dirtyslots__", css))
140 self.__sslots_l__ = list(sslots)
141 self.__sslots_a__ = list(sslots | dslots)
142 for i, slot in enumerate(self.__sslots_a__):
143 setattr(self, slot, specslot(slot, i, slot in dslots))
145 class specdirty(sessiondata, metaclass=specclass):
146 __slots__ = ["session", "__sslots__", "_is_dirty"]
148 def __specinit__(self):
152 def __new__(cls, req, sess):
153 self = super().__new__(cls)
155 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
157 self._is_dirty = False
160 def __getnewargs__(self):
161 return (None, self.session)
164 self._is_dirty = True
166 def sessfrozen(self):
167 self._is_dirty = False
170 return self._is_dirty
172 def __getstate__(self):
174 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
175 if val is specslot.unbound:
176 ret[nm] = False, None
181 def __setstate__(self, st):
182 ss = specslot.slist(self)
183 for i, nm in enumerate(type(self).__sslots_a__):
184 bound, val = st.pop(nm, (False, None))
186 ss[i] = specslot.unbound