summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichal Minar <miminar@redhat.com>2014-03-17 12:10:34 +0100
committerMichal Minar <miminar@redhat.com>2014-03-17 16:26:36 +0100
commitaf8b59124f1eab1d21765e5b1a3f2443fc5ff70b (patch)
tree38c751ba3862cd8362254f312cf29482dc32135a
parent3c44652bf567632d3ec2c5ef400f21b01ff66a6e (diff)
downloadopenlmi-scripts-af8b59124f1eab1d21765e5b1a3f2443fc5ff70b.tar.gz
openlmi-scripts-af8b59124f1eab1d21765e5b1a3f2443fc5ff70b.tar.xz
openlmi-scripts-af8b59124f1eab1d21765e5b1a3f2443fc5ff70b.zip
added select_command helper
It is a function that builds LmiSelectCommand subclass.
-rw-r--r--lmi/scripts/common/command/__init__.py1
-rw-r--r--lmi/scripts/common/command/helper.py38
2 files changed, 39 insertions, 0 deletions
diff --git a/lmi/scripts/common/command/__init__.py b/lmi/scripts/common/command/__init__.py
index 2062577..a7c2df4 100644
--- a/lmi/scripts/common/command/__init__.py
+++ b/lmi/scripts/common/command/__init__.py
@@ -47,3 +47,4 @@ from lmi.scripts.common.command.show import LmiShowInstance
from lmi.scripts.common.command.helper import make_list_command
from lmi.scripts.common.command.helper import register_subcommands
+from lmi.scripts.common.command.helper import select_command
diff --git a/lmi/scripts/common/command/helper.py b/lmi/scripts/common/command/helper.py
index 3a2bfde..578fa8b 100644
--- a/lmi/scripts/common/command/helper.py
+++ b/lmi/scripts/common/command/helper.py
@@ -33,6 +33,7 @@ Module with convenient function for defining user commands.
from lmi.scripts.common.command import LmiLister
from lmi.scripts.common.command import LmiCommandMultiplexer
+from lmi.scripts.common.command import LmiSelectCommand
from lmi.scripts.common.command import util
def make_list_command(func,
@@ -97,3 +98,40 @@ def register_subcommands(command_name, usage, command_map,
return LmiCommandMultiplexer.__metaclass__(command_name,
(LmiCommandMultiplexer, ), props)
+def select_command(command_name, *args, **kwargs):
+ """
+ Create command selector that loads command whose requirements are met.
+
+ Example of invocation: ::
+
+ Hardware = select_command('Hardware',
+ ("Openlmi-Hardware >= 0.4.2", "lmi.scripts.hardware.current.Cmd"),
+ ("Openlmi-Hardware < 0.4.2" , "lmi.scripts.hardware.pre042.Cmd"),
+ default=HwMissing
+ )
+
+ Above example checks remote broker for OpenLMI-Hardware provider. If it is
+ installed and its version is equal or higher than 0.4.2, command from
+ ``current`` module will be used. For older registered versions command
+ contained in ``pre042`` module will be loaded. If hardware provider is not
+ available, HwMissing command will be loaded instead.
+
+ .. seealso::
+ Check out the grammer describing language used in these conditions at
+ :py:mod:`lmi.scripts.common.versioncheck.parser`.
+
+ :param args: List of pairs ``(condition, command)`` that are inspected in
+ given order until single condition is satisfied. Associated command is
+ then loaded. Command is either a reference to command class or path to
+ it given as string. In latter case last dot divides module's import
+ path and command name.
+ :param default: This command will be loaded when no condition from *args*
+ is satisfied.
+ """
+ props = { 'SELECT' : args
+ , 'DEFAULT' : kwargs.get('default', None)
+ , '__module__' : util.get_module_name()
+ }
+ return LmiSelectCommand.__metaclass__(command_name,
+ (LmiSelectCommand, ), props)
+