Dolda2000 GitWeb
/
wrw.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added encoding tools for parameterized MIME headers.
[wrw.git]
/
wrw
/
util.py
diff --git
a/wrw/util.py
b/wrw/util.py
index
759c295
..
7209ae4
100644
(file)
--- a/
wrw/util.py
+++ b/
wrw/util.py
@@
-1,5
+1,5
@@
import inspect, math
import inspect, math
-import req, dispatch, session, form, resp, proto
+
from .
import req, dispatch, session, form, resp, proto
def wsgiwrap(callable):
def wrapper(env, startreq):
def wsgiwrap(callable):
def wrapper(env, startreq):
@@
-8,16
+8,19
@@
def wsgiwrap(callable):
return wrapper
def formparams(callable):
return wrapper
def formparams(callable):
+ spec = inspect.getargspec(callable)
def wrapper(req):
def wrapper(req):
- data = form.formdata(req)
- spec = inspect.getargspec(callable)
+ try:
+ data = form.formdata(req)
+ except IOError:
+ raise resp.httperror(400, "Invalid request", "Form data was incomplete")
args = dict(data.items())
args["req"] = req
if not spec.keywords:
for arg in list(args):
if arg not in spec.args:
del args[arg]
args = dict(data.items())
args["req"] = req
if not spec.keywords:
for arg in list(args):
if arg not in spec.args:
del args[arg]
- for i in
xrange(len(spec.args) - len(spec.defaults
)):
+ for i in
range(len(spec.args) - (len(spec.defaults) if spec.defaults else 0
)):
if spec.args[i] not in args:
raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
return callable(**args)
if spec.args[i] not in args:
raise resp.httperror(400, "Missing parameter", ("The query parameter `", resp.h.code(spec.args[i]), "' is required but not supplied."))
return callable(**args)
@@
-38,6
+41,8
@@
class funplex(object):
def __call__(self, req):
if req.pathinfo == "":
def __call__(self, req):
if req.pathinfo == "":
+ if "__root__" in self.dir:
+ return self.dir["__root__"](req)
raise resp.redirect(req.uriname + "/")
if req.pathinfo[:1] != "/":
raise resp.notfound()
raise resp.redirect(req.uriname + "/")
if req.pathinfo[:1] != "/":
raise resp.notfound()
@@
-49,7
+54,9
@@
class funplex(object):
p = p.partition("/")[0]
bi = len(p) + 1
if p in self.dir:
p = p.partition("/")[0]
bi = len(p) + 1
if p in self.dir:
- return self.dir[p](req.shift(bi))
+ sreq = req.shift(bi)
+ sreq.selfpath = req.pathinfo[1:]
+ return self.dir[p](sreq)
raise resp.notfound()
def add(self, fun):
raise resp.notfound()
def add(self, fun):
@@
-62,7
+69,7
@@
class funplex(object):
return fun
return dec
return fun
return dec
-def persession(data
=
None):
+def persession(data
=
None):
def dec(callable):
def wrapper(req):
sess = session.get(req)
def dec(callable):
def wrapper(req):
sess = session.get(req)
@@
-85,12
+92,12
@@
class preiter(object):
self.bk = real
self.bki = iter(real)
self._next = None
self.bk = real
self.bki = iter(real)
self._next = None
- self.
next
()
+ self.
__next__
()
def __iter__(self):
return self
def __iter__(self):
return self
- def
next
(self):
+ def
__next__
(self):
if self._next is self.end:
raise StopIteration()
ret = self._next
if self._next is self.end:
raise StopIteration()
ret = self._next
@@
-110,9
+117,19
@@
def pregen(callable):
wrapper.__wrapped__ = callable
return wrapper
wrapper.__wrapped__ = callable
return wrapper
+def stringwrap(charset):
+ def dec(callable):
+ @pregen
+ def wrapper(*args, **kwargs):
+ for string in callable(*args, **kwargs):
+ yield string.encode(charset)
+ wrapper.__wrapped__ = callable
+ return wrapper
+ return dec
+
class sessiondata(object):
@classmethod
class sessiondata(object):
@classmethod
- def get(cls, req, create
=
True):
+ def get(cls, req, create
=
True):
sess = cls.sessdb().get(req)
with sess.lock:
try:
sess = cls.sessdb().get(req)
with sess.lock:
try:
@@
-131,7
+148,7
@@
class sessiondata(object):
class autodirty(sessiondata):
@classmethod
def get(cls, req):
class autodirty(sessiondata):
@classmethod
def get(cls, req):
- ret = super(
autodirty, cls
).get(req)
+ ret = super().get(req)
if "_is_dirty" not in ret.__dict__:
ret.__dict__["_is_dirty"] = False
return ret
if "_is_dirty" not in ret.__dict__:
ret.__dict__["_is_dirty"] = False
return ret
@@
-143,18
+160,18
@@
class autodirty(sessiondata):
return self._is_dirty
def __setattr__(self, name, value):
return self._is_dirty
def __setattr__(self, name, value):
- super(
autodirty, self
).__setattr__(name, value)
+ super().__setattr__(name, value)
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
def __delattr__(self, name):
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
def __delattr__(self, name):
- super(
autodirty, self
).__delattr__(name, value)
+ super().__delattr__(name, value)
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
class manudirty(object):
def __init__(self, *args, **kwargs):
if "_is_dirty" in self.__dict__:
self.__dict__["_is_dirty"] = True
class manudirty(object):
def __init__(self, *args, **kwargs):
- super(
manudirty, self
).__init__(*args, **kwargs)
+ super().__init__(*args, **kwargs)
self.__dirty = False
def sessfrozen(self):
self.__dirty = False
def sessfrozen(self):
@@
-197,7
+214,7
@@
class specslot(object):
class specclass(type):
def __init__(self, name, bases, tdict):
class specclass(type):
def __init__(self, name, bases, tdict):
- super(
specclass, self
).__init__(name, bases, tdict)
+ super().__init__(name, bases, tdict)
sslots = set()
dslots = set()
for cls in self.__mro__:
sslots = set()
dslots = set()
for cls in self.__mro__:
@@
-209,8
+226,7
@@
class specclass(type):
for i, slot in enumerate(self.__sslots_a__):
setattr(self, slot, specslot(slot, i, slot in dslots))
for i, slot in enumerate(self.__sslots_a__):
setattr(self, slot, specslot(slot, i, slot in dslots))
-class specdirty(sessiondata):
- __metaclass__ = specclass
+class specdirty(sessiondata, metaclass=specclass):
__slots__ = ["session", "__sslots__", "_is_dirty"]
def __specinit__(self):
__slots__ = ["session", "__sslots__", "_is_dirty"]
def __specinit__(self):
@@
-218,7
+234,7
@@
class specdirty(sessiondata):
@staticmethod
def __new__(cls, req, sess):
@staticmethod
def __new__(cls, req, sess):
- self = super(
specdirty, cls
).__new__(cls)
+ self = super().__new__(cls)
self.session = sess
self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
self.__specinit__()
self.session = sess
self.__sslots__ = [specslot.unbound] * len(cls.__sslots_a__)
self.__specinit__()
@@
-258,6
+274,6
@@
class specdirty(sessiondata):
def datecheck(req, mtime):
if "If-Modified-Since" in req.ihead:
rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
def datecheck(req, mtime):
if "If-Modified-Since" in req.ihead:
rtime = proto.phttpdate(req.ihead["If-Modified-Since"])
- if rtime >= math.floor(mtime):
+ if rtime
is not None and rtime
>= math.floor(mtime):
raise resp.unmodified()
req.ohead["Last-Modified"] = proto.httpdate(mtime)
raise resp.unmodified()
req.ohead["Last-Modified"] = proto.httpdate(mtime)