Added frontend.LocalOrRemote command base class for commands like env

This commit is contained in:
Jason Gerard DeRose 2008-11-14 21:29:46 -07:00
parent 3433840692
commit 36737c2d91
3 changed files with 88 additions and 2 deletions

View File

@ -873,7 +873,8 @@ freeIPA.org:
import plugable
from backend import Backend, Context
from frontend import Command, Object, Method, Property, Application
from frontend import Command, LocalOrRemote, Application
from frontend import Object, Method, Property
from ipa_types import Bool, Int, Unicode, Enum
from frontend import Param, DefaultFrom

View File

@ -782,6 +782,37 @@ class Command(plugable.Plugin):
yield arg
class LocalOrRemote(Command):
"""
A command that is explicitly executed locally or remotely.
This is for commands that makes sense to execute either locally or
remotely to return a perhaps different result. The best example of
this is the `ipalib.plugins.f_misc.env` plugin which returns the
key/value pairs describing the configuration state: it can be
"""
takes_options = (
Param('server', type=ipa_types.Bool(), default=False,
doc='Forward to server instead of running locally',
),
)
def run(self, *args, **options):
"""
Dispatch to forward() or execute() based on ``server`` option.
When running in a client context, this command is executed remotely if
``options['server']`` is true; otherwise it is executed locally.
When running in a server context, this command is always executed
locally and the value of ``options['server']`` is ignored.
"""
if options['server'] and not self.env.in_server:
return self.forward(*args, **options)
return self.execute(*args, **options)
class Object(plugable.Plugin):
__public__ = frozenset((
'backend',

View File

@ -22,7 +22,7 @@ Test the `ipalib.frontend` module.
"""
from tests.util import raises, getitem, no_set, no_del, read_only
from tests.util import check_TypeError, ClassChecker
from tests.util import check_TypeError, ClassChecker, get_api
from ipalib import frontend, backend, plugable, errors, ipa_types, config
@ -796,6 +796,60 @@ class test_Command(ClassChecker):
assert o.run.im_func is my_cmd.forward.im_func
class test_LocalOrRemote(ClassChecker):
"""
Test the `ipalib.frontend.LocalOrRemote` class.
"""
_cls = frontend.LocalOrRemote
def test_init(self):
"""
Test the `ipalib.frontend.LocalOrRemote.__init__` method.
"""
o = self.cls()
o.finalize()
assert list(o.args) == []
assert list(o.options) == ['server']
op = o.options.server
assert op.required is True
assert op.default is False
def test_run(self):
"""
Test the `ipalib.frontend.LocalOrRemote.run` method.
"""
class example(self.cls):
takes_args = ['key?']
def forward(self, *args, **options):
return ('forward', args, options)
def execute(self, *args, **options):
return ('execute', args, options)
# Test when in_server=False:
(api, home) = get_api(in_server=False)
api.register(example)
api.finalize()
cmd = api.Command.example
assert cmd() == ('execute', (None,), dict(server=False))
assert cmd('var') == ('execute', (u'var',), dict(server=False))
assert cmd(server=True) == ('forward', (None,), dict(server=True))
assert cmd('var', server=True) == \
('forward', (u'var',), dict(server=True))
# Test when in_server=True (should always call execute):
(api, home) = get_api(in_server=True)
api.register(example)
api.finalize()
cmd = api.Command.example
assert cmd() == ('execute', (None,), dict(server=False))
assert cmd('var') == ('execute', (u'var',), dict(server=False))
assert cmd(server=True) == ('execute', (None,), dict(server=True))
assert cmd('var', server=True) == \
('execute', (u'var',), dict(server=True))
class test_Object(ClassChecker):
"""
Test the `ipalib.frontend.Object` class.