def url(route=None, controller=None, action=None, name=None): route = Route(name, route) route.makeregexp('') regexp = re.sub(r'(?<!\\)\\', '', route.regexp) return URLSpec(regexp, controller, dict(action=action), name=name)
class Route(object): __attributes__ = ('name', 'domain', 'path', 'target') def __init__(self, **kwargs): for attr in self.__attributes__: setattr(self, attr, None) self._update(kwargs) self._domain_route, self._path_route = None, None def _update(self, kwargs): for attr in self.__attributes__: if attr in kwargs: setattr(self, attr, kwargs[attr]) def match(self, request): d = None if self.domain: if self._domain_route is None: self._domain_route = RoutesRoute(None, self.domain, _explicit=True) self._domain_route.makeregexp([]) m = self._domain_route.match(request.host) if m is not False: d = m.copy() if self.path: if self._path_route is None: self._path_route = RoutesRoute(None, self.path, _explicit=True) self._path_route.makeregexp([]) m = self._path_route.match(request.path) if m is not False: if d is None: d = {} d.update(m) return d def to_json(self): """Return a python dict.""" return dict((attr, getattr(self, attr)) for attr in self.__attributes__)