Skip to content
This repository has been archived by the owner on Jul 24, 2018. It is now read-only.

Commit

Permalink
Merge pull request #12 from fedora-infra/feature/rule-negation
Browse files Browse the repository at this point in the history
Allow individual rules to be negated.
  • Loading branch information
ralphbean committed Aug 24, 2014
2 parents 992e13e + 5f84885 commit d6eeac2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
22 changes: 22 additions & 0 deletions alembic/versions/18ebf3181f87_add_a_negated_column_for_rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Add a negated column for rules.
Revision ID: 18ebf3181f87
Revises: 4a95022fd7f3
Create Date: 2014-08-22 15:48:08.952913
"""

# revision identifiers, used by Alembic.
revision = '18ebf3181f87'
down_revision = '4a95022fd7f3'

from alembic import op
import sqlalchemy as sa


def upgrade():
op.add_column('rules', sa.Column('negated', sa.Boolean(), default=False))


def downgrade():
op.drop_column('rules', 'negated')
6 changes: 5 additions & 1 deletion fmn/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,16 @@ def matches(filter, message, valid_paths, rule_cache, config):

for rule in filter['rules']:
fn = rule['fn']
negated = rule['negated']
arguments = rule['arguments']
rule_cache_key = rule['cache_key']

try:
if rule_cache_key not in rule_cache:
rule_cache[rule_cache_key] = fn(config, message, **arguments)
value = fn(config, message, **arguments)
if negated:
value = not value
rule_cache[rule_cache_key] = value

if not rule_cache[rule_cache_key]:
return False
Expand Down
22 changes: 20 additions & 2 deletions fmn/lib/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,25 @@ class Rule(BASE):
code_path = sa.Column(sa.String(50), nullable=False)
# JSON-encoded kw
_arguments = sa.Column(sa.String(256))
# Should we negate the output of the computed rule?
negated = sa.Column(sa.Boolean, default=False)

def __json__(self, reify=False):
result = {
'created_on': self.created_on,
'code_path': self.code_path,
'arguments': self.arguments,
'cache_key': self.cache_key,
'negated': self.negated,
}
if reify:
result['fn'] = fedmsg.utils.load_class(str(self.code_path))
return result

def __repr__(self):
return "<fmn.lib.models.Rule: %r(**%r)>" % (
self.code_path, self.arguments)
negation = self.negated and '!' or ''
return "<fmn.lib.models.Rule: %s%r(**%r)>" % (
negation, self.code_path, self.arguments)

@property
def cache_key(self):
Expand Down Expand Up @@ -365,6 +369,20 @@ def remove_rule(self, session, code_path, **kw):

raise ValueError("No such rule found: %r" % code_path)

def negate_rule(self, session, code_path, **kw):
for r in self.rules:
if r.code_path == code_path:
r.negated = not r.negated
session.commit()

pref = self.preference
if pref:
self.notify(pref.openid, pref.context_name, "rules")

return

raise ValueError("No such rule found: %r" % code_path)


class DetailValue(BASE):
__tablename__ = 'detail_values'
Expand Down

0 comments on commit d6eeac2

Please sign in to comment.