mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
236: Ported pubic.Command to new Option; updated corresponding unit tests
This commit is contained in:
@@ -160,28 +160,16 @@ class Option(plugable.ReadOnly):
|
|||||||
|
|
||||||
class Command(plugable.Plugin):
|
class Command(plugable.Plugin):
|
||||||
__public__ = frozenset((
|
__public__ = frozenset((
|
||||||
'normalize',
|
|
||||||
'get_default',
|
'get_default',
|
||||||
|
'normalize',
|
||||||
'validate',
|
'validate',
|
||||||
'execute',
|
'execute',
|
||||||
'__call__',
|
'__call__',
|
||||||
'get_doc',
|
'Option',
|
||||||
'options',
|
|
||||||
))
|
))
|
||||||
__Option = None
|
__Option = None
|
||||||
options = tuple()
|
options = tuple()
|
||||||
|
|
||||||
def get_doc(self, _):
|
|
||||||
"""
|
|
||||||
Returns the gettext translated doc-string for this command.
|
|
||||||
|
|
||||||
For example:
|
|
||||||
|
|
||||||
>>> def get_doc(self, _):
|
|
||||||
>>> return _('add new user')
|
|
||||||
"""
|
|
||||||
raise NotImplementedError('%s.get_doc()' % self.name)
|
|
||||||
|
|
||||||
def get_options(self):
|
def get_options(self):
|
||||||
return self.options
|
return self.options
|
||||||
|
|
||||||
@@ -196,21 +184,21 @@ class Command(plugable.Plugin):
|
|||||||
return self.__Option
|
return self.__Option
|
||||||
Option = property(__get_Option)
|
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():
|
||||||
if key in self.options:
|
if key in self.Option:
|
||||||
yield (
|
yield (
|
||||||
key, self.options[key].normalize(value)
|
key, self.Option[key].normalize(value)
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
yield (key, value)
|
yield (key, value)
|
||||||
|
|
||||||
def normalize(self, **kw):
|
def normalize(self, **kw):
|
||||||
self.print_call('normalize', kw, 1)
|
self.print_call('normalize', kw, 1)
|
||||||
return dict(self.normalize_iter(kw))
|
return dict(self.__normalize_iter(kw))
|
||||||
|
|
||||||
def get_default_iter(self, kw):
|
def __get_default_iter(self, kw):
|
||||||
for option in self.options():
|
for option in self.Option():
|
||||||
if option.name not in kw:
|
if option.name not in kw:
|
||||||
value = option.get_default(**kw)
|
value = option.get_default(**kw)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
@@ -218,17 +206,17 @@ class Command(plugable.Plugin):
|
|||||||
|
|
||||||
def get_default(self, **kw):
|
def get_default(self, **kw):
|
||||||
self.print_call('default', kw, 1)
|
self.print_call('default', kw, 1)
|
||||||
return dict(self.get_default_iter(kw))
|
return dict(self.__get_default_iter(kw))
|
||||||
|
|
||||||
def validate(self, **kw):
|
def validate(self, **kw):
|
||||||
self.print_call('validate', kw, 1)
|
self.print_call('validate', kw, 1)
|
||||||
for opt in self.options():
|
for option in self.Option():
|
||||||
value = kw.get(opt.name, None)
|
value = kw.get(option.name, None)
|
||||||
if value is None:
|
if value is None:
|
||||||
if opt.required:
|
if option.required:
|
||||||
raise errors.RequirementError(opt.name)
|
raise errors.RequirementError(option.name)
|
||||||
continue
|
continue
|
||||||
opt.validate(value)
|
option.validate(value)
|
||||||
|
|
||||||
def execute(self, **kw):
|
def execute(self, **kw):
|
||||||
self.print_call('execute', kw, 1)
|
self.print_call('execute', kw, 1)
|
||||||
|
|||||||
@@ -304,7 +304,7 @@ class test_Option(ClassChecker):
|
|||||||
assert o.get_values() == values
|
assert o.get_values() == values
|
||||||
|
|
||||||
|
|
||||||
class dont_Command(ClassChecker):
|
class test_Command(ClassChecker):
|
||||||
"""
|
"""
|
||||||
Tests the `public.Command` class.
|
Tests the `public.Command` class.
|
||||||
"""
|
"""
|
||||||
@@ -326,11 +326,6 @@ class dont_Command(ClassChecker):
|
|||||||
normalize = lambda value: value.lower()
|
normalize = lambda value: value.lower()
|
||||||
type_ = ipa_types.Unicode()
|
type_ = ipa_types.Unicode()
|
||||||
|
|
||||||
class option0(my_option):
|
|
||||||
pass
|
|
||||||
class option1(my_option):
|
|
||||||
required = True
|
|
||||||
|
|
||||||
class example(self.cls):
|
class example(self.cls):
|
||||||
options = (
|
options = (
|
||||||
public.Option('option0', 'Option zero', type_,
|
public.Option('option0', 'Option zero', type_,
|
||||||
@@ -349,7 +344,7 @@ class dont_Command(ClassChecker):
|
|||||||
|
|
||||||
def test_class(self):
|
def test_class(self):
|
||||||
assert self.cls.__bases__ == (plugable.Plugin,)
|
assert self.cls.__bases__ == (plugable.Plugin,)
|
||||||
assert type(self.cls.options) == property
|
assert self.cls.options == tuple()
|
||||||
|
|
||||||
def test_get_options(self):
|
def test_get_options(self):
|
||||||
"""
|
"""
|
||||||
@@ -357,27 +352,26 @@ class dont_Command(ClassChecker):
|
|||||||
"""
|
"""
|
||||||
assert list(self.cls().get_options()) == []
|
assert list(self.cls().get_options()) == []
|
||||||
sub = self.subcls()
|
sub = self.subcls()
|
||||||
for (i, proxy) in enumerate(sub.get_options()):
|
for (i, option) in enumerate(sub.get_options()):
|
||||||
assert isinstance(proxy, plugable.PluginProxy)
|
assert isinstance(option, public.Option)
|
||||||
assert read_only(proxy, 'name') == 'option%d' % i
|
assert read_only(option, 'name') == 'option%d' % i
|
||||||
assert proxy.implements(public.Option)
|
|
||||||
assert i == 1
|
assert i == 1
|
||||||
|
|
||||||
def test_options(self):
|
def test_Option(self):
|
||||||
"""
|
"""
|
||||||
Tests the `public.Command.options` property.
|
Tests the `public.Command.Option` property.
|
||||||
"""
|
"""
|
||||||
assert 'options' in self.cls.__public__ # Public
|
assert 'Option' in self.cls.__public__ # Public
|
||||||
sub = self.subcls()
|
sub = self.subcls()
|
||||||
options = sub.options
|
O = sub.Option
|
||||||
assert type(options) == plugable.NameSpace
|
assert type(O) is plugable.NameSpace
|
||||||
assert len(options) == 2
|
assert len(O) == 2
|
||||||
for name in ('option0', 'option1'):
|
for name in ('option0', 'option1'):
|
||||||
assert name in options
|
assert name in O
|
||||||
proxy = options[name]
|
option = O[name]
|
||||||
assert getattr(options, name) is proxy
|
assert getattr(O, name) is option
|
||||||
assert isinstance(proxy, plugable.PluginProxy)
|
assert isinstance(option, public.Option)
|
||||||
assert proxy.name == name
|
assert option.name == name
|
||||||
|
|
||||||
def test_normalize(self):
|
def test_normalize(self):
|
||||||
"""
|
"""
|
||||||
@@ -385,9 +379,9 @@ class dont_Command(ClassChecker):
|
|||||||
"""
|
"""
|
||||||
assert 'normalize' in self.cls.__public__ # Public
|
assert 'normalize' in self.cls.__public__ # Public
|
||||||
kw = dict(
|
kw = dict(
|
||||||
option0='OPTION0',
|
option0=u'OPTION0',
|
||||||
option1='OPTION1',
|
option1=u'OPTION1',
|
||||||
option2='option2',
|
option2=u'option2',
|
||||||
)
|
)
|
||||||
norm = dict((k, v.lower()) for (k, v) in kw.items())
|
norm = dict((k, v.lower()) for (k, v) in kw.items())
|
||||||
sub = self.subcls()
|
sub = self.subcls()
|
||||||
@@ -424,8 +418,8 @@ class dont_Command(ClassChecker):
|
|||||||
|
|
||||||
# Check with valid args
|
# Check with valid args
|
||||||
okay = dict(
|
okay = dict(
|
||||||
option0='option0',
|
option0=u'option0',
|
||||||
option1='option1',
|
option1=u'option1',
|
||||||
another_option='some value',
|
another_option='some value',
|
||||||
)
|
)
|
||||||
sub.validate(**okay)
|
sub.validate(**okay)
|
||||||
|
|||||||
Reference in New Issue
Block a user