Skip to content

Commit

Permalink
Merge pull request fedora-infra#40 from fedora-infra/feature/invert-c…
Browse files Browse the repository at this point in the history
…allable-hints

Allow callable hints to be inverted.
  • Loading branch information
ralphbean committed Mar 19, 2015
2 parents e503c53 + 46e00af commit 41d6b0a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
13 changes: 11 additions & 2 deletions fmn/lib/hinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ def gather_hinting(config, filter, valid_paths):
info = valid_paths[root][name]

if info['hints-callable']:
# Call the callable hint to get its values
result = info['hints-callable'](config=config, **rule.arguments)

# If the rule is inverted, but the hint is not invertible, then
# there is no hinting we can provide. Carry on.
if rule.negated and not info['hints-invertible']:
continue

for key, values in result.items():
# Negate the hint if necessary
key = 'not_' + key if rule.negated else key
hinting[key].extend(values)

# Then, finish off with all the other ordinary, non-callable hints
for key, value in info['datanommer-hints'].items():

# If the rule is inverted, but the hint is not invertible, then
Expand All @@ -67,8 +77,7 @@ def gather_hinting(config, filter, valid_paths):
continue

# Otherwise, construct the inverse hint if necessary
if rule.negated:
key = 'not_' + key
key = 'not_' + key if rule.negated else key

# And tack it on.
hinting[key] += value
Expand Down
23 changes: 23 additions & 0 deletions fmn/lib/tests/test_hinting.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class MockRule(object):
arguments = {
'argument1': 'cowabunga',
}
negated = False

class MockFilter(object):
rules = [MockRule()]
Expand All @@ -36,3 +37,25 @@ class MockFilter(object):
hints = fmn.lib.hinting.gather_hinting(
self.config, filter_obj, self.valid_paths)
eq_(hints, {'the-hint-is': ['cowabunga']})

def test_inverted_hint_callable(self):
rules = self.valid_paths['fmn.lib.tests.example_rules']
rule = rules['callable_hint_masked_rule']
eq_(len(rule['args']), 3)
eq_(rule['datanommer-hints'], {})

class MockRule(object):
code_path = 'fmn.lib.tests.example_rules:callable_hint_masked_rule'
arguments = {
'argument1': 'cowabunga',
}
negated = True

class MockFilter(object):
rules = [MockRule()]

filter_obj = MockFilter()

hints = fmn.lib.hinting.gather_hinting(
self.config, filter_obj, self.valid_paths)
eq_(hints, {'not_the-hint-is': ['cowabunga']})

0 comments on commit 41d6b0a

Please sign in to comment.