From 2cc88a7a3233d6bd70ec174ba976d093dc7f0d98 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Thu, 7 Aug 2008 05:02:56 +0000 Subject: [PATCH] 74: Finished opt.__rules_iter(); is_rule(obj) now returns False if obj is not callable; updated unit tests --- ipalib/public.py | 11 +++++++---- ipalib/tests/test_public.py | 23 ++++++++--------------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/ipalib/public.py b/ipalib/public.py index b43e0af97..baae83e42 100644 --- a/ipalib/public.py +++ b/ipalib/public.py @@ -35,9 +35,7 @@ def rule(obj): return obj def is_rule(obj): - return getattr(obj, RULE_FLAG, False) is True - - + return callable(obj) and getattr(obj, RULE_FLAG, False) is True class opt(plugable.ReadOnly): @@ -65,7 +63,12 @@ class opt(plugable.ReadOnly): rules = property(__get_rules) def __rules_iter(self): - pass + for name in dir(self): + if name.startswith('_'): + continue + attr = getattr(self, name) + if is_rule(attr): + yield attr def validate(self, value): pass diff --git a/ipalib/tests/test_public.py b/ipalib/tests/test_public.py index 122f489d4..ef2ded171 100644 --- a/ipalib/tests/test_public.py +++ b/ipalib/tests/test_public.py @@ -47,27 +47,20 @@ def test_is_rule(): is_rule = public.is_rule flag = public.RULE_FLAG - class example(object): + class no_call(object): def __init__(self, value): if value is not None: assert value in (True, False) setattr(self, flag, value) - obj = example(True) - assert getattr(obj, flag) is True - assert is_rule(obj) - - obj = example(False) - assert getattr(obj, flag) is False - assert not is_rule(obj) - - obj = example(None) - assert not hasattr(obj, flag) - assert not is_rule(obj) - - - + class call(no_call): + def __call__(self): + pass + assert is_rule(call(True)) + assert not is_rule(no_call(True)) + assert not is_rule(call(False)) + assert not is_rule(call(None)) class test_opt():