Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit d69f4aa4fda9f39021fad4940bf3b1d1f6e1a50e
Author: Peter Hatina <phatina(a)redhat.com>
Date: Fri Jul 19 12:04:19 2013 +0200
lmi decorators, code cleanup
---------------------------------------------------------------
cli-tools/lmi/lmi_client_shell.py | 277 ++++++++++++++++++++++++-------------
1 files changed, 183 insertions(+), 94 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index 84fa854..cff35f2 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -624,17 +624,26 @@ class _LmiSubscription(BaseObject):
if self._cim_handler_tpl[1]:
self._client._delete_instance(self._cim_handler_tpl[0].path)
-class lmi_return_val_if_fail(object):
+class lmi_return_expr_if_fail(object):
"""
- Decorator, which returns a specified value, if a object member is of expression
False.
+ Decorator, which calls a specified expression and returns its return value instead
of
+ calling the decorated method, if provided test expression is False; otherwise a
method
+ is called.
Example of usage:
class Foo:
def __init__(self, member):
self._member = member
- @lmi_return_val_if_fail("_member", False)
+ def failed(self):
+ print "expression failed"
+ return False
+
+ # NOTE: the self parameter to the method call needs to be passed via
expr_ret_args,
+ # therefore, there is a dummy lambda obj: obj, which is basically self
variable.
+ @lmi_return_expr_if_fail(lambda obj: obj._member, failed, lambda obj: obj)
def some_method(self):
+ print "some_method called"
return True
f = Foo(None)
@@ -643,19 +652,21 @@ class lmi_return_val_if_fail(object):
f = Foo(True)
f.some_method() == True
"""
- def __init__(self, attr, rval):
+ def __init__(self, expr_test, expr_ret, *expr_ret_args, **expr_ret_kwargs):
"""
- Constructs a lmi_return_val_if_fail decorator object.
+ Constructs a lmi_return_expr_if_fail decorator object.
Arguments:
- attr -- the object attribute, which will be tested before the call to be
- of expression True
- rval -- return value of the method, if the object attribute as expression
- failed
+ expr_test -- expression which determines, if to execute a return
+ value expression
+ expr_ret -- expression, which is called, if the expr_test returns False
+ expr_ret_args -- expr_ret position arguments
+ expr_ret_kwargs -- expr_ret keyword arguments
"""
- self._attr = attr
- self._rval = rval
- self._ret = True
+ self._expr_test = expr_test
+ self._expr_ret = expr_ret
+ self._expr_ret_args = expr_ret_args
+ self._expr_ret_kwargs = expr_ret_kwargs
def __call__(self, fn):
"""
@@ -665,35 +676,128 @@ class lmi_return_val_if_fail(object):
fn -- decorated method
"""
def wrapper(self_wr, *args, **kwargs):
- attrs = self._attr.split(".")
- obj = self_wr
- for a in attrs:
- try:
- obj = getattr(obj, a)
- except AttributeError, e:
- obj = None
- if not obj:
- if self._ret:
- return self._rval
- else:
- return
+ failed = False
+ try:
+ if not self._expr_test(self_wr):
+ failed = True
+ except AttributeError, e:
+ failed = True
+ if failed:
+ return self._expr_ret(*self._expr_ret_args, **self._expr_ret_kwargs)
return fn(self_wr, *args, **kwargs)
return wrapper
+class lmi_return_val_if_fail(lmi_return_expr_if_fail):
+ """
+ Decorator, which returns a specified value and no method call is performed, if
+ provided test expression is False; otherwise a method is called.
+
+ Example of usage:
+ class Foo:
+ def __init__(self, member):
+ self._member = member
+
+ @lmi_return_val_if_fail(lambda obj: obj._member, False)
+ def some_method(self):
+ print "some_method called"
+ return True
+
+ f = Foo(None)
+ f.some_method() == False
+
+ f = Foo(True)
+ f.some_method() == True
+ """
+ def __init__(self, expr_test, rval):
+ """
+ Constructs a lmi_return_val_if_fail object.
+
+ Arguments:
+ expr_test -- if the expression returns False, a method call is called
+ rval -- return value of the method, if the object attribute as expression
+ failed
+ """
+ lmi_return_expr_if_fail.__init__(self, expr_test, lambda: rval)
+
class lmi_return_if_fail(lmi_return_val_if_fail):
"""
- Decorator, which returns, if a object member is False.
+ Decorator, which returns None and no method call is performed, if provided test
+ expression is False; otherwise a method is called.
+
+ Example of usage:
+ class Foo:
+ def __init__(self, member):
+ self._member = member
+
+ @lmi_return_if_fail(lambda obj: obj._member)
+ def some_method(self):
+ print "some_method called"
+ return True
+
+ f = Foo(None)
+ f.some_method() == None
+
+ f = Foo(True)
+ f.some_method() == True
"""
- def __init__(self, attr):
+ def __init__(self, expr_test):
"""
Constructs a lmi_return_if_fail decorator object.
Arguments:
- attr -- the object attribute, which will be tested before the call to be
- of expression True
+ expr_test -- if the expression returns True, a method call is called
"""
- super(self.__class__, self).__init__(attr, None)
- self._ret = False
+ lmi_return_val_if_fail.__init__(self, expr_test, None)
+
+class lmi_possibly_deleted(lmi_return_val_if_fail):
+ """
+ Decorator, which returns None, if provided test expression is True.
+
+ Example of usage:
+ class Foo:
+ def __init__(self, deleted):
+ self._deleted = deleted
+
+ @lmi_possibly_deleted(lambda obj: obj._member, lambda: False)
+ def some_method(self):
+ print "some_method called"
+ return True
+
+ f = Foo(None)
+ f.some_method() == False
+
+ f = Foo(True)
+ f.some_method() == True
+ """
+ def __init__(self, rval):
+ """
+ Constructs a lmi_possibly_deleted object."
+
+ Arguments:
+ rval -- return value, if expr_test fails
+ """
+ lmi_return_val_if_fail.__init__(self, lambda obj: obj._deleted, rval)
+
+ def __call__(self, fn):
+ """
+ Executes a method call, if the test passed. If the test expression
+ returns False, specified return value in the constructor is returned.
+ If the shell does not dump exceptions, an appropriate exception is raised.
+
+ Raised exceptions:
+ LmiDeletedObjectError
+
+ Arguments:
+ fn -- decorated method
+ """
+ def wrapper(self_wr, *args, **kwargs):
+ if self._expr_test(self_wr):
+ errorstr = "This instance has been deleted from a CIM broker"
+ if LmiBaseClient._get_use_exceptions():
+ raise LmiDeletedObjectError(errorstr)
+ return self._expr_ret()
+ return fn(self_wr, *args, **kwargs)
+ return wrapper
class _LmiNamespace(BaseObject):
"""
@@ -1044,7 +1148,7 @@ class _LmiClass(BaseObject):
raise AttributeError(name)
@lmi_class_fetch_lazy
- @lmi_return_val_if_fail("_namespace", None)
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, None)
def create_instance(self, properties = None, qualifiers = None, property_list =
None):
"""
Creates a new CIMInstance at the server side and returns LmiReturn value
@@ -1074,7 +1178,7 @@ class _LmiClass(BaseObject):
return _LmiInstance(self._client, self, cim_instance)
@lmi_class_fetch_lazy
- @lmi_return_if_fail("_namespace")
+ @lmi_return_if_fail(lambda obj: obj._namespace)
def doc(self):
"""
Prints out pretty verbose message with documentation for the class. If the shell
@@ -1100,7 +1204,7 @@ class _LmiClass(BaseObject):
# NOTE: usage with Key=something, Value=something is deprecated
# NOTE: inst_filter is either None or dict
- @lmi_return_val_if_fail("_namespace", [])
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, [])
def instance_names(self, inst_filter = None, **kwargs):
"""
Returns a LmiReturnValue containing a list of _LmiInstanceNames.
@@ -1156,7 +1260,7 @@ class _LmiClass(BaseObject):
# NOTE: usage with Key=something, Value=something is deprecated
# NOTE: inst_filter is either None or dict
- @lmi_return_val_if_fail("_namespace", [])
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, [])
def instances(self, inst_filter = None, **kwargs):
"""
Returns a list of objects of _LmiInstance.
@@ -1202,7 +1306,7 @@ class _LmiClass(BaseObject):
return result
@lmi_class_fetch_lazy
- @lmi_return_val_if_fail("_namespace", [])
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, [])
def valuemap_properties(self):
"""
Returns a list of strings of the constant names.
@@ -1210,7 +1314,7 @@ class _LmiClass(BaseObject):
return self._valuemap_properties_list
@lmi_class_fetch_lazy
- @lmi_return_val_if_fail("_namespace", [])
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, [])
def properties(self):
"""
Returns a list of strings of the CIMClass properties.
@@ -1218,7 +1322,7 @@ class _LmiClass(BaseObject):
return self._cim_class.properties.keys()
@lmi_class_fetch_lazy
- @lmi_return_if_fail("_namespace")
+ @lmi_return_if_fail(lambda obj: obj._namespace)
def print_properties(self):
"""
Prints out the list of CIMClass properties.
@@ -1227,7 +1331,7 @@ class _LmiClass(BaseObject):
sys.stdout.write("%s\n" % prop)
@lmi_class_fetch_lazy
- @lmi_return_val_if_fail("_namespace", [])
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, [])
def methods(self):
"""
Returns a list of strings of CIMClass methods.
@@ -1235,7 +1339,7 @@ class _LmiClass(BaseObject):
return self._cim_class.methods.keys()
@lmi_class_fetch_lazy
- @lmi_return_if_fail("_namespace")
+ @lmi_return_if_fail(lambda obj: obj._namespace)
def print_methods(self):
"""
Prints out the list of CIMClass methods.
@@ -1251,7 +1355,7 @@ class _LmiClass(BaseObject):
return self._cim_classname
@property
- @lmi_return_val_if_fail("_namespace", "Unknown")
+ @lmi_return_val_if_fail(lambda obj: obj._namespace, "Unknown")
def namespace(self):
"""
Property returning a string of the namespace name.
@@ -1353,29 +1457,6 @@ class _LmiInstanceName(BaseObject):
"""
return self._cim_instance_name
-def possibly_deleted(fn):
- """
- Decorator, which disables a method call, if a CIMInstance was deleted from
- the server side. The decorator is applied, when a _LmiInstance.delete() was
- issued.
-
- Arguments:
- fn -- wrapped method
- """
- def wrapped(self, *args, **kwargs):
- if not self._deleted:
- return fn(self, *args, **kwargs)
- errorstr = "This instance has been deleted from a CIM broker"
- if LmiBaseClient._get_use_exceptions():
- raise LmiDeletedObjectError(errorstr)
- return LmiReturnValue(
- hostname = self._client.hostname,
- instance = self,
- use_display_hook = True,
- rval = None,
- errorstr = errorstr)
- return wrapped
-
class _LmiInstanceMethod(BaseObject):
"""
LMI wrapper class representing CIMMethod.
@@ -1844,7 +1925,7 @@ class _LmiInstance(BaseObject):
self.__dict__["_client"] = client
self.__dict__["_lmi_class"] = lmi_class
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def __getattr__(self, name):
"""
Returns either a class member, _LmiInstanceMethod object, or a CIMInstance
object
@@ -1872,7 +1953,7 @@ class _LmiInstance(BaseObject):
return member
raise AttributeError(name)
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def __setattr__(self, name, value):
"""
Modifies a CIMInstance property.
@@ -1890,7 +1971,7 @@ class _LmiInstance(BaseObject):
else:
self.__dict__[name] = value
- @possibly_deleted
+ @lmi_possibly_deleted("")
def __repr__(self):
"""
Returns a pretty string for the object.
@@ -1901,7 +1982,7 @@ class _LmiInstance(BaseObject):
return "%s(classname='%s', ...)" % (self.__class__.__name__,
self.classname)
@property
- @possibly_deleted
+ @lmi_possibly_deleted("")
def classname(self):
"""
Property returning a string of a class name.
@@ -1912,7 +1993,7 @@ class _LmiInstance(BaseObject):
return self._lmi_class.classname
@property
- @possibly_deleted
+ @lmi_possibly_deleted("")
def namespace(self):
"""
Property retuning a string of a namespace name.
@@ -1923,7 +2004,7 @@ class _LmiInstance(BaseObject):
return self._lmi_class.namespace
@property
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def path(self):
"""
Property returning a string of a CIMInstance path.
@@ -1933,8 +2014,8 @@ class _LmiInstance(BaseObject):
"""
return self._cim_instance.path
- @possibly_deleted
- @lmi_return_if_fail("_cim_instance.path")
+ @lmi_possibly_deleted(None)
+ @lmi_return_if_fail(lambda obj: obj._cim_instance.path)
def doc(self):
"""
Prints out pretty verbose message with documentation for the instance. If the
shell
@@ -1947,7 +2028,7 @@ class _LmiInstance(BaseObject):
"""
_LmiXmlFormatter(self._cim_instance.tocimxml()).fancy_format(self._client.interactive)
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def tomof(self):
"""
Prints out a message with MOF representation of CIMMethod. If the shell
@@ -1960,7 +2041,11 @@ class _LmiInstance(BaseObject):
"""
_LmiMofFormatter(self._cim_instance.tomof()).fancy_format(self._client.interactive)
- @possibly_deleted
+ @lmi_possibly_deleted( \
+ LmiReturnValue( \
+ rval = False, \
+ errorstr = "This instance has been deleted from a CIM broker") \
+ )
def refresh(self):
"""
Retreives a new CIMInstance object. Basically refreshes the object properties.
@@ -1976,8 +2061,8 @@ class _LmiInstance(BaseObject):
self._cim_instance = new_cim_instance
return LmiReturnValue(rval = True)
- @possibly_deleted
- @lmi_return_val_if_fail("_cim_instance.path", [])
+ @lmi_possibly_deleted([])
+ @lmi_return_val_if_fail(lambda obj: obj._cim_instance.path, [])
def associator_names(self, AssocClass = None, ResultClass = None, Role = None,\
ResultRole = None):
"""
@@ -2024,7 +2109,7 @@ class _LmiInstance(BaseObject):
result.append(_LmiInstanceName(self._client, i))
return result
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def first_associator_name(self, AssocClass = None, ResultClass = None, Role = None,
ResultRole = None):
"""
@@ -2061,8 +2146,8 @@ class _LmiInstance(BaseObject):
return None
return result[0]
- @possibly_deleted
- @lmi_return_val_if_fail("_cim_instance.path", [])
+ @lmi_possibly_deleted([])
+ @lmi_return_val_if_fail(lambda obj: obj._cim_instance.path, [])
def associators(self, AssocClass = None, ResultClass = None, Role = None,\
ResultRole = None, Props = None):
"""
@@ -2125,7 +2210,7 @@ class _LmiInstance(BaseObject):
result.append(lmi_assoc_inst)
return result
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def first_associator(self, AssocClass = None, ResultClass = None, Role = None, \
ResultRole = None, Props = None):
"""
@@ -2174,8 +2259,8 @@ class _LmiInstance(BaseObject):
return None
return result[0]
- @possibly_deleted
- @lmi_return_val_if_fail("_cim_instance.path", [])
+ @lmi_possibly_deleted([])
+ @lmi_return_val_if_fail(lambda obj: obj._cim_instance.path, [])
def reference_names(self, ResultClass = None, Role = None):
"""
Returns a list of association _LmiInstanceName objects with this object.
@@ -2205,7 +2290,7 @@ class _LmiInstance(BaseObject):
result.append(_LmiInstanceName(self._client, i))
return result
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def first_reference_name(self, ResultClass = None, Role = None):
"""
Returns the first association _LmiInstanceName with this object.
@@ -2228,8 +2313,8 @@ class _LmiInstance(BaseObject):
return None
return result[0]
- @possibly_deleted
- @lmi_return_val_if_fail("_cim_instance.path", [])
+ @lmi_possibly_deleted([])
+ @lmi_return_val_if_fail(lambda obj: obj._cim_instance.path, [])
def references(self, ResultClass = None, Role = None, Props = None):
"""
Returns a list of association _LmiInstance objects with this object.
@@ -2274,7 +2359,7 @@ class _LmiInstance(BaseObject):
result.append(lmi_references_inst)
return result
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def first_reference(self, ResultClass = None, Role = None, Props = None):
"""
Returns the first association _LmiInstance with this object.
@@ -2300,7 +2385,7 @@ class _LmiInstance(BaseObject):
return None
return result[0]
- @possibly_deleted
+ @lmi_possibly_deleted([])
def properties(self):
"""
Returns a list of CIMInstance properties.
@@ -2310,7 +2395,7 @@ class _LmiInstance(BaseObject):
"""
return self._cim_instance.properties.keys()
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def print_properties(self):
"""
Prints out the list of CIMInstance properties.
@@ -2321,7 +2406,7 @@ class _LmiInstance(BaseObject):
for (name, prop) in self._cim_instance.properties.iteritems():
sys.stdout.write("%s\n" % name)
- @possibly_deleted
+ @lmi_possibly_deleted({})
def properties_dict(self):
"""
Returns dictionary containing property name and value pairs.
@@ -2333,7 +2418,7 @@ class _LmiInstance(BaseObject):
return dict(zip(self._cim_instance.properties.keys(),
map(lambda x: x.value, self._cim_instance.properties.values())))
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def property_value(self, prop_name):
"""
Returns a CIMInstance property value.
@@ -2346,7 +2431,7 @@ class _LmiInstance(BaseObject):
"""
return getattr(self, prop_name)
- @possibly_deleted
+ @lmi_possibly_deleted([])
def methods(self):
"""
Returns a list of CIMInstance methods' names.
@@ -2356,7 +2441,7 @@ class _LmiInstance(BaseObject):
"""
return self._lmi_class.methods()
- @possibly_deleted
+ @lmi_possibly_deleted(None)
def print_methods(self):
"""
Prints out the list of CIMInstance methods' names.
@@ -2366,7 +2451,11 @@ class _LmiInstance(BaseObject):
"""
self._lmi_class.print_methods()
- @possibly_deleted
+ @lmi_possibly_deleted( \
+ LmiReturnValue( \
+ rval = False, \
+ errorstr = "This instance has been deleted from a CIM broker") \
+ )
def push(self):
"""
Pushes the modified object to the CIMOM. Returns LmiReturnValue with rval set to
@@ -2392,8 +2481,8 @@ class _LmiInstance(BaseObject):
rparams = return_value.rparams,
errorstr = return_value.errorstr)
- @possibly_deleted
- @lmi_return_if_fail("_lmi_class._namespace")
+ @lmi_possibly_deleted(None)
+ @lmi_return_if_fail(lambda obj: obj._lmi_class._namespace)
def delete(self):
"""
Deletes this instance from the CIMOM.