Skip to content

Commit

Permalink
When a conflict_solver returns __default__ call the opposite one befo…
Browse files Browse the repository at this point in the history
…re calling default one.
  • Loading branch information
Toilal committed Oct 24, 2015
1 parent 94dbc33 commit 4d3e1fa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
29 changes: 16 additions & 13 deletions rebulk/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,24 @@ def conflict_prefer_longer(matches):
if conflicting_matches:
# keep the match only if it's the longest
for conflicting_match in filter(lambda match: not match.private, conflicting_matches):
conflict_solver = default_conflict_solver
reverse = False
conflict_solvers = [(default_conflict_solver, False)]

if match.conflict_solver:
conflict_solver = match.conflict_solver
elif conflicting_match.conflict_solver:
conflict_solver = conflicting_match.conflict_solver
reverse = True
if reverse:
to_remove = conflict_solver(conflicting_match, match)
else:
to_remove = conflict_solver(match, conflicting_match)
if to_remove == DEFAULT:
to_remove = default_conflict_solver(match, conflicting_match)
if to_remove and to_remove not in to_remove_matches:
to_remove_matches.add(to_remove)
conflict_solvers.append((match.conflict_solver, False))
if conflicting_match.conflict_solver:
conflict_solvers.append((conflicting_match.conflict_solver, True))

for conflict_solver, reverse in reversed(conflict_solvers):
if reverse:
to_remove = conflict_solver(conflicting_match, match)
else:
to_remove = conflict_solver(match, conflicting_match)
if to_remove == DEFAULT:
continue
if to_remove and to_remove not in to_remove_matches:
to_remove_matches.add(to_remove)
break

for match in to_remove_matches:
matches.remove(match)
Expand Down
10 changes: 10 additions & 0 deletions rebulk/test/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def test_conflict_solver():
assert len(processed_matches) == 1
assert processed_matches[0].value == "2345678"

re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: '__default__')
re2 = StringPattern("34567", conflict_solver=lambda match, conflicting: conflicting)

matches = Matches(re1.matches(input_string))
matches.extend(re2.matches(input_string))

processed_matches = conflict_prefer_longer(matches)
assert len(processed_matches) == 1
assert processed_matches[0].value == "34567"

re1 = StringPattern("2345678", conflict_solver=lambda match, conflicting: match)
re2 = StringPattern("34567")

Expand Down

0 comments on commit 4d3e1fa

Please sign in to comment.