Skip to content

Commit

Permalink
Fix chain patterns not properly detected in certain scenarios
Browse files Browse the repository at this point in the history
Close #7
  • Loading branch information
Toilal committed Nov 26, 2016
1 parent 42d0a58 commit 8b84357
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions rebulk/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def _match(self, pattern, input_string, context=None):
chain_input_string = input_string
offset = 0
while offset < len(input_string):
chain_found = False
current_chain_matches = []
valid_chain = True
is_chain_start = True
Expand All @@ -174,6 +175,7 @@ def _match(self, pattern, input_string, context=None):
chain_input_string,
context)
if raw_chain_part_matches:
chain_found = True
Chain._fix_matches_offset(raw_chain_part_matches, input_string, offset)
offset = raw_chain_part_matches[-1].raw_end
chain_input_string = input_string[offset:]
Expand All @@ -185,9 +187,9 @@ def _match(self, pattern, input_string, context=None):
offset = current_chain_matches[0].raw_end
break
is_chain_start = False
if not current_chain_matches:
if not chain_found:
break
if valid_chain:
if current_chain_matches and valid_chain:
match = self._build_chain_match(current_chain_matches, input_string)
chain_matches.append(match)

Expand Down
37 changes: 37 additions & 0 deletions rebulk/test/test_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,40 @@ def test_matches_6():

matches = rebulk.matches("Some Series E01E02E03E04E05E06") # No validator on parent, so it should give 4 episodes.
assert len(matches) == 4


def test_matches_7():
seps_surround = partial(chars_surround, ' .-/')
rebulk = Rebulk()
rebulk.regex_defaults(flags=re.IGNORECASE)
rebulk.defaults(validate_all=True,
children=True, private_parent=True,
validator={'__parent__': seps_surround})

rebulk.chain(). \
regex(r'S(?P<season>\d+)'). \
regex(r'[ -](?P<season>\d+)').repeater('*')

matches = rebulk.matches("Some S01")
assert len(matches) == 1
matches[0].value = 1

matches = rebulk.matches("Some S01-02")
assert len(matches) == 2
matches[0].value = 1
matches[1].value = 2

matches = rebulk.matches("programs4/Some S01-02")
assert len(matches) == 2
matches[0].value = 1
matches[1].value = 2

matches = rebulk.matches("programs4/SomeS01middle.S02-03.andS04here")
assert len(matches) == 2
matches[0].value = 2
matches[1].value = 3

matches = rebulk.matches("Some 02.and.S04-05.here")
assert len(matches) == 2
matches[0].value = 4
matches[1].value = 5

0 comments on commit 8b84357

Please sign in to comment.