@@ -55,7 +55,7 @@ def match(self, parsed_piece):
5555 return []
5656 parsed_pieces = [EMPTY_PARSED_PIECE , ]
5757 parsed_pieces .extend (view .parsed_pieces )
58- return [n .info for n in self ._nodes [view .view ].match (parsed_pieces )]
58+ return [n .info for n in self ._nodes [view .view ].match (parsed_pieces , match_all = True )]
5959
6060
6161class PiecePatternViewMatcher (_ViewMatcher ):
@@ -131,6 +131,7 @@ def match(self, parsed_piece):
131131
132132
133133class PatternMatchNode (object ):
134+ __slot__ = ()
134135
135136 def __init__ (self , pattern , info = None ):
136137 self ._pattern = pattern
@@ -161,16 +162,21 @@ def preprocess(self):
161162 def iter_children (self ):
162163 return itervalues (self ._children )
163164
164- def match (self , parsed_pieces , idx , matched_nodes ):
165+ def match (self , parsed_pieces , idx , matched_nodes , match_all = False ):
165166 parsed_piece = parsed_pieces [idx ]
166167 for matcher in itervalues (self ._view_matchers ):
167168 nodes = matcher .match (parsed_piece )
168- for node in nodes :
169- if node .leaf ():
170- matched_nodes .append (node )
169+ self ._deep_match (nodes , parsed_pieces , idx ,
170+ matched_nodes , match_all )
171+
172+ def _deep_match (self , nodes , parsed_pieces , idx , matched_nodes , match_all ):
173+ for node in nodes :
174+ if node .leaf ():
175+ matched_nodes .append (node )
176+ if not match_all :
171177 return
172- else :
173- node .match (parsed_pieces , idx + 1 , matched_nodes )
178+ else :
179+ node .match (parsed_pieces , idx + 1 , matched_nodes , match_all )
174180
175181 @property
176182 def pattern (self ):
@@ -218,9 +224,9 @@ def load_from_patterns(self, patterns, info):
218224 def root (self ):
219225 return self ._root
220226
221- def match (self , parsed_pieces ):
227+ def match (self , parsed_pieces , match_all = False ):
222228 matched_nodes = []
223- self ._root .match (parsed_pieces , 0 , matched_nodes )
229+ self ._root .match (parsed_pieces , 0 , matched_nodes , match_all )
224230 return matched_nodes
225231
226232 def preprocess (self ):
@@ -253,3 +259,7 @@ def match(self, url):
253259 if sid in self ._pattern_match_trees :
254260 return self ._pattern_match_trees [sid ].match (parsed_pieces )
255261 return []
262+
263+
264+ def most_matched (matched_nodes ):
265+ return matched_nodes [0 :1 ]
0 commit comments