Repository : http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
commit 6d8f1fd7f8a161cd457352f74d9dcf1a7be08e34 Author: Peter Hatina phatina@redhat.com Date: Wed Apr 3 16:02:48 2013 +0200
move indication routines to LmiConnection class
cli-tools/lmi/lmi_client_shell.py | 205 +++++++++++++++++++------------------ cli-tools/lmishell | 12 +- 2 files changed, 111 insertions(+), 106 deletions(-)
diff --git a/cli-tools/lmi/lmi_client_shell.py b/cli-tools/lmi/lmi_client_shell.py index 2f4b778..fe17097 100644 --- a/cli-tools/lmi/lmi_client_shell.py +++ b/cli-tools/lmi/lmi_client_shell.py @@ -74,42 +74,14 @@ def cast_lmi_to_cim(t, value): return tuple(map(cast_func, value)) return cast_func(value)
-# Register _lmi_indications_cleanup at script exit, -# so we drop all the indication subscriptions for now -def _lmi_indications_cleanup(): - global _lmi_indications - for subscription in _lmi_indications.values(): - subscription.delete() - _lmi_indications = {} - def _lmi_indication_listeners_cleanup(): global _lmi_indication_listeners for listener in _lmi_indication_listeners.values(): listener.stop() _lmi_indication_listeners = {}
-atexit.register(_lmi_indications_cleanup) atexit.register(_lmi_indication_listeners_cleanup)
-def _lmi_delete_indication(indication_name): - if not indication_name in _lmi_indications: - errorstr = "No such indication subscribed" - if LmiBaseClient._get_use_exceptions(): - raise LmiIndicationError(errorstr) - return LmiReturnValue( - use_display_hook = True, - rval = False, - errorstr = errorstr) - indication = _lmi_indications[indication_name] - indication.delete() - _lmi_indications.pop(indication_name) - return LmiReturnValue( - hostname = indication._client.hostname, - use_display_hook = True, - rval = True, - rparams = {}, - errorstr = "") - def _lmi_start_indication_listener(hostname, port, handler, certfile = None, keyfile = None): global _lmi_indication_listeners listener = LmiIndicationListener( hostname, port, handler, certfile, keyfile) @@ -145,19 +117,10 @@ def _lmi_stop_indication_listener(hostname, port): use_display_hook = True, rval = True)
-def _lmi_indications_list(): - global _lmi_indications - return _lmi_indications.keys() - def _lmi_indication_listeners_list(): global _lmi_indication_listeners return _lmi_indication_listeners.keys()
-def _lmi_print_indications(): - global _lmi_indications - for i in _lmi_indications.keys(): - sys.stdout.write("%s\n" % i) - def _lmi_print_indication_listeners(): global _lmi_indication_listeners for i in _lmi_indication_listeners: @@ -380,8 +343,8 @@ class BaseObject(object): class _LmiSubscription(BaseObject): def __init__(self, client, cim_filter, cim_handler, cim_subscription): self._client = client - self._cim_filter = cim_filter - self._cim_handler = cim_handler + self._cim_filter_tpl = cim_filter + self._cim_handler_tpl = cim_handler self._cim_subscription = cim_subscription
def __lt__(self, other): @@ -389,8 +352,10 @@ class _LmiSubscription(BaseObject):
def delete(self): self._client._delete_instance(self._cim_subscription.path) - self._client._delete_instance(self._cim_filter.path) - self._client._delete_instance(self._cim_handler.path) + if self._cim_filter_tpl[1]: + self._client._delete_instance(self._cim_filter_tpl[0].path) + if self._cim_handler_tpl[1]: + self._client._delete_instance(self._cim_handler_tpl[0].path)
class _LmiNamespace(BaseObject): def __init__(self, client, name): @@ -451,65 +416,6 @@ class _LmiNamespace(BaseObject): return [] return self.__transform_cim_to_lmi(inst_list)
- def create_indication(self, **kwargs): - _return_value = lambda rval, errorstr = "": LmiReturnValue( - hostname = self._client.hostname, - use_display_hook = True, - rval = rval, - errorstr = errorstr) - try: - global _lmi_indications - system_name = self._client.hostname - filter_props = { - "CreationClassName" : kwargs["FilterCreationClassName"], - "SystemCreationClassName" : kwargs["FilterSystemCreationClassName"], - "SourceNamespace" : kwargs["FilterSourceNamespace"], - "SystemName" : system_name, - "Query" : kwargs["Query"], - "QueryLanguage" : kwargs["QueryLanguage"], - "Name" : kwargs["Name"] + "-filter" - } - handler_props = { - "CreationClassName" : kwargs["HandlerCreationClassName"], - "SystemCreationClassName" : kwargs["HandlerSystemCreationClassName"], - "SystemName" : system_name, - "Destination" : kwargs["Destination"], - "Name" : kwargs["Name"] + "-handler" - } - (cim_filter, _, errorstr) = self._client._create_instance( - "CIM_IndicationFilter", self._name, filter_props) - if not cim_filter: - if LmiBaseClient._get_use_exceptions(): - raise LmiIndicationError(errorstr) - return _return_value(False, errorstr) - (cim_handler, _, errorstr) = self._client._create_instance( - "CIM_IndicationHandlerCIMXML", self._name, handler_props) - if not cim_handler: - self._client._delete_instance(cim_filter.path) - if LmiBaseClient._get_use_exceptions(): - raise LmiIndicationError(errorstr) - return _return_value(False, errorstr) - subscription_props = { - "Filter" : cim_filter.path, - "Handler" : cim_handler.path - } - (cim_subscription, _, errorstr) = self._client._create_instance( - "CIM_IndicationSubscription", self._name, subscription_props) - if not cim_subscription: - self._client._delete_instance(cim_filter.path) - self._client._delete_instance(cim_handler.path) - if LmiBaseClient._get_use_exceptions(): - raise LmiIndicationError(errorstr) - return _return_value(False, errorstr) - _lmi_indications[kwargs["Name"]] = _LmiSubscription(self._client, - cim_filter, cim_handler, cim_subscription) - except KeyError, e: - errorstr = "Not all necessary parameters supported" - if LmiBaseClient._get_use_exceptions(): - raise LmiIndicationError(errorstr) - return _return_value(False, errorstr) - return _return_value(True, "") - @property def name(self): return self._name @@ -1042,6 +948,7 @@ class LmiConnection(object): def __init__(self, hostname, username = "", password = "", interactive = False, use_cache = True, \ conn_type = LmiBaseClient.CONN_TYPE_WBEM): self._client = _LmiShellClient(hostname, username, password, interactive, use_cache, conn_type) + self._indications = {}
@property def hostname(self): @@ -1080,3 +987,101 @@ class LmiConnection(object): finally: LmiBaseClient._set_use_exceptions(use_exceptions) return True + + def subscribe_indication(self, **kwargs): + _return_value = lambda rval, errorstr = "": LmiReturnValue( + hostname = self._client.hostname, + use_display_hook = True, + rval = rval, + errorstr = errorstr) + try: + cim_filter_provided = "Filter" in kwargs + if cim_filter_provided: + cim_filter = kwargs["Filter"]._cim_instance + else: + cim_filter_props = { + "CreationClassName" : kwargs["FilterCreationClassName"], + "SystemCreationClassName" : kwargs["FilterSystemCreationClassName"], + "SourceNamespace" : kwargs["FilterSourceNamespace"], + "SystemName" : self._client.hostname, + "Query" : kwargs["Query"], + "QueryLanguage" : kwargs["QueryLanguage"], + "Name" : kwargs["Name"] + "-filter" + } + (cim_filter, _, errorstr) = self._client._create_instance( + kwargs["FilterCreationClassName"], kwargs["CreationNamespace"], + cim_filter_props) + if not cim_filter: + if LmiBaseClient._get_use_exceptions(): + raise LmiIndicationError(errorstr) + return _return_value(False, errorstr) + cim_handler_provided = "Handler" in kwargs + if cim_handler_provided: + cim_handler = kwargs["Handler"]._cim_instance + else: + cim_handler_props = { + "CreationClassName" : kwargs["HandlerCreationClassName"], + "SystemCreationClassName" : kwargs["HandlerSystemCreationClassName"], + "SystemName" : self._client.hostname, + "Destination" : kwargs["Destination"], + "Name" : kwargs["Name"] + "-handler" + } + (cim_handler, _, errorstr) = self._client._create_instance( + kwargs["HandlerCreationClassName"], kwargs["CreationNamespace"], + cim_handler_props) + if not cim_handler: + if not "Filter" in kwargs: + self._client._delete_instance(cim_filter.path) + if LmiBaseClient._get_use_exceptions(): + raise LmiIndicationError(errorstr) + return _return_value(False, errorstr) + cim_subscription_props = { + "Filter" : cim_filter.path, + "Handler" : cim_handler.path + } + (cim_subscription, _, errorstr) = self._client._create_instance( + kwargs["SubscriptionCreationClassName"], kwargs["CreationNamespace"], + cim_subscription_props) + if not cim_subscription: + if not "Filter" in kwargs: + self._client._delete_instance(cim_filter.path) + if not "Handler" in kwargs: + self._client._delete_instance(cim_handler.path) + if LmiBaseClient._get_use_exceptions(): + raise LmiIndicationError(errorstr) + return _return_value(False, errorstr) + self._indications[kwargs["Name"]] = _LmiSubscription( + self._client, + (cim_filter, not cim_filter_provided), + (cim_handler, not cim_handler_provided), + cim_subscription) + except KeyError, e: + errorstr = "Not all necessary parameters provided" + if LmiBaseClient._get_use_exceptions(): + raise LmiIndicationError(errorstr) + return _return_value(False, errorstr) + return _return_value(True) + + def unsubscribe_indication(self, name): + if not name in self._indications: + errorstr = "No such indication" + if LmiBaseClient._get_use_exceptions(): + raise LmiIndicationError(errorstr) + return LmiReturnValue(rval = False, errorstr = errorstr) + indication = self._indications.pop(name) + indication.delete() + return LmiReturnValue(rval = True, + use_display_hook = True, + hostname = self._client.hostname) + + def unsubscribe_all_indications(self): + for i in self._indications.values(): + i.delete() + self._indications = {} + + def print_subscribed_indications(self): + for i in self._indications.keys(): + sys.stdout.write("%s\n" % i) + + def subscribed_indications(self): + return self._indications.keys() diff --git a/cli-tools/lmishell b/cli-tools/lmishell index 89844d8..74f4728 100755 --- a/cli-tools/lmishell +++ b/cli-tools/lmishell @@ -31,12 +31,9 @@ from lmi.lmi_client_shell import _LmiNamespaceRoot from lmi.lmi_client_shell import _LmiClass from lmi.lmi_client_shell import _LmiInstance from lmi.lmi_client_shell import _LmiInstanceName -from lmi.lmi_client_shell import _lmi_delete_indication from lmi.lmi_client_shell import _lmi_start_indication_listener from lmi.lmi_client_shell import _lmi_stop_indication_listener -from lmi.lmi_client_shell import _lmi_print_indications from lmi.lmi_client_shell import _lmi_print_indication_listeners -from lmi.lmi_client_shell import _lmi_indications_list from lmi.lmi_client_shell import _lmi_indication_listeners_list
def get_item_or_default(dic, item, default): @@ -219,12 +216,9 @@ class LmiInteractiveShell(code.InteractiveConsole): DEFAULT_LOCALS = { "LmiConnection" : LmiConnection, "use_exceptions" : LmiBaseClient._set_use_exceptions, - "delete_indication" : _lmi_delete_indication, "start_indication_listener" : _lmi_start_indication_listener, "stop_indication_listener" : _lmi_stop_indication_listener, - "print_indications" : _lmi_print_indications, "print_indication_listeners" : _lmi_print_indication_listeners, - "indications" : _lmi_indications_list, "indication_listeners" : _lmi_indication_listeners_list }
@@ -284,6 +278,11 @@ class LmiInteractiveShell(code.InteractiveConsole): more = 0 self.save_history()
+ def cleanup(self): + for (k, v) in self.locals.iteritems(): + if isinstance(v, LmiConnection): + v.unsubscribe_all_indications() + def load_history(self): if self._history_length == 0 or not os.path.exists(self._history_file): return @@ -331,6 +330,7 @@ def __lmi_raw_input(prompt, use_echo = True): def _lmi_interact(locals = None): console = LmiInteractiveShell("> ", "... ", locals) console.interact() + console.cleanup()
def _connect(hostname, username = "", password = "", interactive = False, use_cache = True): connection = None