Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit 205d130c1e0b5bbec9eef538818cd7c9d4e08df7
Author: Peter Hatina <phatina(a)redhat.com>
Date: Tue Jan 29 18:00:04 2013 +0100
introduce use_exceptions() switch
---------------------------------------------------------------
cli-tools/lmi/lmi_client_base.py | 32 ++++++++++++++++++++++++++++++++
cli-tools/lmi/lmi_client_shell.py | 26 +++++++++++++++++++++++---
cli-tools/lmishell | 8 +++++++-
3 files changed, 62 insertions(+), 4 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_base.py b/cli-tools/lmi/lmi_client_base.py
index ac6b66c..a850f77 100644
--- a/cli-tools/lmi/lmi_client_base.py
+++ b/cli-tools/lmi/lmi_client_base.py
@@ -13,11 +13,18 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <
http://www.gnu.org/licenses/>.
+import sys
import pywbem
import collections
_RValue = collections.namedtuple('RValue', ["rval",
"rparams", "errorstr"])
+def raise_or_dump_exception(e):
+ if not LmiBaseClient._get_use_exceptions():
+ return
+ (et, ei, tb) = sys.exc_info()
+ raise et, ei, tb
+
class LmiReturnValue(_RValue):
def __getattr__(self, name):
if not name in self._properties:
@@ -56,6 +63,7 @@ class LmiReturnValue(_RValue):
class LmiBaseClient(object):
QUERY_LANG_CQL = "CQL"
QUERY_LANG_WQL = "WQL"
+ use_exceptions_flag = False
def __init__(self, hostname, username = "", password = ""):
self._hostname = hostname
@@ -71,8 +79,10 @@ class LmiBaseClient(object):
try:
inst_name_list = self._cliconn.EnumerateInstanceNames(klass, **kwargs)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
except pywbem.cim_http.AuthError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[0])
return LmiReturnValue(rval = inst_name_list, rparams = {}, errorstr =
"")
@@ -107,6 +117,7 @@ class LmiBaseClient(object):
try:
inst = self._cliconn.GetInstance(InstanceName=i, LocalOnly = False)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
# we can get this error for eg. Linux_UnixProcess instances
# when, there the process exited earlier than this method
if e.args[0] == pywbem.CIM_ERR_NOT_FOUND:
@@ -124,6 +135,7 @@ class LmiBaseClient(object):
try:
self._cliconn.DeleteInstance(inst.path)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = False, rparams = {}, errorstr = e.args[1])
return LmiReturnValue(rval = True, rparams = {}, errorstr = "")
@@ -131,8 +143,10 @@ class LmiBaseClient(object):
try:
class_name_list = self._cliconn.EnumerateClassNames(namespace, **kwargs)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
except pywbem.cim_http.AuthError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[0])
return LmiReturnValue(rval = class_name_list, rparams = {}, errorstr =
"")
@@ -140,8 +154,10 @@ class LmiBaseClient(object):
try:
class_list = self._cliconn.EnumerateClasses(namespace, **kwargs)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
except pywbem.cim_http.AuthError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[0])
return LmiReturnValue(rval = class_list, rparams = {}, errorstr = "")
@@ -149,8 +165,10 @@ class LmiBaseClient(object):
try:
klass = self._cliconn.GetClass(class_name, namespace, **kwargs)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
except pywbem.cim_http.AuthError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[0])
return LmiReturnValue(rval = klass, rparams = {}, errorstr = "")
@@ -158,6 +176,7 @@ class LmiBaseClient(object):
try:
(rval, rparams) = self._cliconn.InvokeMethod(method, instance.path,
**params)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = -1, rparams = {}, errorstr = e.args[1] + ":
'" + method + "'")
return LmiReturnValue(rval = rval, rparams = rparams, errorstr = "")
@@ -169,18 +188,21 @@ class LmiBaseClient(object):
try:
return self._cliconn.Associators(instance.path, **params)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return []
def _get_references(self, instance, **params):
try:
return self._cliconn.References(instance.path, **params)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return []
def _modify_instance(self, instance, **params):
try:
self._cliconn.ModifyInstance(instance, **params)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = e.args[0], rparams = {}, errorstr = e.args[1])
return LmiReturnValue(rval = 0, rparams = {}, errorstr = "")
@@ -188,6 +210,7 @@ class LmiBaseClient(object):
try:
self._cliconn.DeleteInstance(instance, **params)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = e.args[0], rparams = {}, errorstr = e.args[1])
return LmiReturnValue(rval = 0, rparams = {}, errorstr = "")
@@ -195,9 +218,18 @@ class LmiBaseClient(object):
try:
inst_list = self._cliconn.ExecQuery(query_lang, query, namespace)
except pywbem.cim_operations.CIMError, e:
+ raise_or_dump_exception(e)
return LmiReturnValue(rval = None, rparams = {}, errorstr = e.args[1])
return LmiReturnValue(rval = inst_list, rparams = {}, errorstr = "")
+ @staticmethod
+ def _set_use_exceptions(use = True):
+ LmiBaseClient.use_exceptions_flag = use
+
+ @staticmethod
+ def _get_use_exceptions():
+ return LmiBaseClient.use_exceptions_flag
+
@property
def username(self):
return self._username
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py
index fb33926..eba4da5 100644
--- a/cli-tools/lmi/lmi_client_shell.py
+++ b/cli-tools/lmi/lmi_client_shell.py
@@ -24,6 +24,17 @@ from xml.dom.minicompat import NodeList
from lmi_client_base import LmiBaseClient
from lmi_client_base import LmiReturnValue
+class LmiDeletedObjectError(Exception):
+ """Raised, when there is an attempt to access properties
+ on deleted _LmiInstance object"""
+
+class LmiUnknownParameterError(Exception):
+ """Raised, when there is a method call issued and unknown
+ method parameter is provided"""
+
+class LmiMethodCallError(Exception):
+ """Raised, when an error occurs within method call"""
+
class _LmiTextFormatter(object):
def __init__(self, text):
self._text = text
@@ -385,13 +396,16 @@ def possibly_deleted(fn):
def wrapped(self = None, *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,
rparams = {},
- errorstr = "This instance has been deleted from a CIM broker")
+ errorstr = errorstr)
return wrapped
class _LmiInstance(BaseObject):
@@ -454,9 +468,11 @@ class _LmiInstance(BaseObject):
else:
# NOTE: maybe we could check for pywbem type
# and not to exit prematurely
+ errorstr = "Unknown parameter '%s' supplied for method
'%s'" % (param, self._method.name)
+ if LmiBaseClient._get_use_exceptions():
+ raise LmiUnknownParameterError(errorstr)
return LmiReturnValue(rval = -1, rparams = {},
- errorstr = "Unknown parameter '%s' supplied for
method '%s'"
- % (param, self._method.name), hostname =
self._client.hostname,
+ errorstr = errorstr, hostname = self._client.hostname,
use_display_hook = True)
(rval, rparams, errorstr) = self._client._call_method_raw(self._instance,
self._method.name, **kwargs)
@@ -468,11 +484,15 @@ class _LmiInstance(BaseObject):
rparams = self.__transform_return_values(rparams)
if not rparams:
# NOTE: this is wrong! What should we do?
+ if LmiBaseClient._get_use_exceptions():
+ raise LmiMethodCallError("Could not perform CIM -> LMI
object transformation")
return LmiReturnValue(rval = rval, rparams = {}, errorstr =
errorstr)
(new_inst, _, errorstr) = self._client._get_instance(
self._instance.path, LocalOnly = False)
if not new_inst:
# NOTE: this is wrong! What should we do?
+ if LmiBaseClient._get_use_exceptions():
+ raise LmiMethodCallError("Could not update an LMI object after a
method call")
return LmiReturnValue(rval = rval, rparams = {}, errorstr = errorstr)
self._instance._inst = new_inst
return LmiReturnValue(
diff --git a/cli-tools/lmishell b/cli-tools/lmishell
index 289b459..5093401 100755
--- a/cli-tools/lmishell
+++ b/cli-tools/lmishell
@@ -21,6 +21,7 @@ import readline
import rlcompleter
import __builtin__
from lmi.lmi_client_base import LmiReturnValue
+from lmi.lmi_client_base import LmiBaseClient
from lmi.lmi_client_shell import LmiConnection
from lmi.lmi_client_shell import _LmiNamespace
from lmi.lmi_client_shell import _LmiNamespaceRoot
@@ -170,6 +171,7 @@ class LmiInteractiveShell(code.InteractiveConsole):
locals["connect"] = _connect_interactive
locals["clear_history"] = self.clear_history
locals["use_display_sugar"] = _use_display_sugar
+ locals["use_exceptions"] = _use_exceptions
code.InteractiveConsole.__init__(self, locals)
self._completer = _LmiCompleter(self.locals)
self._history_path = os.path.expanduser("~/.lmi_shell_history")
@@ -263,6 +265,9 @@ def _connect(hostname, username = "", password =
"", interactive = False):
def _use_display_sugar(use = True):
sys.displayhook = _lmi_displayhook if use else sys.__displayhook__
+def _use_exceptions(use = True):
+ LmiBaseClient._set_use_exceptions(use)
+
if __name__ == "__main__":
options = _LmiShellOptions(sys.argv)
if not options.good:
@@ -279,7 +284,8 @@ if __name__ == "__main__":
else:
locals = {
"LmiConnection" : LmiConnection,
- "connect" : _connect_noninteractive
+ "connect" : _connect_noninteractive,
+ "use_exceptions" : _use_exceptions
}
sys.argv = options.script_args
try: