Commit | Line | Data |
---|---|---|
1864be32 | 1 | import inspect, math |
2a5a8ce7 | 2 | from . import req, dispatch, session, form, resp, proto |
b409a338 FT |
3 | |
4 | def wsgiwrap(callable): | |
5 | def wrapper(env, startreq): | |
7450e2fc | 6 | return dispatch.handleenv(env, startreq, callable) |
0f18b774 | 7 | wrapper.__wrapped__ = callable |
b409a338 | 8 | return wrapper |
d9979128 | 9 | |
bbdebbab | 10 | def formparams(callable): |
f639ac81 | 11 | spec = inspect.getargspec(callable) |
bbdebbab FT |
12 | def wrapper(req): |
13 | data = form.formdata(req) | |
bbdebbab FT |
14 | args = dict(data.items()) |
15 | args["req"] = req | |
16 | if not spec.keywords: | |
17 | for arg in list(args): | |
18 | if arg not in spec.args: | |
19 | del args[arg] | |
babf3e21 | 20 | for i in range(len(spec.args) - (len(spec.defaults) if spec.defaults else 0)): |
5a071401 FT |
21 | if spec.args[i] not in args: |
22 | raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied.")) | |
bbdebbab | 23 | return callable(**args) |
0f18b774 | 24 | wrapper.__wrapped__ = callable |
bbdebbab FT |
25 | return wrapper |
26 | ||
612eb9f5 FT |
27 | class funplex(object): |
28 | def __init__(self, *funs, **nfuns): | |
29 | self.dir = {} | |
30 | self.dir.update(((self.unwrap(fun).__name__, fun) for fun in funs)) | |
31 | self.dir.update(nfuns) | |
32 | ||
33 | @staticmethod | |
24e514f0 FT |
34 | def unwrap(fun): |
35 | while hasattr(fun, "__wrapped__"): | |
36 | fun = fun.__wrapped__ | |
37 | return fun | |
612eb9f5 FT |
38 | |
39 | def __call__(self, req): | |
525d7938 FT |
40 | if req.pathinfo == "": |
41 | raise resp.redirect(req.uriname + "/") | |
42 | if req.pathinfo[:1] != "/": | |
43 | raise resp.notfound() | |
44 | p = req.pathinfo[1:] | |
45 | if p == "": | |
46 | p = "__index__" | |
47 | bi = 1 | |
48 | else: | |
49 | p = p.partition("/")[0] | |
50 | bi = len(p) + 1 | |
612eb9f5 FT |
51 | if p in self.dir: |
52 | return self.dir[p](req.shift(bi)) | |
525d7938 | 53 | raise resp.notfound() |
612eb9f5 FT |
54 | |
55 | def add(self, fun): | |
56 | self.dir[self.unwrap(fun).__name__] = fun | |
57 | return fun | |
58 | ||
59 | def name(self, name): | |
60 | def dec(fun): | |
61 | self.dir[name] = fun | |
62 | return fun | |
63 | return dec | |
525d7938 | 64 | |
9bc70dab | 65 | def persession(data=None): |
d9979128 FT |
66 | def dec(callable): |
67 | def wrapper(req): | |
68 | sess = session.get(req) | |
69 | if callable not in sess: | |
70 | if data is None: | |
71 | sess[callable] = callable() | |
72 | else: | |
73 | if data not in sess: | |
74 | sess[data] = data() | |
75 | sess[callable] = callable(data) | |
76 | return sess[callable].handle(req) | |
0f18b774 | 77 | wrapper.__wrapped__ = callable |
d9979128 FT |
78 | return wrapper |
79 | return dec | |
d1f70c6c | 80 | |
77dd732a FT |
81 | class preiter(object): |
82 | __slots__ = ["bk", "bki", "_next"] | |
83 | end = object() | |
84 | def __init__(self, real): | |
85 | self.bk = real | |
86 | self.bki = iter(real) | |
87 | self._next = None | |
a7a09080 | 88 | self.__next__() |
77dd732a FT |
89 | |
90 | def __iter__(self): | |
91 | return self | |
92 | ||
a7a09080 | 93 | def __next__(self): |
77dd732a FT |
94 | if self._next is self.end: |
95 | raise StopIteration() | |
96 | ret = self._next | |
97 | try: | |
98 | self._next = next(self.bki) | |
99 | except StopIteration: | |
100 | self._next = self.end | |
101 | return ret | |
102 | ||
103 | def close(self): | |
104 | if hasattr(self.bk, "close"): | |
105 | self.bk.close() | |
106 | ||
107 | def pregen(callable): | |
108 | def wrapper(*args, **kwargs): | |
109 | return preiter(callable(*args, **kwargs)) | |
0f18b774 | 110 | wrapper.__wrapped__ = callable |
77dd732a FT |
111 | return wrapper |
112 | ||
62113fc6 FT |
113 | def stringwrap(charset): |
114 | def dec(callable): | |
115 | @pregen | |
116 | def wrapper(*args, **kwargs): | |
117 | for string in callable(*args, **kwargs): | |
118 | yield string.encode(charset) | |
119 | wrapper.__wrapped__ = callable | |
120 | return wrapper | |
121 | return dec | |
122 | ||
d1f70c6c FT |
123 | class sessiondata(object): |
124 | @classmethod | |
9bc70dab | 125 | def get(cls, req, create=True): |
d1f70c6c FT |
126 | sess = cls.sessdb().get(req) |
127 | with sess.lock: | |
128 | try: | |
129 | return sess[cls] | |
130 | except KeyError: | |
8f911ff6 FT |
131 | if not create: |
132 | return None | |
5b35322c | 133 | ret = cls(req, sess) |
d1f70c6c FT |
134 | sess[cls] = ret |
135 | return ret | |
136 | ||
137 | @classmethod | |
138 | def sessdb(cls): | |
1f61bf31 | 139 | return session.default.val |
d1f70c6c | 140 | |
f13b8f5a FT |
141 | class autodirty(sessiondata): |
142 | @classmethod | |
143 | def get(cls, req): | |
d13a1a57 | 144 | ret = super().get(req) |
f13b8f5a FT |
145 | if "_is_dirty" not in ret.__dict__: |
146 | ret.__dict__["_is_dirty"] = False | |
617b21df | 147 | return ret |
f13b8f5a | 148 | |
d1f70c6c | 149 | def sessfrozen(self): |
f13b8f5a | 150 | self.__dict__["_is_dirty"] = False |
d1f70c6c FT |
151 | |
152 | def sessdirty(self): | |
f13b8f5a | 153 | return self._is_dirty |
d1f70c6c FT |
154 | |
155 | def __setattr__(self, name, value): | |
a4ad119b | 156 | super().__setattr__(name, value) |
f13b8f5a FT |
157 | if "_is_dirty" in self.__dict__: |
158 | self.__dict__["_is_dirty"] = True | |
d1f70c6c FT |
159 | |
160 | def __delattr__(self, name): | |
d13a1a57 | 161 | super().__delattr__(name, value) |
f13b8f5a FT |
162 | if "_is_dirty" in self.__dict__: |
163 | self.__dict__["_is_dirty"] = True | |
3b9bc700 FT |
164 | |
165 | class manudirty(object): | |
166 | def __init__(self, *args, **kwargs): | |
d13a1a57 | 167 | super().__init__(*args, **kwargs) |
3b9bc700 FT |
168 | self.__dirty = False |
169 | ||
170 | def sessfrozen(self): | |
171 | self.__dirty = False | |
172 | ||
173 | def sessdirty(self): | |
174 | return self.__dirty | |
175 | ||
176 | def dirty(self): | |
177 | self.__dirty = True | |
df5f7868 FT |
178 | |
179 | class specslot(object): | |
180 | __slots__ = ["nm", "idx", "dirty"] | |
181 | unbound = object() | |
182 | ||
183 | def __init__(self, nm, idx, dirty): | |
184 | self.nm = nm | |
185 | self.idx = idx | |
186 | self.dirty = dirty | |
187 | ||
188 | @staticmethod | |
189 | def slist(ins): | |
190 | # Avoid calling __getattribute__ | |
191 | return specdirty.__sslots__.__get__(ins, type(ins)) | |
192 | ||
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) | |
197 | return val | |
198 | ||
199 | def __set__(self, ins, val): | |
200 | self.slist(ins)[self.idx] = val | |
201 | if self.dirty: | |
202 | ins.dirty() | |
203 | ||
204 | def __delete__(self, ins): | |
205 | self.slist(ins)[self.idx] = specslot.unbound | |
206 | ins.dirty() | |
207 | ||
208 | class specclass(type): | |
209 | def __init__(self, name, bases, tdict): | |
f605aaf2 | 210 | super().__init__(name, bases, tdict) |
df5f7868 FT |
211 | sslots = set() |
212 | dslots = set() | |
213 | for cls in self.__mro__: | |
214 | css = cls.__dict__.get("__saveslots__", ()) | |
215 | sslots.update(css) | |
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)) | |
221 | ||
f605aaf2 | 222 | class specdirty(sessiondata, metaclass=specclass): |
df5f7868 FT |
223 | __slots__ = ["session", "__sslots__", "_is_dirty"] |
224 | ||
225 | def __specinit__(self): | |
226 | pass | |
227 | ||
228 | @staticmethod | |
229 | def __new__(cls, req, sess): | |
f605aaf2 | 230 | self = super().__new__(cls) |
df5f7868 FT |
231 | self.session = sess |
232 | self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__) | |
233 | self.__specinit__() | |
234 | self._is_dirty = False | |
235 | return self | |
236 | ||
237 | def __getnewargs__(self): | |
238 | return (None, self.session) | |
239 | ||
240 | def dirty(self): | |
241 | self._is_dirty = True | |
242 | ||
243 | def sessfrozen(self): | |
244 | self._is_dirty = False | |
245 | ||
246 | def sessdirty(self): | |
247 | return self._is_dirty | |
248 | ||
249 | def __getstate__(self): | |
250 | ret = {} | |
251 | for nm, val in zip(type(self).__sslots_a__, specslot.slist(self)): | |
252 | if val is specslot.unbound: | |
253 | ret[nm] = False, None | |
254 | else: | |
255 | ret[nm] = True, val | |
256 | return ret | |
257 | ||
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)) | |
df5f7868 FT |
262 | if not bound: |
263 | ss[i] = specslot.unbound | |
264 | else: | |
265 | ss[i] = val | |
1864be32 FT |
266 | |
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) |