mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2026-11-02 20:30:26 -06:00
285: Started work on Command.args_to_kw() method; added unit test for functionality so far in args_to_kw()
This commit is contained in:
1 parent
687f603562
commit
100492d98a
3 files changed
+63
No files matched your search
@@ -107,6 +107,19 @@ class IPAError(Exception):
|
||||
return self.format % self.args
|
||||
|
||||
|
||||
class ArgumentError(IPAError):
|
||||
"""
|
||||
Raised when a command is called with wrong number of arguments.
|
||||
"""
|
||||
|
||||
format = '%s %s'
|
||||
|
||||
def __init__(self, command, error):
|
||||
self.command = command
|
||||
self.error = error
|
||||
IPAError.__init__(self, command.name, error)
|
||||
|
||||
|
||||
class ValidationError(IPAError):
|
||||
"""
|
||||
Base class for all types of validation errors.
|
||||
|
||||
@@ -236,6 +236,9 @@ class Command(plugable.Plugin):
|
||||
def __init__(self):
|
||||
self.args = plugable.NameSpace(self.__check_args(), sort=False)
|
||||
self.options = plugable.NameSpace(self.__check_options(), sort=False)
|
||||
self.params = plugable.NameSpace(
|
||||
tuple(self.args()) + tuple(self.options()), sort=False
|
||||
)
|
||||
|
||||
def get_args(self):
|
||||
return self.takes_args
|
||||
@@ -347,6 +350,32 @@ class Command(plugable.Plugin):
|
||||
for option in sorted(self.options(), key=get_key):
|
||||
yield option
|
||||
|
||||
def args_to_kw(self, *args):
|
||||
Args = tuple(self.args())
|
||||
if len(args) > len(Args):
|
||||
if len(Args) > 0 and not Args[-1].multivalue:
|
||||
if len(Args) == 1:
|
||||
error = 'takes at most 1 argument'
|
||||
else:
|
||||
error = 'takes at most %d arguments' % len(Args)
|
||||
raise errors.ArgumentError(self, error)
|
||||
else:
|
||||
raise errors.ArgumentError(self, 'takes no arguments')
|
||||
MinArgs = sum(int(A.required) for A in Args)
|
||||
if len(args) < MinArgs:
|
||||
if MinArgs == 1:
|
||||
error = 'takes at least 1 argument'
|
||||
else:
|
||||
error = 'takes at least %d arguments' % MinArgs
|
||||
raise errors.ArgumentError(self, error)
|
||||
for (i, Arg) in enumerate(Args):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class Object(plugable.Plugin):
|
||||
|
||||
@@ -550,6 +550,27 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
assert 'execute' in self.cls.__public__ # Public
|
||||
|
||||
def test_args_to_kw(self):
|
||||
o = self.__get_instance()
|
||||
e = raises(errors.ArgumentError, o.args_to_kw, 1)
|
||||
assert str(e) == 'example takes no arguments'
|
||||
|
||||
o = self.__get_instance(args=('one?',))
|
||||
e = raises(errors.ArgumentError, o.args_to_kw, 1, 2)
|
||||
assert str(e) == 'example takes at most 1 argument'
|
||||
|
||||
o = self.__get_instance(args=('one', 'two?'))
|
||||
e = raises(errors.ArgumentError, o.args_to_kw, 1, 2, 3)
|
||||
assert str(e) == 'example takes at most 2 arguments'
|
||||
|
||||
o = self.__get_instance(args=('one', 'two?'))
|
||||
e = raises(errors.ArgumentError, o.args_to_kw)
|
||||
assert str(e) == 'example takes at least 1 argument'
|
||||
|
||||
o = self.__get_instance(args=('one', 'two', 'three?'))
|
||||
e = raises(errors.ArgumentError, o.args_to_kw, 1)
|
||||
assert str(e) == 'example takes at least 2 arguments'
|
||||
|
||||
|
||||
class test_Object(ClassChecker):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user