def _compile_pattern( self, pattern ): """`pattern` is URL routing pattern. This method compiles the pattern in three different ways and returns them as a tuple of (regex, tmpl, redict) `regex`, A regular expression string that can be used to match incoming request-url to resolve view-callable. `tmpl`, A template formating string that can be used to generate URLs by apps. `redict`, A dictionary of variable components in path segment and optional regular expression that must match its value. This can be used for validation during URL generation. """ regex, tmpl, redict = r'^', '', {} segs = list( filter( None, pattern.split( URLSEP ))) while segs : regex += URLSEP tmpl += URLSEP part = segs.pop(0) if not part : continue if part[0] == '*' : part = URLSEP.join( [part] + segs ) prefx, name, reg, sufx = None, part[1:], r'.*', None segs = [] else : prefx, interp, sufx = re_patt.match( part ).groups() if interp : try : name, reg = interp[1:-1].split(',', 1) except : name, reg = interp[1:-1], None else : name, reg = None, None regex += prefx if prefx else r'' if name and reg and sufx : regex += r'(?P<%s>%s(?=%s))%s' % (name, reg, sufx, sufx) elif name and reg : regex += r'(?P<%s>%s)' % (name, reg) elif name and sufx : regex += r'(?P<%s>.+(?=%s))%s' % (name, sufx, sufx) elif name : regex += r'(?P<%s>.+)' % (name,) elif sufx : regex += sufx tmpl += prefx if prefx else '' tmpl += '{' + name + '}' if name else '' tmpl += sufx if sufx else '' redict[ name ] = reg regex += '$' return regex, tmpl, redict
def _compile_pattern( self, pattern ): """`pattern` is URL routing pattern. This method compiles the pattern in three different ways and returns them as a tuple of (regex, tmpl, redict) `regex`, A regular expression string that can be used to match incoming request-url to resolve view-callable. `tmpl`, A template formating string that can be used to generate URLs by apps. `redict`, A dictionary of variable components in path segment and optional regular expression that must match its value. This can be used for validation during URL generation. """ regex, tmpl, redict = r'^', '', {} segs = pattern.split( URLSEP ) while segs : seg = segs.pop(0) if not seg : continue regex, tmpl = (regex + URLSEP), (tmpl + URLSEP) prefx, _, name, reg, sufx = re_patt.match( seg ).groups() if name[0] == '*' : rempath = URLSEP.join( [seg] + segs ) prefx, _, name, reg, sufx = re_patt.match( rempath ).groups() segs = [] reg = reg and reg[1:] regex += prefx if prefx else r'' if name and reg and sufx : regex += r'(?P<%s>%s(?=%s))%s' % (name, reg, sufx, sufx) elif name and reg : regex += r'(?P<%s>%s)' % (name, reg) elif name and sufx : regex += r'(?P<%s>.+(?=%s))%s' % (name, sufx, sufx) elif name : regex += r'(?p<%s>.+)' % (name,) elif sufx : regex += sufx tmpl += prefx if prefx else '' tmpl += '{' + name + '}' if name else '' tmpl += sufx if sufx else '' redict[ name ] = reg regex += '$' return regex, tmpl, redict