Skip to content

Commit

Permalink
Replaced CaperResult.captured with CaperNode.captured() so the result…
Browse files Browse the repository at this point in the history
… is based on the current result chain.
  • Loading branch information
fuzeman committed Dec 10, 2013
1 parent 23f6c64 commit 1cbafba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
30 changes: 16 additions & 14 deletions src/caper/constraint.py
Expand Up @@ -66,15 +66,15 @@ def _compare_re(self, node, name, arg):
else:
raise ValueError("Unable to find attribute with name '%s'" % name)

def execute(self, fragment, **kwargs):
def execute(self, parent_node, fragment, **kwargs):
func_name = 'constraint_%s' % self.constraint_type

if hasattr(self, func_name):
return getattr(self, func_name)(fragment, **kwargs)
return getattr(self, func_name)(parent_node, fragment, **kwargs)

raise ValueError('Unknown constraint type "%s"' % self.constraint_type)

def constraint_match(self, fragment):
def constraint_match(self, parent_node, fragment):
results = []
total_weight = 0

Expand All @@ -85,27 +85,29 @@ def constraint_match(self, fragment):

return total_weight / (float(len(results)) or 1), all(results) if len(results) > 0 else False

def constraint_result(self, fragment):
tag = self.kwargs.get('tag')
key = self.kwargs.get('key')

if not tag or not key:
def constraint_result(self, parent_node, fragment):
ctag = self.kwargs.get('tag')
if not ctag:
return 0, False

captured = self.capture_group.result.captured
ckey = self.kwargs.get('key')

if tag not in captured or key not in captured[tag]:
return 0, False
for tag, keys in parent_node.captured():
if tag != ctag:
continue

if not ckey or ckey in keys:
return 1.0, True

return 1.0, True
return 0.0, False

def constraint_failure(self, fragment, match):
def constraint_failure(self, parent_node, fragment, match):
if not match or not match.success:
return 1.0, True

return 0, False

def constraint_success(self, fragment, match):
def constraint_success(self, parent_node, fragment, match):
#if match and match.success:
# return 1.0, True

Expand Down
9 changes: 1 addition & 8 deletions src/caper/group.py
Expand Up @@ -170,13 +170,6 @@ def parse_closure(self, parent_head, subject):
else:
nodes.append(CaperClosureNode(subject, parent_head))

if match.result:
if match.tag not in self.result.captured:
self.result.captured[match.tag] = []

for key in match.result:
self.result.captured[match.tag].append(key)

nodes.append(result[0] if len(result) == 1 else result)

return nodes
Expand Down Expand Up @@ -227,7 +220,7 @@ def check_constraints(self, constraints, parent_head, subject, **kwargs):

# Check constraints
for constraint in constraints:
weight, success = constraint.execute(subject, **kwargs)
weight, success = constraint.execute(parent_node, subject, **kwargs)

if success:
Logr.debug('capturing broke on "%s" at %s', subject.value, constraint)
Expand Down
4 changes: 2 additions & 2 deletions src/caper/parsers/usenet.py
Expand Up @@ -31,7 +31,7 @@
]),

('detail', [
r'[\s-]*\"(?P<file_name>.*?)\"(\sy(?P<extra>yEnc))?'
r'[\s-]*\"(?P<file_name>.*?)\"(\s(?P<extra>yEnc))?'
])
]

Expand All @@ -55,7 +55,7 @@ def run(self, closures):

self.capture_closure('usenet', regex='usenet', single=False)\
.capture_closure('part', regex='part', single=True) \
.until_result(key='part') \
.until_result(tag='part') \
.until_failure()\
.execute()

Expand Down
14 changes: 12 additions & 2 deletions src/caper/result.py
Expand Up @@ -41,6 +41,18 @@ def __init__(self, closure, parent=None, match=None):
def next(self):
raise NotImplementedError()

def captured(self):
cur = self

if cur.match:
yield cur.match.tag, cur.match.result.keys()

while cur.parent:
cur = cur.parent

if cur.match:
yield cur.match.tag, cur.match.result.keys()


class CaperRootNode(CaperNode):
def __init__(self, closure):
Expand Down Expand Up @@ -114,8 +126,6 @@ def __init__(self):

self.chains = []

self.captured = {}

def build(self):
max_matched = 0

Expand Down

0 comments on commit 1cbafba

Please sign in to comment.