1 from __future__ import with_statement
3 import req, dispatch, session, form
5 def wsgiwrap(callable):
6 def wrapper(env, startreq):
7 return dispatch.handle(req.origrequest(env), startreq, callable)
10 def formparams(callable):
12 data = form.formdata(req)
13 spec = inspect.getargspec(callable)
14 args = dict(data.items())
17 for arg in list(args):
18 if arg not in spec[0]:
20 return callable(**args)
23 def persession(data = None):
26 sess = session.get(req)
27 if callable not in sess:
29 sess[callable] = callable()
33 sess[callable] = callable(data)
34 return sess[callable].handle(req)
38 class sessiondata(object):
40 def get(cls, req, create = True):
41 sess = cls.sessdb().get(req)
54 return session.default.val
56 class autodirty(sessiondata):
59 ret = super(autodirty, cls).get(req)
60 if "_is_dirty" not in ret.__dict__:
61 ret.__dict__["_is_dirty"] = False
65 self.__dict__["_is_dirty"] = False
70 def __setattr__(self, name, value):
71 super(autodirty, self).__setattr__(name, value)
72 if "_is_dirty" in self.__dict__:
73 self.__dict__["_is_dirty"] = True
75 def __delattr__(self, name):
76 super(autodirty, self).__delattr__(name, value)
77 if "_is_dirty" in self.__dict__:
78 self.__dict__["_is_dirty"] = True
80 class manudirty(object):
81 def __init__(self, *args, **kwargs):
82 super(manudirty, self).__init__(*args, **kwargs)
94 class specslot(object):
95 __slots__ = ["nm", "idx", "dirty"]
98 def __init__(self, nm, idx, dirty):
105 # Avoid calling __getattribute__
106 return specdirty.__sslots__.__get__(ins, type(ins))
108 def __get__(self, ins, cls):
109 val = self.slist(ins)[self.idx]
110 if val is specslot.unbound:
111 raise AttributeError("specslot %r is unbound" % self.nm)
114 def __set__(self, ins, val):
115 self.slist(ins)[self.idx] = val
119 def __delete__(self, ins):
120 self.slist(ins)[self.idx] = specslot.unbound
123 class specclass(type):
124 def __init__(self, name, bases, tdict):
125 super(specclass, self).__init__(name, bases, tdict)
128 for cls in self.__mro__:
129 css = cls.__dict__.get("__saveslots__", ())
131 dslots.update(cls.__dict__.get("__dirtyslots__", css))
132 self.__sslots_l__ = list(sslots)
133 self.__sslots_a__ = list(sslots | dslots)
134 for i, slot in enumerate(self.__sslots_a__):
135 setattr(self, slot, specslot(slot, i, slot in dslots))
137 class specdirty(sessiondata):
138 __metaclass__ = specclass
139 __slots__ = ["session", "__sslots__", "_is_dirty"]
141 def __specinit__(self):
145 def __new__(cls, req, sess):
146 self = super(specdirty, cls).__new__(cls)
148 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
150 self._is_dirty = False
153 def __getnewargs__(self):
154 return (None, self.session)
157 self._is_dirty = True
159 def sessfrozen(self):
160 self._is_dirty = False
163 return self._is_dirty
165 def __getstate__(self):
167 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
168 if val is specslot.unbound:
169 ret[nm] = False, None
174 def __setstate__(self, st):
175 ss = specslot.slist(self)
176 for i, nm in enumerate(type(self).__sslots_a__):
177 bound, val = st.pop(nm, (False, None))
179 ss[i] = specslot.unbound