Skip to content

Commit ae284fb

Browse files
committed
fix bug of matching
1 parent 88d1f98 commit ae284fb

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

src/os_urlpattern/cmdline.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
IrregularURLException)
1616
from .formatter import FORMATTERS
1717
from .pattern_maker import PatternMaker
18-
from .pattern_matcher import PatternMatcher
18+
from .pattern_matcher import PatternMatcher, most_matched
1919
from .utils import LogSpeedAdapter, load_obj, pretty_counter
2020

2121

@@ -156,6 +156,12 @@ def add_argument(self, parser):
156156
action='store',
157157
dest='pattern_file')
158158

159+
parser.add_argument('-a', '--all_matched',
160+
help='all matched patterns',
161+
default=False,
162+
action='store_true',
163+
dest='all_matched')
164+
159165
def _load(self, pattern_matcher, args):
160166
stats = Counter()
161167
io_input = args.pattern_file[0]
@@ -178,11 +184,13 @@ def _load(self, pattern_matcher, args):
178184
pattern_matcher.preprocess()
179185
self._logger.debug('[PREPROCESS] Finished')
180186

181-
def _match_result(self, pattern_matcher, raw_url):
187+
def _match_result(self, pattern_matcher, raw_url, args):
182188
result = None
183189
try:
184190
url = raw_url.decode(DEFAULT_ENCODING)
185191
result = pattern_matcher.match(url)
192+
if not args.all_matched:
193+
result = most_matched(result)
186194
result = ", ".join([r.info['ptn'] for r in result])
187195
except (InvalidPatternException,
188196
IrregularURLException,
@@ -201,7 +209,7 @@ def _match(self, pattern_matcher, args):
201209
for line in args.file[0]:
202210
speed_logger.debug('[MATCHING]')
203211
line = line.strip()
204-
result = self._match_result(pattern_matcher, line)
212+
result = self._match_result(pattern_matcher, line, args)
205213
if not result:
206214
result = b'N'
207215
binary_stdout.write(result)

src/os_urlpattern/pattern_matcher.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6161
class PiecePatternViewMatcher(_ViewMatcher):
@@ -131,6 +131,7 @@ def match(self, parsed_piece):
131131

132132

133133
class 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

Comments
 (0)