(self, rule, method, target, name=None)
Add a new rule or replace the target for an existing rule.
Add a new rule or replace the target for an existing rule.
Add a new rule or replace the target for an existing rule .
def add(self, rule, method, target, name=None):
        ''' Add a new rule or replace the target for an existing rule. '''
        anons     = 0    # Number of anonymous wildcards found
        keys      = []   # Names of keys
        pattern   = ''   # Regular expression pattern with named groups
        filters   = []   # Lists of wildcard input filters
        builder   = []   # Data structure for the URL builder
        is_static = True

        for key, mode, conf in self._itertokens(rule):
            if mode:
                is_static = False
                if mode == 'default': mode = self.default_filter
                mask, in_filter, out_filter = self.filters[mode](conf)
                if not key:
                    pattern += '(?:%s)' % mask
                    key = 'anon%d' % anons
                    anons += 1
                else:
                    pattern += '(?P<%s>%s)' % (key, mask)
                    keys.append(key)
                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.setdefault(method, {})
            self.static[method][self.build(rule)] = (target, None)
            return

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

        if filters:
            def getargs(path):
                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
        elif re_pattern.groupindex:
            def getargs(path):
                return re_match(path).groupdict()
        else:
            getargs = None

        flatpat = _re_flatten(pattern)
        whole_rule = (rule, flatpat, target, getargs)

        if (flatpat, method) in self._groups:
            if DEBUG:
                msg = 'Route <%s %s> overwrites a previously defined route'
                warnings.warn(msg % (method, rule), RuntimeWarning)
            self.dyna_routes[method][self._groups[flatpat, method]] = whole_rule
        else:
            self.dyna_routes.setdefault(method, []).append(whole_rule)
            self._groups[flatpat, method] = len(self.dyna_routes[method]) - 1

        self._compile(method)
def add ( self , rule , method , target , name = None ) : anons = 0 # Number of anonymous wildcards found keys = [ ] # Names of keys pattern = '' # Regular expression pattern with named groups filters = [ ] # Lists of wildcard input filters builder = [ ] # Data structure for the URL builder is_static = True for key , mode , conf in self . _itertokens ( rule ) : if mode : is_static = False if mode == 'default' : mode = self . default_filter mask , in_filter , out_filter = self . filters [ mode ] ( conf ) if not key : pattern += '(?:%s)' % mask key = 'anon%d' % anons anons += 1 else : pattern += '(?P<%s>%s)' % ( key , mask ) keys . append ( key ) 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 . setdefault ( method , { } ) self . static [ method ] [ self . build ( rule ) ] = ( target , None ) return try : re_pattern = re . compile ( '^(%s)$' % pattern ) re_match = re_pattern . match except re . error : raise RouteSyntaxError ( "Could not add Route: %s (%s)" % ( rule , _e ( ) ) ) if filters : def getargs ( path ) : 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 elif re_pattern . groupindex : def getargs ( path ) : return re_match ( path ) . groupdict ( ) else : getargs = None flatpat = _re_flatten ( pattern ) whole_rule = ( rule , flatpat , target , getargs ) if ( flatpat , method ) in self . _groups : if DEBUG : msg = 'Route <%s %s> overwrites a previously defined route' warnings . warn ( msg % ( method , rule ) , RuntimeWarning ) self . dyna_routes [ method ] [ self . _groups [ flatpat , method ] ] = whole_rule else : self . dyna_routes . setdefault ( method , [ ] ) . append ( whole_rule ) self . _groups [ flatpat , method ] = len ( self . dyna_routes [ method ] ) - 1 self . _compile ( method )