15
15
# ============================================================================
16
16
FuzzyRule = namedtuple ('FuzzyRule' ,
17
17
'url_prefix, regex, replace_after, filter_str, ' +
18
- 'match_type, find_all ' )
18
+ 'match_type, re_type ' )
19
19
20
20
21
21
# ============================================================================
22
22
class FuzzyMatcher (object ):
23
23
DEFAULT_FILTER = ['urlkey:{0}' ]
24
24
DEFAULT_MATCH_TYPE = 'prefix'
25
25
DEFAULT_REPLACE_AFTER = '?'
26
+ DEFAULT_RE_TYPE = 'search'
26
27
27
28
FUZZY_SKIP_PARAMS = ('alt_url' , 'reverse' , 'closest' , 'end_key' ,
28
29
'url' , 'matchType' , 'filter' )
@@ -58,16 +59,16 @@ def parse_fuzzy_rule(self, rule):
58
59
replace_after = self .DEFAULT_REPLACE_AFTER
59
60
filter_str = self .DEFAULT_FILTER
60
61
match_type = self .DEFAULT_MATCH_TYPE
61
- find_all = False
62
+ re_type = self . DEFAULT_RE_TYPE
62
63
63
64
else :
64
65
regex = self .make_regex (config .get ('match' ))
65
66
replace_after = config .get ('replace' , self .DEFAULT_REPLACE_AFTER )
66
67
filter_str = config .get ('filter' , self .DEFAULT_FILTER )
67
68
match_type = config .get ('type' , self .DEFAULT_MATCH_TYPE )
68
- find_all = config .get ('find_all ' , False )
69
+ re_type = config .get ('re_type ' , self . DEFAULT_RE_TYPE )
69
70
70
- return FuzzyRule (url_prefix , regex , replace_after , filter_str , match_type , find_all )
71
+ return FuzzyRule (url_prefix , regex , replace_after , filter_str , match_type , re_type )
71
72
72
73
def get_fuzzy_match (self , urlkey , url , params ):
73
74
filters = set ()
@@ -78,9 +79,12 @@ def get_fuzzy_match(self, urlkey, url, params):
78
79
continue
79
80
80
81
groups = None
81
- if rule .find_all :
82
+ if rule .re_type == 'findall' :
82
83
groups = rule .regex .findall (urlkey )
83
- else :
84
+ if rule .re_type == 'sub' :
85
+ matched_rule = rule
86
+ break
87
+ elif rule .re_type == 'search' :
84
88
m = rule .regex .search (urlkey )
85
89
groups = m and m .groups ()
86
90
@@ -102,7 +106,7 @@ def get_fuzzy_match(self, urlkey, url, params):
102
106
no_filters = (not filters or filters == {'urlkey:' }) and (matched_rule .replace_after == '?' )
103
107
104
108
inx = url .find (matched_rule .replace_after )
105
- if inx > 0 :
109
+ if inx > 0 and matched_rule . re_type != 'sub' :
106
110
length = inx + len (matched_rule .replace_after )
107
111
# don't include trailing '?' for default filter
108
112
if no_filters :
@@ -111,13 +115,17 @@ def get_fuzzy_match(self, urlkey, url, params):
111
115
if url [length - 1 ] == '/' :
112
116
length -= 1
113
117
url = url [:length ]
114
- elif not no_filters :
118
+ elif not no_filters and matched_rule . re_type != 'sub' :
115
119
url += matched_rule .replace_after [0 ]
116
120
117
121
if matched_rule .match_type == 'domain' :
118
122
host = urlsplit (url ).netloc
119
123
url = host .split ('.' , 1 )[1 ]
120
124
125
+ if matched_rule .re_type == 'sub' :
126
+ filters = {'urlkey:' }
127
+ url = re .sub (rule .regex , rule .replace_after , url )
128
+
121
129
fuzzy_params = {'url' : url ,
122
130
'matchType' : matched_rule .match_type ,
123
131
'filter' : filters ,
0 commit comments