2 from . import req, dispatch, session, form, resp, proto
4 def wsgiwrap(callable):
5 def wrapper(env, startreq):
6 return dispatch.handleenv(env, startreq, callable)
7 wrapper.__wrapped__ = callable
10 def stringwrap(charset):
12 def wrapper(*args, **kwargs):
13 bk = callable(*args, **kwargs)
15 yield string.encode(charset)
16 wrapper.__wrapped__ = callable
20 def formparams(callable):
21 spec = inspect.getargspec(callable)
23 data = form.formdata(req)
24 args = dict(data.items())
27 for arg in list(args):
28 if arg not in spec.args:
30 for i in range(len(spec.args) - (len(spec.defaults) if spec.defaults else 0)):
31 if spec.args[i] not in args:
32 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
33 return callable(**args)
34 wrapper.__wrapped__ = callable
37 class funplex(object):
38 def __init__(self, *funs, **nfuns):
40 self.dir.update(((self.unwrap(fun).__name__, fun) for fun in funs))
41 self.dir.update(nfuns)
45 while hasattr(fun, "__wrapped__"):
49 def __call__(self, req):
50 if req.pathinfo == "":
51 raise resp.redirect(req.uriname + "/")
52 if req.pathinfo[:1] != "/":
59 p = p.partition("/")[0]
62 return self.dir[p](req.shift(bi))
66 self.dir[self.unwrap(fun).__name__] = fun
75 def persession(data=None):
78 sess = session.get(req)
79 if callable not in sess:
81 sess[callable] = callable()
85 sess[callable] = callable(data)
86 return sess[callable].handle(req)
87 wrapper.__wrapped__ = callable
91 class preiter(object):
92 __slots__ = ["bk", "bki", "_next"]
94 def __init__(self, real):
104 if self._next is self.end:
105 raise StopIteration()
108 self._next = next(self.bki)
109 except StopIteration:
110 self._next = self.end
114 if hasattr(self.bk, "close"):
117 def pregen(callable):
118 def wrapper(*args, **kwargs):
119 return preiter(callable(*args, **kwargs))
120 wrapper.__wrapped__ = callable
123 class sessiondata(object):
125 def get(cls, req, create=True):
126 sess = cls.sessdb().get(req)
139 return session.default.val
141 class autodirty(sessiondata):
144 ret = super().get(req)
145 if "_is_dirty" not in ret.__dict__:
146 ret.__dict__["_is_dirty"] = False
149 def sessfrozen(self):
150 self.__dict__["_is_dirty"] = False
153 return self._is_dirty
155 def __setattr__(self, name, value):
156 super().__setattr__(name, value)
157 if "_is_dirty" in self.__dict__:
158 self.__dict__["_is_dirty"] = True
160 def __delattr__(self, name):
161 super().__delattr__(name, value)
162 if "_is_dirty" in self.__dict__:
163 self.__dict__["_is_dirty"] = True
165 class manudirty(object):
166 def __init__(self, *args, **kwargs):
167 super().__init__(*args, **kwargs)
170 def sessfrozen(self):
179 class specslot(object):
180 __slots__ = ["nm", "idx", "dirty"]
183 def __init__(self, nm, idx, dirty):
190 # Avoid calling __getattribute__
191 return specdirty.__sslots__.__get__(ins, type(ins))
193 def __get__(self, ins, cls):
194 val = self.slist(ins)[self.idx]
195 if val is specslot.unbound:
196 raise AttributeError("specslot %r is unbound" % self.nm)
199 def __set__(self, ins, val):
200 self.slist(ins)[self.idx] = val
204 def __delete__(self, ins):
205 self.slist(ins)[self.idx] = specslot.unbound
208 class specclass(type):
209 def __init__(self, name, bases, tdict):
210 super().__init__(name, bases, tdict)
213 for cls in self.__mro__:
214 css = cls.__dict__.get("__saveslots__", ())
216 dslots.update(cls.__dict__.get("__dirtyslots__", css))
217 self.__sslots_l__ = list(sslots)
218 self.__sslots_a__ = list(sslots | dslots)
219 for i, slot in enumerate(self.__sslots_a__):
220 setattr(self, slot, specslot(slot, i, slot in dslots))
222 class specdirty(sessiondata, metaclass=specclass):
223 __slots__ = ["session", "__sslots__", "_is_dirty"]
225 def __specinit__(self):
229 def __new__(cls, req, sess):
230 self = super().__new__(cls)
232 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
234 self._is_dirty = False
237 def __getnewargs__(self):
238 return (None, self.session)
241 self._is_dirty = True
243 def sessfrozen(self):
244 self._is_dirty = False
247 return self._is_dirty
249 def __getstate__(self):
251 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
252 if val is specslot.unbound:
253 ret[nm] = False, None
258 def __setstate__(self, st):
259 ss = specslot.slist(self)
260 for i, nm in enumerate(type(self).__sslots_a__):
261 bound, val = st.pop(nm, (False, None))
263 ss[i] = specslot.unbound
267 def datecheck(req, mtime):
268 if "If-Modified-Since" in req.ihead:
269 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
270 if rtime >= math.floor(mtime):
271 raise resp.unmodified()
272 req.ohead["Last-Modified"] = proto.httpdate(mtime)