Example #1
0
 def default(self, *path, **kw):
     # attempt to find matching route
     path = list(path)
     for i in xrange(len(path)):
         try:
             if isinstance(path[i], str):
                 path[i] = path[i].decode('utf-8')
                 pass
         except UnicodeDecodeError:
             pass
     pathlen = len(path)
     method = cherrypy.request.method
     e = None
     for route in self.routes:
         match = route.matchpath(path, method)
         if match is False:
             continue
         #any methods with routes attached are assumed to be expoed
         #the method itself should not have the .exposed attribute else
         # it'll be accessible at an unexpected url
         kw.update(match) # should probably create a new dict here
         try:
             if getattr(route.target, 'lock_session', None):
                 # the default wrapper only acquires a read lock
                 # honour the target's request for a write lock prior to dispatch
                 cherrypy.session.escalate_lock()
             return route.target(self, **kw)
         except TypeError, x:
             test_callable_spec(route.target, [], kw)
             raise
         except RequestRefused, e:
             pass
Example #2
0
    def __new__(m, clsname, bases, dict):
        routes = []
        for attrname, attr in dict.items():
            if type(attr) == types.FunctionType:
                if hasattr(attr, 'routes'):
                    routes.extend(attr.routes)
                    if not hasattr(attr, 'route_exposed') and hasattr(attr, 'exposed'):
                        del attr.exposed
        dict['routes'] = routes
        if routes:
            routes.sort(reverse=True) # place most specific routes first

            def default(self, *path, **kw):
                # attempt to find matching route
                path = list(path)
                for i in xrange(len(path)):
                    try:
                        if isinstance(path[i], str):
                            path[i] = path[i].decode('utf-8')
                            pass
                    except UnicodeDecodeError:
                        pass
                pathlen = len(path)
                method = cherrypy.request.method
                e = None
                for route in self.routes:
                    match = route.matchpath(path, method)
                    if match is False:
                        continue
                    #any methods with routes attached are assumed to be expoed
                    #the method itself should not have the .exposed attribute else
                    # it'll be accessible at an unexpected url
                    kw.update(match) # should probably create a new dict here
                    try:
                        if getattr(route.target, 'lock_session', None):
                            # the default wrapper only acquires a read lock
                            # honour the target's request for a write lock prior to dispatch
                            cherrypy.session.escalate_lock()
                        return route.target(self, **kw)
                    except TypeError, x:
                        test_callable_spec(route.target, [], kw)
                        raise
                    except RequestRefused, e:
                        pass

                if hasattr(default, 'original'):
                    try:
                        return default.original(*path, **kw)
                    except TypeError, x:
                        test_callable_spec(default.original, path, kw)
                        raise