(self, rule, method, target, name=None)
return match
Add a new route or replace the target for an existing route.
Add a new route or replace the target for an existing route.
Add a new route or replace the target for an existing route .
def add(self, rule, method, target, name=None):
        ''' Add a new route or replace the target for an existing route. '''
        if rule in self.rules:
            self.rules[rule][method] = target
            if name: self.builder[name] = self.builder[rule]
            return

        target = self.rules[rule] = {method: target}

        # Build pattern and other structures for dynamic routes
        anons = 0      # Number of anonymous wildcards
        pattern = ''   # Regular expression  pattern
        filters = []   # Lists of wildcard input filters
        builder = []   # Data structure for the URL builder
        is_static = True
        for key, mode, conf in self.parse_rule(rule):
            if mode:
                is_static = False
                mask, in_filter, out_filter = self.filters[mode](conf)
                if key:
                    pattern += '(?P<%s>%s)' % (key, mask)
                else:
                    pattern += '(?:%s)' % mask
                    key = 'anon%d' % anons; anons += 1
                if in_filter: filters.append((key, in_filter))
                builder.append((key, out_filter or str))
            elif key:
                pattern += re.escape(key)
                builder.append((None, key))
        self.builder[rule] = builder
        if name: self.builder[name] = builder

        if is_static and not self.strict_order:
            self.static[self.build(rule)] = target
            return

        def fpat_sub(m):
            return m.group(0) if len(m.group(1)) % 2 else m.group(1) + '(?:'
        flat_pattern = re.sub(r'(\\*)(\(\?P<[^>]*>|\((?!\?))', fpat_sub, pattern)

        try:
            re_match = re.compile('^(%s)$' % pattern).match
        except re.error:
            raise RouteSyntaxError("Could not add Route: %s (%s)" % (rule, _e()))

        def match(path):
            """ Return an url-argument dictionary. """
            url_args = re_match(path).groupdict()
            for name, wildcard_filter in filters:
                try:
                    url_args[name] = wildcard_filter(url_args[name])
                except ValueError:
                    raise HTTPError(400, 'Path has wrong format.')
            return url_args

        try:
            combined = '%s|(^%s$)' % (self.dynamic[-1][0].pattern, flat_pattern)
            self.dynamic[-1] = (re.compile(combined), self.dynamic[-1][1])
            self.dynamic[-1][1].append((match, target))
        except (AssertionError, IndexError): # AssertionError: Too many groups
            self.dynamic.append((re.compile('(^%s$)' % flat_pattern),
                                [(match, target)]))
        return match
def add ( self , rule , method , target , name = None ) : if rule in self . rules : self . rules [ rule ] [ method ] = target if name : self . builder [ name ] = self . builder [ rule ] return target = self . rules [ rule ] = { method : target } # Build pattern and other structures for dynamic routes anons = 0 # Number of anonymous wildcards pattern = '' # Regular expression  pattern filters = [ ] # Lists of wildcard input filters builder = [ ] # Data structure for the URL builder is_static = True for key , mode , conf in self . parse_rule ( rule ) : if mode : is_static = False mask , in_filter , out_filter = self . filters [ mode ] ( conf ) if key : pattern += '(?P<%s>%s)' % ( key , mask ) else : pattern += '(?:%s)' % mask key = 'anon%d' % anons anons += 1 if in_filter : filters . append ( ( key , in_filter ) ) builder . append ( ( key , out_filter or str ) ) elif key : pattern += re . escape ( key ) builder . append ( ( None , key ) ) self . builder [ rule ] = builder if name : self . builder [ name ] = builder if is_static and not self . strict_order : self . static [ self . build ( rule ) ] = target return def fpat_sub ( m ) : return m . group ( 0 ) if len ( m . group ( 1 ) ) % 2 else m . group ( 1 ) + '(?:' flat_pattern = re . sub ( r'(\\*)(\(\?P<[^>]*>|\((?!\?))' , fpat_sub , pattern ) try : re_match = re . compile ( '^(%s)$' % pattern ) . match except re . error : raise RouteSyntaxError ( "Could not add Route: %s (%s)" % ( rule , _e ( ) ) ) def match ( path ) : """ Return an url-argument dictionary. """ url_args = re_match ( path ) . groupdict ( ) for name , wildcard_filter in filters : try : url_args [ name ] = wildcard_filter ( url_args [ name ] ) except ValueError : raise HTTPError ( 400 , 'Path has wrong format.' ) return url_args try : combined = '%s|(^%s$)' % ( self . dynamic [ - 1 ] [ 0 ] . pattern , flat_pattern ) self . dynamic [ - 1 ] = ( re . compile ( combined ) , self . dynamic [ - 1 ] [ 1 ] ) self . dynamic [ - 1 ] [ 1 ] . append ( ( match , target ) ) except ( AssertionError , IndexError ) : # AssertionError: Too many groups self . dynamic . append ( ( re . compile ( '(^%s$)' % flat_pattern ) , [ ( match , target ) ] ) ) return match