def add(self, rule, method, target, name=None):
if rule in self.rules:<EOL><INDENT>self.rules[rule][method] = target<EOL>if name: self.builder[name] = self.builder[rule]<EOL>return<EOL><DEDENT>target = self.rules[rule] = {method: target}<EOL>anons = <NUM_LIT:0>      <EOL>pattern = '<STR_LIT>'   <EOL>filters = []   <EOL>builder = []   <EOL>is_static = True<EOL>for key, mode, conf in self.parse_rule(rule):<EOL><INDENT>if mode:<EOL><INDENT>is_static = False<EOL>mask, in_filter, out_filter = self.filters[mode](conf)<EOL>if key:<EOL><INDENT>pattern += '<STR_LIT>' % (key, mask)<EOL><DEDENT>else:<EOL><INDENT>pattern += '<STR_LIT>' % mask<EOL>key = '<STR_LIT>' % anons; anons += <NUM_LIT:1><EOL><DEDENT>if in_filter: filters.append((key, in_filter))<EOL>builder.append((key, out_filter or str))<EOL><DEDENT>elif key:<EOL><INDENT>pattern += re.escape(key)<EOL>builder.append((None, key))<EOL><DEDENT><DEDENT>self.builder[rule] = builder<EOL>if name: self.builder[name] = builder<EOL>if is_static and not self.strict_order:<EOL><INDENT>self.static[self.build(rule)] = target<EOL>return<EOL><DEDENT>def fpat_sub(m):<EOL><INDENT>return m.group(<NUM_LIT:0>) if len(m.group(<NUM_LIT:1>)) % <NUM_LIT:2> else m.group(<NUM_LIT:1>) + '<STR_LIT>'<EOL><DEDENT>flat_pattern = re.sub(r'<STR_LIT>', fpat_sub, pattern)<EOL>try:<EOL><INDENT>re_match = re.compile('<STR_LIT>' % pattern).match<EOL><DEDENT>except re.error as e:<EOL><INDENT>raise RouteSyntaxError("<STR_LIT>" % (rule, e))<EOL><DEDENT>def match(path):<EOL><INDENT>"""<STR_LIT>"""<EOL>url_args = re_match(path).groupdict()<EOL>for name, wildcard_filter in filters:<EOL><INDENT>try:<EOL><INDENT>url_args[name] = wildcard_filter(url_args[name])<EOL><DEDENT>except ValueError:<EOL><INDENT>raise HTTPError(<NUM_LIT>, '<STR_LIT>')<EOL><DEDENT><DEDENT>return url_args<EOL><DEDENT>try:<EOL><INDENT>combined = '<STR_LIT>' % (self.dynamic[-<NUM_LIT:1>][<NUM_LIT:0>].pattern, flat_pattern)<EOL>self.dynamic[-<NUM_LIT:1>] = (re.compile(combined), self.dynamic[-<NUM_LIT:1>][<NUM_LIT:1>])<EOL>self.dynamic[-<NUM_LIT:1>][<NUM_LIT:1>].append((match, target))<EOL><DEDENT>except (AssertionError, IndexError) as e: <EOL><INDENT>self.dynamic.append((re.compile('<STR_LIT>' % flat_pattern),<EOL>[(match, target)]))<EOL><DEDENT>return match<EOL>
Add a new route or replace the target for an existing route.