Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 8f4689671458964ca24c853014904a30112f0035
Author: Michal Minar <miminar(a)redhat.com>
Date: Wed Oct 8 13:57:38 2014 +0200
scripts: added CONNECTION_TIMEOUT property
Allow to override default connection timeout on particular commands.
---------------------------------------------------------------
cli/lmi/scripts/common/command/meta.py | 19 ++++++++++++++++++-
cli/lmi/scripts/common/command/session.py | 17 +++++++++++++++++
doc/src/scripts/devel/command-properties.rst | 10 +++++++++-
3 files changed, 44 insertions(+), 2 deletions(-)
diff --git a/cli/lmi/scripts/common/command/meta.py
b/cli/lmi/scripts/common/command/meta.py
index 3e9db7b..d34410c 100644
--- a/cli/lmi/scripts/common/command/meta.py
+++ b/cli/lmi/scripts/common/command/meta.py
@@ -125,6 +125,19 @@ def _handle_namespace(dcl):
return namespace
dcl['cim_namespace'] = classmethod(_new_cim_namespace)
+def _handle_connection_timeout(dcl):
+ """
+ Overrides ``connection_timeout()`` class method if ``CONNECTION_TIMEOUT``
+ property is given.
+
+ :param dictionary dcl: Class dictionary being modified by this method.
+ """
+ if 'CONNECTION_TIMEOUT' in dcl:
+ timeout = dcl.pop('CONNECTION_TIMEOUT')
+ def _new_connection_timeout(_cls):
+ return timeout
+ dcl['connection_timeout'] = classmethod(_new_connection_timeout)
+
def _handle_callable(name, bases, dcl):
"""
Process the ``CALLABLE`` property of end-point command. Create the
@@ -597,7 +610,7 @@ class EndPointCommandMetaClass(abc.ABCMeta):
class SessionCommandMetaClass(EndPointCommandMetaClass):
"""
Meta class for commands operating upon a session object.
- All associated functions take as first argument an namespace abstraction
+ All associated functions take as first argument a namespace abstraction
of type ``lmi.shell``.
Handles following class properties:
@@ -607,10 +620,14 @@ class SessionCommandMetaClass(EndPointCommandMetaClass):
function. Defaults to ``"root/cimv2"``. If ``False``, raw
:py:class:`lmi.shell.LMIConnection` object will be passed to
associated function.
+ ``CONNECTION_TIMEOUT`` : ``int``
+ Maximum number of seconds to wait for broker's response. If reached,
+ :py:class:`~lmi.shell.LMIExceptions.ConnectionError` will be raised.
"""
def __new__(mcs, name, bases, dcl):
_handle_usage(name, bases, dcl)
_handle_namespace(dcl)
+ _handle_connection_timeout(dcl)
_handle_callable(name, bases, dcl)
return EndPointCommandMetaClass.__new__(mcs, name, bases, dcl)
diff --git a/cli/lmi/scripts/common/command/session.py
b/cli/lmi/scripts/common/command/session.py
index da22b4a..cbbf9fa 100644
--- a/cli/lmi/scripts/common/command/session.py
+++ b/cli/lmi/scripts/common/command/session.py
@@ -54,6 +54,16 @@ class LmiSessionCommand(LmiEndPointCommand):
return Configuration.get_instance().namespace
@classmethod
+ def connection_timeout(cls):
+ """
+ Returns maximum number of seconds the client shall wait for broker's
+ reply. Defaults to ``None`` which means that it's up to the connection
+ object itself to decide. It can be oveririden with
+ ``CONNECTION_TIMEOUT`` property in declaration of command.
+ """
+ return None
+
+ @classmethod
def dest_pos_args_count(cls):
"""
There is a namespace/connection object passed as the first positional
@@ -181,6 +191,13 @@ class LmiSessionCommand(LmiEndPointCommand):
raise TypeError("expected an instance of LMIConnection for"
" connection argument, not %s" % repr(connection))
namespace = self.cim_namespace()
+ timeout = self.connection_timeout()
+ if timeout is not None:
+ if hasattr(connection.connection, "timeout"):
+ LOG().debug("Set connection timeout to %d seconds.", timeout)
+ connection.timeout = timeout * 1000
+ else:
+ LOG().debug("Could not set connection timeout.")
if namespace is not None:
connection = LMIUtil.lmi_wrap_cim_namespace(
connection, namespace)
diff --git a/doc/src/scripts/devel/command-properties.rst
b/doc/src/scripts/devel/command-properties.rst
index 1a6ab11..c7dfd3d 100644
--- a/doc/src/scripts/devel/command-properties.rst
+++ b/doc/src/scripts/devel/command-properties.rst
@@ -234,6 +234,7 @@ Function invocation
Influencing properties:
* ``NAMESPACE`` (namespace_)
+ * ``CONNECTION_TIMEOUT`` (timeout_)
Property descriptions
~~~~~~~~~~~~~~~~~~~~~
@@ -256,11 +257,17 @@ Property descriptions
This usually won't need any modification. Sometimes perhaps associated
function might want to access more than one namespace, in that case an
- instance of :py:class:`lmi.shell.LMIConnection.LMIConnection` might prove more
useful.
+ instance of :py:class:`~lmi.shell.LMIConnection.LMIConnection` might prove more
useful.
Namespace can also be overriden globally in a configuration file or with
an option on command line.
+.. _timeout:
+
+``CONNECTION_TIMEOUT`` : ``int`` (defaults to ``None``)
+ Specifies maximum number of seconds we should wait for broker's reply. If
+ reached, :py:class:`~lmi.shell.LMIExceptions.ConnectionError` will be
+ raised. The default value is provided by underlying library [#]_.
Output rendering
----------------
@@ -565,6 +572,7 @@ method.
have nothing to do with arguments.
.. [#] Application object is accessible through ``app`` property of each
command instance.
+.. [#] *lmiwbem*'s default timeout is 1 minute as of now.
.. ****************************************************************************