Skip to content

Commit

Permalink
fix #209: Fix infinite recursion when multiple languages are ignored …
Browse files Browse the repository at this point in the history
…for second pass.

This rewrites the buggy implementation of skip_nodes option
  • Loading branch information
Toilal committed Sep 3, 2015
1 parent 8ec4d7b commit 9c1e689
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
35 changes: 26 additions & 9 deletions guessit/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ def process_nodes(self, nodes):
for node in nodes:
self.process_node(node)

def process_node(self, node, iterative=True, partial_span=None):
def process_node(self, node, iterative=True, partial_span=None, skip_nodes=True):
if skip_nodes and not isinstance(skip_nodes, list):
skip_nodes = self.options.get('skip_nodes')
elif not isinstance(skip_nodes, list):
skip_nodes = []

if partial_span:
value = node.value[partial_span[0]:partial_span[1]]
else:
Expand Down Expand Up @@ -257,17 +262,29 @@ def process_node(self, node, iterative=True, partial_span=None):
if partial_span:
span = (span[0] + partial_span[0], span[1] + partial_span[0])

skip_nodes = self.options.get('skip_nodes')
if skip_nodes:
skip_nodes = [skip_node for skip_node in self.options.get('skip_nodes') if skip_node.parent.span[0] == node.span[0] or skip_node.parent.span[1] == node.span[1]]
# if we guessed a node that we need to skip, recurse down the tree and ignore that node
indices = set()
skip_nodes_spans = []
next_skip_nodes = []
for skip_node in skip_nodes:
skip_node_relative_span = (skip_node.span[0] - node.offset, skip_node.span[1] - node.offset)
if skip_node_relative_span == span:
partition_spans = [s for s in node.get_partition_spans(span) if s != skip_node.span]
for partition_span in partition_spans:
relative_span = (partition_span[0] - node.offset, partition_span[1] - node.offset)
self.process_node(node, partial_span=relative_span)
return
skip_for_next = False
skip_nodes_spans.append(skip_node.span)
if node.offset <= skip_node.span[0] <= node.span[1]:
indices.add(skip_node.span[0] - node.offset)
skip_for_next = True
if node.offset <= skip_node.span[1] <= node.span[1]:
indices.add(skip_node.span[1] - node.offset)
skip_for_next = True
if not skip_for_next:
next_skip_nodes.append(skip_node)
if indices:
partition_spans = [s for s in node.get_partition_spans(indices) if s not in skip_nodes_spans]
for partition_span in partition_spans:
relative_span = (partition_span[0] - node.offset, partition_span[1] - node.offset)
self.process_node(node, partial_span=relative_span, skip_nodes=next_skip_nodes)
return

# restore sentinels compensation
if isinstance(result, Guess):
Expand Down
18 changes: 18 additions & 0 deletions guessit/test/episodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1834,3 +1834,21 @@
series: The Good Wife
title: Trust Issues
videoCodec: h264

? Fear the Walking Dead - 01x02 - So Close, Yet So Far.REPACK-KILLERS.French.C.updated.Addic7ed.com.mkv
: episodeNumber: 2
language: fr
other: Proper
properCount: 1
season: 1
series: Fear the Walking Dead
title: So Close, Yet So Far

? Fear the Walking Dead - 01x02 - En Close, Yet En Far.REPACK-KILLERS.French.C.updated.Addic7ed.com.mkv
: episodeNumber: 2
language: fr
other: Proper
properCount: 1
season: 1
series: Fear the Walking Dead
title: En Close, Yet En Far

0 comments on commit 9c1e689

Please sign in to comment.