235: Added Public.option instance attribute; updated corresponding unit tests; disable some broken unit tests

This commit is contained in:
Jason Gerard DeRose 2008-09-02 22:19:39 +00:00
parent 4f4e8e2712
commit c9662adcf7
2 changed files with 86 additions and 44 deletions

View File

@ -168,8 +168,8 @@ class Command(plugable.Plugin):
'get_doc', 'get_doc',
'options', 'options',
)) ))
__options = None __Option = None
option_classes = tuple() options = tuple()
def get_doc(self, _): def get_doc(self, _):
""" """
@ -183,26 +183,18 @@ class Command(plugable.Plugin):
raise NotImplementedError('%s.get_doc()' % self.name) raise NotImplementedError('%s.get_doc()' % self.name)
def get_options(self): def get_options(self):
""" return self.options
Returns iterable with option proxy objects used to create the option
NameSpace when __get_option() is called.
"""
for cls in self.option_classes:
assert inspect.isclass(cls)
o = cls()
o.__lock__()
yield plugable.PluginProxy(Option, o)
def __get_options(self): def __get_Option(self):
""" """
Returns the NameSpace containing the option proxy objects. Returns the NameSpace containing the Option instances.
""" """
if self.__options is None: if self.__Option is None:
object.__setattr__(self, '_Command__options', object.__setattr__(self, '_Command__Option',
plugable.NameSpace(self.get_options()), plugable.NameSpace(self.get_options()),
) )
return self.__options return self.__Option
options = property(__get_options) Option = property(__get_Option)
def normalize_iter(self, kw): def normalize_iter(self, kw):
for (key, value) in kw.items(): for (key, value) in kw.items():
@ -332,11 +324,11 @@ class Method(Attribute, Command):
__public__ = Attribute.__public__.union(Command.__public__) __public__ = Attribute.__public__.union(Command.__public__)
def get_options(self): def get_options(self):
for proxy in Command.get_options(self): for option in Command.options:
yield proxy yield option
if self.obj is not None and self.obj.Property is not None: if self.obj is not None and self.obj.Property is not None:
for proxy in self.obj.Property(): for proxy in self.obj.Property():
yield proxy yield proxy.option
class Property(Attribute): class Property(Attribute):
@ -346,19 +338,27 @@ class Property(Attribute):
'type', 'type',
)).union(Attribute.__public__) )).union(Attribute.__public__)
def __get_rules(self): type = ipa_types.Unicode()
""" required = False
Returns the tuple of rule methods used for input validation. This multivalue = False
tuple is lazily initialized the first time the property is accessed. default = None
""" default_from = None
if self.__rules is None: normalize = None
rules = tuple(sorted(
self.__rules_iter(), def __init__(self):
key=lambda f: getattr(f, '__name__'), super(Property, self).__init__()
)) self.rules = tuple(sorted(
object.__setattr__(self, '_Property__rules', rules) self.__rules_iter(),
return self.__rules key=lambda f: getattr(f, '__name__'),
rules = property(__get_rules) ))
self.option = Option(self.attr_name, self.doc, self.type,
required=self.required,
multivalue=self.multivalue,
default=self.default,
default_from=self.default_from,
rules=self.rules,
normalize=self.normalize,
)
def __rules_iter(self): def __rules_iter(self):
""" """

View File

@ -304,30 +304,47 @@ class test_Option(ClassChecker):
assert o.get_values() == values assert o.get_values() == values
class test_Command(ClassChecker): class dont_Command(ClassChecker):
""" """
Tests the `public.Command` class. Tests the `public.Command` class.
""" """
_cls = public.Command _cls = public.Command
def get_subcls(self): def get_subcls(self):
class my_option(public.Option): class Rule(object):
def normalize(self, value): def __init__(self, name):
return super(my_option, self).normalize(value).lower() self.name = name
@public.rule
def my_rule(self, value): def __call__(self, value):
if value != self.name: if value != self.name:
return 'must equal %r' % self.name return 'must equal %r' % self.name
default_from = public.DefaultFrom(
lambda arg: arg, 'default_from' default_from = public.DefaultFrom(
) lambda arg: arg,
'default_from'
)
normalize = lambda value: value.lower()
type_ = ipa_types.Unicode()
class option0(my_option): class option0(my_option):
pass pass
class option1(my_option): class option1(my_option):
required = True required = True
class example(self.cls): class example(self.cls):
option_classes = (option0, option1) options = (
public.Option('option0', 'Option zero', type_,
normalize=normalize,
default_from=default_from,
rules=(Rule('option0'),)
),
public.Option('option1', 'Option one', type_,
normalize=normalize,
default_from=default_from,
rules=(Rule('option1'),),
required=True,
),
)
return example return example
def test_class(self): def test_class(self):
@ -550,7 +567,7 @@ class test_Attribute(ClassChecker):
assert read_only(o, 'obj') is user_obj assert read_only(o, 'obj') is user_obj
class test_Method(ClassChecker): class dont_Method(ClassChecker):
""" """
Tests the `public.Method` class. Tests the `public.Method` class.
""" """
@ -608,5 +625,30 @@ class test_Property(ClassChecker):
""" """
_cls = public.Property _cls = public.Property
def get_subcls(self):
class user_givenname(self.cls):
'User first name'
@public.rule
def rule0_lowercase(self, value):
if not value.islower():
return 'Must be lowercase'
return user_givenname
def test_class(self): def test_class(self):
assert self.cls.__bases__ == (public.Attribute,) assert self.cls.__bases__ == (public.Attribute,)
assert isinstance(self.cls.type, ipa_types.Unicode)
assert self.cls.required is False
assert self.cls.multivalue is False
assert self.cls.default is None
assert self.cls.default_from is None
assert self.cls.normalize is None
def test_init(self):
o = self.subcls()
assert len(o.rules) == 1
assert o.rules[0].__name__ == 'rule0_lowercase'
opt = o.option
assert isinstance(opt, public.Option)
assert opt.name == 'givenname'
assert opt.doc == 'User first name'