X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=wrw%2Futil.py;h=eaa6a6280ffdcf90d79d086e72efb416d1213b49;hb=fd7af0a278cf6a58b8ea371e3e84c99c1cb77e0c;hp=6368b18f198eb65fe3cc024fcd4747a40b3aa78e;hpb=7450e2fcc553f66844b34d5c062a965b8fca28c5;p=wrw.git diff --git a/wrw/util.py b/wrw/util.py index 6368b18..eaa6a62 100644 --- a/wrw/util.py +++ b/wrw/util.py @@ -1,11 +1,20 @@ import inspect -import req, dispatch, session, form +from . import req, dispatch, session, form, resp def wsgiwrap(callable): def wrapper(env, startreq): return dispatch.handleenv(env, startreq, callable) return wrapper +def stringwrap(charset): + def dec(callable): + def wrapper(*args, **kwargs): + bk = callable(*args, **kwargs) + for string in bk: + yield string.encode(charset) + return wrapper + return dec + def formparams(callable): def wrapper(req): data = form.formdata(req) @@ -16,6 +25,9 @@ def formparams(callable): for arg in list(args): if arg not in spec.args: del args[arg] + for i in range(len(spec.args) - len(spec.defaults)): + 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) return wrapper @@ -34,6 +46,37 @@ def persession(data = None): return wrapper return dec +class preiter(object): + __slots__ = ["bk", "bki", "_next"] + end = object() + def __init__(self, real): + self.bk = real + self.bki = iter(real) + self._next = None + self.__next__() + + def __iter__(self): + return self + + def __next__(self): + if self._next is self.end: + raise StopIteration() + ret = self._next + try: + self._next = next(self.bki) + except StopIteration: + self._next = self.end + return ret + + def close(self): + if hasattr(self.bk, "close"): + self.bk.close() + +def pregen(callable): + def wrapper(*args, **kwargs): + return preiter(callable(*args, **kwargs)) + return wrapper + class sessiondata(object): @classmethod def get(cls, req, create = True): @@ -55,7 +98,7 @@ class sessiondata(object): 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 @@ -67,18 +110,18 @@ class autodirty(sessiondata): 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): - 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): - super(manudirty, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.__dirty = False def sessfrozen(self): @@ -121,7 +164,7 @@ class specslot(object): 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__: @@ -133,8 +176,7 @@ class specclass(type): 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): @@ -142,7 +184,7 @@ class specdirty(sessiondata): @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__()