Esempio n. 1
0
    def _match(self, url, environ):
        """Internal Route matcher

        Matches a URL against a route, and returns a tuple of the match
        dict and the route object if a match is successfull, otherwise
        it returns empty.

        For internal use only.

        """
        if not self._created_regs and self.controller_scan:
            self.create_regs()
        elif not self._created_regs:
            raise RoutesException("You must generate the regular expressions"
                                  " before matching.")

        if self.always_scan:
            self.create_regs()

        matchlog = []
        if self.prefix:
            if re.match(self._regprefix, url):
                url = re.sub(self._regprefix, r'\1', url)
                if not url:
                    url = '/'
            else:
                return (None, None, matchlog)

        environ = environ or self.environ
        sub_domains = self.sub_domains
        sub_domains_ignore = self.sub_domains_ignore
        domain_match = self.domain_match
        debug = self.debug

        if self._master_regexp is not None:
            # Check to see if its a valid url against the main regexp
            # Done for faster invalid URL elimination
            valid_url = re.match(self._master_regexp, url)
        else:
            # Regex is None due to OverflowError caused by too many routes.
            # This will allow larger projects to work but might increase time
            # spent invalidating URLs in the loop below.
            valid_url = True
        if not valid_url:
            return (None, None, matchlog)

        for route in self.matchlist:
            if route.static:
                if debug:
                    matchlog.append(dict(route=route, static=True))
                continue
            match = route.match(url, environ, sub_domains, sub_domains_ignore,
                                domain_match)
            if debug:
                matchlog.append(dict(route=route, regexp=bool(match)))
            if isinstance(match, dict) or match:
                return (match, route, matchlog)
        return (None, None, matchlog)
Esempio n. 2
0
    def _match(self, url):
        """Internal Route matcher
        
        Matches a URL against a route, and returns a tuple of the match
        dict and the route object if a match is successfull, otherwise
        it returns empty.
        
        For internal use only.
        
        """
        if not self._created_regs and self.controller_scan:
            self.create_regs()
        elif not self._created_regs:
            raise RoutesException("You must generate the regular expressions"
                                  " before matching.")

        if self.always_scan:
            self.create_regs()

        matchlog = []
        if self.prefix:
            if re.match(self._regprefix, url):
                url = re.sub(self._regprefix, r'\1', url)
                if not url:
                    url = '/'
            else:
                return (None, None, matchlog)
        environ = self.environ
        sub_domains = self.sub_domains
        sub_domains_ignore = self.sub_domains_ignore
        domain_match = self.domain_match
        debug = self.debug
        for route in self.matchlist:
            if route.static:
                if debug:
                    matchlog.append(dict(route=route, static=True))
                continue
            match = route.match(url, environ, sub_domains, sub_domains_ignore,
                                domain_match)
            if debug:
                matchlog.append(dict(route=route, regexp=bool(match)))
            if isinstance(match, dict) or match:
                return (match, route, matchlog)
        return (None, None, matchlog)
Esempio n. 3
0
    def match(self, url):
        """Match a URL against against one of the routes contained.
        
        Will return None if no valid match is found.
        
        .. code-block:: python
            
            resultdict = m.match('/joe/sixpack')
        
        """
        if not url:
            raise RoutesException('No URL provided, the minimum URL necessary'
                                  ' to match is "/".')

        result = self._match(url)
        if self.debug:
            return result[0], result[1], result[2]
        if isinstance(result[0], dict) or result[0]:
            return result[0]
        return None
Esempio n. 4
0
 def routematch(self, url=None, environ=None):
     """Match a URL against against one of the routes contained.
     
     Will return None if no valid match is found, otherwise a
     result dict and a route object is returned.
     
     .. code-block:: python
     
         resultdict, route_obj = m.match('/joe/sixpack')
     
     """
     if not url and not environ:
         raise RoutesException('URL or environ must be provided')
     
     if not url:
         url = environ['PATH_INFO']
     result = self._match(url, environ)
     if self.debug:
         return result[0], result[1], result[2]
     if isinstance(result[0], dict) or result[0]:
         return result[0], result[1]
     return None