Updated Caper to v0.3.1
This commit is contained in:
1 parent
2520b19798
commit
eb151a4c5d
11 files changed
+574
-124
No files matched your search
+87
-24
@@ -14,7 +14,7 @@
|
||||
|
||||
|
||||
class CaptureConstraint(object):
|
||||
def __init__(self, capture_group, comparisons=None, **kwargs):
|
||||
def __init__(self, capture_group, constraint_type, comparisons=None, target=None, **kwargs):
|
||||
"""Capture constraint object
|
||||
|
||||
:type capture_group: CaptureGroup
|
||||
@@ -22,50 +22,113 @@ class CaptureConstraint(object):
|
||||
|
||||
self.capture_group = capture_group
|
||||
|
||||
self.comparisons = comparisons if comparisons else []
|
||||
self.constraint_type = constraint_type
|
||||
self.target = target
|
||||
|
||||
for key, value in kwargs.items():
|
||||
key = key.split('__')
|
||||
self.comparisons = comparisons if comparisons else []
|
||||
self.kwargs = {}
|
||||
|
||||
for orig_key, value in kwargs.items():
|
||||
key = orig_key.split('__')
|
||||
if len(key) != 2:
|
||||
self.kwargs[orig_key] = value
|
||||
continue
|
||||
name, method = key
|
||||
|
||||
method = '_compare_' + method
|
||||
method = 'constraint_match_' + method
|
||||
if not hasattr(self, method):
|
||||
self.kwargs[orig_key] = value
|
||||
continue
|
||||
|
||||
self.comparisons.append((name, getattr(self, method), value))
|
||||
|
||||
def _compare_eq(self, fragment, name, expected):
|
||||
if not hasattr(fragment, name):
|
||||
return 1.0, False
|
||||
def execute(self, parent_node, node, **kwargs):
|
||||
func_name = 'constraint_%s' % self.constraint_type
|
||||
|
||||
return 1.0, getattr(fragment, name) == expected
|
||||
if hasattr(self, func_name):
|
||||
return getattr(self, func_name)(parent_node, node, **kwargs)
|
||||
|
||||
def _compare_re(self, fragment, name, arg):
|
||||
if name == 'fragment':
|
||||
group, minimum_weight = arg if type(arg) is tuple and len(arg) > 1 else (arg, 0)
|
||||
raise ValueError('Unknown constraint type "%s"' % self.constraint_type)
|
||||
|
||||
weight, match, num_fragments = self.capture_group.parser.matcher.fragment_match(fragment, group)
|
||||
return weight, weight > minimum_weight
|
||||
elif type(arg).__name__ == 'SRE_Pattern':
|
||||
return 1.0, arg.match(getattr(fragment, name)) is not None
|
||||
elif hasattr(fragment, name):
|
||||
match = self.capture_group.parser.matcher.value_match(getattr(fragment, name), arg, single=True)
|
||||
return 1.0, match is not None
|
||||
else:
|
||||
raise ValueError("Unable to find attribute with name '%s'" % name)
|
||||
#
|
||||
# Node Matching
|
||||
#
|
||||
|
||||
def execute(self, fragment):
|
||||
def constraint_match(self, parent_node, node):
|
||||
results = []
|
||||
total_weight = 0
|
||||
|
||||
for name, method, argument in self.comparisons:
|
||||
weight, success = method(fragment, name, argument)
|
||||
weight, success = method(node, name, argument)
|
||||
total_weight += weight
|
||||
results.append(success)
|
||||
|
||||
return total_weight / float(len(results)), all(results) if len(results) > 0 else False
|
||||
return total_weight / (float(len(results)) or 1), all(results) if len(results) > 0 else False
|
||||
|
||||
def constraint_match_eq(self, node, name, expected):
|
||||
if not hasattr(node, name):
|
||||
return 1.0, False
|
||||
|
||||
return 1.0, getattr(node, name) == expected
|
||||
|
||||
def constraint_match_re(self, node, name, arg):
|
||||
# Node match
|
||||
if name == 'node':
|
||||
group, minimum_weight = arg if type(arg) is tuple and len(arg) > 1 else (arg, 0)
|
||||
|
||||
weight, match, num_fragments = self.capture_group.parser.matcher.fragment_match(node, group)
|
||||
return weight, weight > minimum_weight
|
||||
|
||||
# Regex match
|
||||
if type(arg).__name__ == 'SRE_Pattern':
|
||||
return 1.0, arg.match(getattr(node, name)) is not None
|
||||
|
||||
# Value match
|
||||
if hasattr(node, name):
|
||||
match = self.capture_group.parser.matcher.value_match(getattr(node, name), arg, single=True)
|
||||
return 1.0, match is not None
|
||||
|
||||
raise ValueError("Unknown constraint match type '%s'" % name)
|
||||
|
||||
#
|
||||
# Result
|
||||
#
|
||||
|
||||
def constraint_result(self, parent_node, fragment):
|
||||
ctag = self.kwargs.get('tag')
|
||||
if not ctag:
|
||||
return 0, False
|
||||
|
||||
ckey = self.kwargs.get('key')
|
||||
|
||||
for tag, result in parent_node.captured():
|
||||
if tag != ctag:
|
||||
continue
|
||||
|
||||
if not ckey or ckey in result.keys():
|
||||
return 1.0, True
|
||||
|
||||
return 0.0, False
|
||||
|
||||
#
|
||||
# Failure
|
||||
#
|
||||
|
||||
def constraint_failure(self, parent_node, fragment, match):
|
||||
if not match or not match.success:
|
||||
return 1.0, True
|
||||
|
||||
return 0, False
|
||||
|
||||
#
|
||||
# Success
|
||||
#
|
||||
|
||||
def constraint_success(self, parent_node, fragment, match):
|
||||
if match and match.success:
|
||||
return 1.0, True
|
||||
|
||||
return 0, False
|
||||
|
||||
def __repr__(self):
|
||||
return "CaptureConstraint(comparisons=%s)" % repr(self.comparisons)
|
||||
Reference in new issue
Block a user