Merge branch 'master' into python3
authorFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Apr 2013 03:56:52 +0000 (05:56 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Tue, 23 Apr 2013 03:56:52 +0000 (05:56 +0200)
Conflicts:
wrw/__init__.py

1  2 
wrw/__init__.py
wrw/util.py

diff --combined wrw/__init__.py
@@@ -1,7 -1,7 +1,7 @@@
  __all__ = ["wsgiwrap", "restart", "cookie", "formdata"]
  
 -import proto
 -from util import wsgiwrap, formparams, funplex, persession, sessiondata, autodirty, manudirty, specdirty
 -from dispatch import restart
 -import cookie
 -from form import formdata
 +from . import proto
- from .util import wsgiwrap, stringwrap, formparams, persession, sessiondata, autodirty, manudirty, specdirty
++from .util import wsgiwrap, stringwrap, formparams, funplex, persession, sessiondata, autodirty, manudirty, specdirty
 +from .dispatch import restart
 +from . import cookie
 +from .form import formdata
diff --combined wrw/util.py
@@@ -1,20 -1,11 +1,20 @@@
  import inspect
 -import req, dispatch, session, form, resp
 +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)
              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.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
  
+ def funplex(*funs, **nfuns):
+     dir = {}
+     dir.update(((fun.__name__, fun) for fun in funs))
+     dir.update(nfuns)
+     def handler(req):
+         if req.pathinfo == "":
+             raise resp.redirect(req.uriname + "/")
+         if req.pathinfo[:1] != "/":
+             raise resp.notfound()
+         p = req.pathinfo[1:]
+         if p == "":
+             p = "__index__"
+             bi = 1
+         else:
+             p = p.partition("/")[0]
+             bi = len(p) + 1
+         if p in dir:
+             return dir[p](req.shift(bi))
+         raise resp.notfound()
+     return handler
  def persession(data = None):
      def dec(callable):
          def wrapper(req):
@@@ -53,12 -65,12 +74,12 @@@ class preiter(object)
          self.bk = real
          self.bki = iter(real)
          self._next = None
 -        self.next()
 +        self.__next__()
  
      def __iter__(self):
          return self
  
 -    def next(self):
 +    def __next__(self):
          if self._next is self.end:
              raise StopIteration()
          ret = self._next
@@@ -98,7 -110,7 +119,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
          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):
@@@ -164,7 -176,7 +185,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__:
          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):
  
      @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__()