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)
19 def formparams(callable):
21 data = form.formdata(req)
22 spec = inspect.getargspec(callable)
23 args = dict(data.items())
26 for arg in list(args):
27 if arg not in spec.args:
29 for i in range(len(spec.args) - len(spec.defaults)):
30 if spec.args[i] not in args:
31 raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
32 return callable(**args)
33 wrapper.__wrapped__ = callable
36 def funplex(*funs, **nfuns):
38 while hasattr(fun, "__wrapped__"):
42 dir.update(((unwrap(fun).__name__, fun) for fun in funs))
45 if req.pathinfo == "":
46 raise resp.redirect(req.uriname + "/")
47 if req.pathinfo[:1] != "/":
54 p = p.partition("/")[0]
57 return dir[p](req.shift(bi))
61 def persession(data = None):
64 sess = session.get(req)
65 if callable not in sess:
67 sess[callable] = callable()
71 sess[callable] = callable(data)
72 return sess[callable].handle(req)
73 wrapper.__wrapped__ = callable
77 class preiter(object):
78 __slots__ = ["bk", "bki", "_next"]
80 def __init__(self, real):
90 if self._next is self.end:
94 self._next = next(self.bki)
100 if hasattr(self.bk, "close"):
103 def pregen(callable):
104 def wrapper(*args, **kwargs):
105 return preiter(callable(*args, **kwargs))
106 wrapper.__wrapped__ = callable
109 class sessiondata(object):
111 def get(cls, req, create = True):
112 sess = cls.sessdb().get(req)
125 return session.default.val
127 class autodirty(sessiondata):
130 ret = super().get(req)
131 if "_is_dirty" not in ret.__dict__:
132 ret.__dict__["_is_dirty"] = False
135 def sessfrozen(self):
136 self.__dict__["_is_dirty"] = False
139 return self._is_dirty
141 def __setattr__(self, name, value):
142 super().__setattr__(name, value)
143 if "_is_dirty" in self.__dict__:
144 self.__dict__["_is_dirty"] = True
146 def __delattr__(self, name):
147 super().__delattr__(name, value)
148 if "_is_dirty" in self.__dict__:
149 self.__dict__["_is_dirty"] = True
151 class manudirty(object):
152 def __init__(self, *args, **kwargs):
153 super().__init__(*args, **kwargs)
156 def sessfrozen(self):
165 class specslot(object):
166 __slots__ = ["nm", "idx", "dirty"]
169 def __init__(self, nm, idx, dirty):
176 # Avoid calling __getattribute__
177 return specdirty.__sslots__.__get__(ins, type(ins))
179 def __get__(self, ins, cls):
180 val = self.slist(ins)[self.idx]
181 if val is specslot.unbound:
182 raise AttributeError("specslot %r is unbound" % self.nm)
185 def __set__(self, ins, val):
186 self.slist(ins)[self.idx] = val
190 def __delete__(self, ins):
191 self.slist(ins)[self.idx] = specslot.unbound
194 class specclass(type):
195 def __init__(self, name, bases, tdict):
196 super().__init__(name, bases, tdict)
199 for cls in self.__mro__:
200 css = cls.__dict__.get("__saveslots__", ())
202 dslots.update(cls.__dict__.get("__dirtyslots__", css))
203 self.__sslots_l__ = list(sslots)
204 self.__sslots_a__ = list(sslots | dslots)
205 for i, slot in enumerate(self.__sslots_a__):
206 setattr(self, slot, specslot(slot, i, slot in dslots))
208 class specdirty(sessiondata, metaclass=specclass):
209 __slots__ = ["session", "__sslots__", "_is_dirty"]
211 def __specinit__(self):
215 def __new__(cls, req, sess):
216 self = super().__new__(cls)
218 self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
220 self._is_dirty = False
223 def __getnewargs__(self):
224 return (None, self.session)
227 self._is_dirty = True
229 def sessfrozen(self):
230 self._is_dirty = False
233 return self._is_dirty
235 def __getstate__(self):
237 for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)):
238 if val is specslot.unbound:
239 ret[nm] = False, None
244 def __setstate__(self, st):
245 ss = specslot.slist(self)
246 for i, nm in enumerate(type(self).__sslots_a__):
247 bound, val = st.pop(nm, (False, None))
249 ss[i] = specslot.unbound
253 def datecheck(req, mtime):
254 if "If-Modified-Since" in req.ihead:
255 rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
256 if rtime >= math.floor(mtime):
257 raise resp.unmodified()
258 req.ohead["Last-Modified"] = proto.httpdate(mtime)