@@ -69,14 +69,17 @@ def parse_resources(resources):
6969 # resource of '*', which is not actually a valid regexp.
7070 resources = [(re_fix (k ), v ) for k , v in resources .items ()]
7171
72- # Sort by regex length to provide consistency of matching and
73- # to provide a proxy for specificity of match. E.G. longer
74- # regular expressions are tried first.
75- def pattern_length (pair ):
76- maybe_regex , _ = pair
77- return len (get_regexp_pattern (maybe_regex ))
78-
79- return sorted (resources , key = pattern_length , reverse = True )
72+ # Sort patterns with static (literal) paths first, then by regex specificity
73+ def sort_key (pair ):
74+ pattern , _ = pair
75+ if isinstance (pattern , RegexObject ):
76+ return (1 , 0 , pattern .pattern .count ("/" ), - len (pattern .pattern ))
77+ elif probably_regex (pattern ):
78+ return (1 , 1 , pattern .count ("/" ), - len (pattern ))
79+ else :
80+ return (0 , 0 , pattern .count ("/" ), - len (pattern ))
81+
82+ return sorted (resources , key = sort_key )
8083
8184 elif isinstance (resources , str ):
8285 return [(re_fix (resources ), {})]
0 commit comments