From 95b689c6c4a9a4eb130a4f80f21d3ceea7468f44 Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Thu, 2 May 2013 11:00:44 +0200 Subject: Added global function to parse InstanceID. --- src/python/openlmi/common/JobManager.py | 36 ++++----------------------------- src/python/openlmi/common/__init__.py | 18 +++++++++++++++++ 2 files changed, 22 insertions(+), 32 deletions(-) diff --git a/src/python/openlmi/common/JobManager.py b/src/python/openlmi/common/JobManager.py index e78967c..3a845fd 100644 --- a/src/python/openlmi/common/JobManager.py +++ b/src/python/openlmi/common/JobManager.py @@ -47,6 +47,7 @@ import threading from Queue import Queue import pywbem import openlmi.common.cmpi_logging as cmpi_logging +import openlmi.common from openlmi.common.IndicationManager import IndicationManager from pywbem.cim_provider2 import CIMProvider2 import socket @@ -464,34 +465,6 @@ class Job(object): classname = self.job_manager.job_classname return 'LMI:' + classname + ':' + str(self.the_id) - @staticmethod - def parse_instance_id(instance_id, job_manager, classname=None): - """ - Return the last part of instance_id. - - :param instance_id: (``string``) InstanceID to parse. - :param job_manager: (``JobManager``) JobManager to query for Job's - classname. - :param classname: (``string``) Optional classname. If not given, - JobManager's job_classname will be used for parsing. Other - classnames may be used to parse e.g. LMI_MethodResult - InstanceIDs. - - :rtype: ``string`` or None if the ``instance_id`` has wrong format. - """ - if classname is None: - classname = job_manager.job_classname - parts = instance_id.split(":") - if len(parts) != 3: - return None - if parts[0] != 'LMI': - return None - if parts[1] != classname: - return None - if not parts[2].isdigit(): - return None - return parts[2] - @cmpi_logging.trace_method def get_pre_call(self): """ @@ -871,11 +844,10 @@ class JobManager(object): """ if classname is None: classname = self.job_classname - the_id = Job.parse_instance_id(instance_id, self, classname) - if the_id: - return self.jobs.get(the_id, None) - else: + the_id = openlmi.common.parse_instance_id(instance_id, classname) + if not the_id.isdigit(): return None + return self.jobs.get(the_id, None) @cmpi_logging.trace_method def _worker_main(self): diff --git a/src/python/openlmi/common/__init__.py b/src/python/openlmi/common/__init__.py index 2d19515..baebcdb 100644 --- a/src/python/openlmi/common/__init__.py +++ b/src/python/openlmi/common/__init__.py @@ -22,3 +22,21 @@ """ Common utilities for OpenLMI python providers. """ +def parse_instance_id(instance_id, classname=None): + """ + Parse InstanceID, check it has LMI:: format and return + the ID. Return None if the format is bad. + :param instance_id: (``string``) String to parse. + :param classname: (``string``) Name of class, whose InstanceID we parse. + If the classname is None, it won't be checked. + :returns: ``string`` with the ID. + """ + parts = instance_id.split(":", 2) + if len(parts) != 3: + return None + if parts[0] != "LMI": + return None + real_classname = parts[1] + if classname and real_classname.lower() != classname.lower(): + return None + return parts[2] -- cgit