def _filter_instance(instance, cim_class, IncludeQualifiers, IncludeClassOrigin, PropertyList): if not IncludeQualifiers: instance.qualifiers = pywbem.NocaseDict() rmprops = [] for prop_name, prop in instance.properties.iteritems(): if PropertyList is not None and prop_name not in PropertyList: rmprops.append(prop_name) continue # Ensure the property is in the class if prop_name not in cim_class.properties: rmprops.append(prop_name) continue if IncludeQualifiers: prop.qualifiers = cim_class.properties[prop_name].qualifiers else: prop.qualifiers = pywbem.NocaseDict() if IncludeClassOrigin: prop.class_origin = cim_class.properties[prop_name].class_origin else: prop.class_origin = None if rmprops: for pname in rmprops: del instance.properties[pname] return instance
def inames_equal(op1, op2): """ Compares 2 object paths for equality. host attribute is ignored. """ if (not isinstance(op1, pywbem.CIMInstanceName) or not isinstance(op2, pywbem.CIMInstanceName) or op1.classname != op2.classname or op1.namespace != op2.namespace or (pywbem.NocaseDict(op1.keybindings) != pywbem.NocaseDict( op2.keybindings))): return False return True
def CreateInstance(NewInstance): ipath = NewInstance.path if not ipath or not ipath.keybindings: raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER, 'No key values for instance') class_name = NewInstance.classname; namespace = ipath.namespace conn = _getdbconnection(namespace) try: theclass = GetClass(NewInstance.classname, namespace, LocalOnly=False, IncludeQualifiers=True, IncludeClassOrigin=True, Connection=conn) except pywbem.CIMError: conn.close(True) raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_CLASS, 'Class %s does not exist in namespace %s' \ % (NewInstance.classname, namespace)) try: # Convert instance name to string strkey = _make_key_string(NewInstance.path) cursor = conn.cursor() cursor.execute('select classname from Instances where strkey=?', (strkey,)) try: cursor.next() cursor.close(True) conn.close(True) raise pywbem.CIMError(pywbem.CIM_ERR_ALREADY_EXISTS) except StopIteration: pass NewInstance.qualifiers = pywbem.NocaseDict() for prop in NewInstance.properties.itervalues(): prop.qualifiers = pywbem.NocaseDict() prop.class_origin = None pd = _encode(NewInstance) cursor.execute('insert into Instances values(?,?,?);', (class_name, strkey, pd)) conn.close(True) return ipath except: conn.close(True) raise
def _filter_class(cim_class, IncludeQualifiers, IncludeClassOrigin, PropertyList): if not IncludeQualifiers: cim_class.qualifiers = pywbem.NocaseDict() rmprops = [] for prop_name, prop in cim_class.properties.iteritems(): if PropertyList is not None and prop_name not in PropertyList: rmprops.append(prop_name) continue if not IncludeQualifiers: prop.qualifiers = pywbem.NocaseDict() if not IncludeClassOrigin: prop.class_origin = None if rmprops: for pname in rmprops: del cim_class.properties[pname] for meth in cim_class.methods.itervalues(): if not IncludeQualifiers: meth.qualifiers = pywbem.NocaseDict() if not IncludeClassOrigin: meth.class_origin = None return cim_class
print("nameSpace=" + nameSpace + " className=" + className + ". Caught:" + str(exc)) exit(0) print("<br>nameSpace=" + nameSpace + " className=" + className + "<br>") try: inst_names = conn.EnumerateInstanceNames(ClassName=className, namespace=nameSpace) except Exception: exc = sys.exc_info()[1] print("EnumerateInstanceNames nameSpace=" + nameSpace + " className=" + className + ". Caught:" + str(exc) + "<br>") exit(0) iname_dict = pywbem.NocaseDict() for iname in inst_names: if iname.classname not in iname_dict: iname_dict[iname.classname] = [iname] else: iname_dict[iname.classname].append(iname) print("Namespace:" + wbem_utils.HrefNamespace(nameSpace, cgiUrl) + "<br") print("<br>Num instances=" + str(len(inst_names)) + "<br><br>") print("<table border='1'>") for iname in inst_names: # iname is a 'CIMInstanceName' object, made of several key-value pairs. print("<tr>") print("<td>" + iname.classname + "</td>")
def _adjust_child_class(child_class, parent_class): # Sync quals quals = pywbem.NocaseDict() # Make sure if 'association' in child_class.qualifiers and \ 'association' not in parent_class.qualifiers: raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER, 'Association class %s is derived from not Association class %s' % \ (child_class.classname, parent_class.classname)) is_association = 'association' in parent_class.qualifiers for child_qual_name, child_qual in child_class.qualifiers.iteritems(): # Is the child qualifier in the parent class if child_qual_name in parent_class.qualifiers: parent_qual = parent_class.qualifiers[child_qual_name] # If the value of the qual is not the same, we save it if the # qual is overridable if parent_qual.value != child_qual.value or \ parent_qual.tosubclass is False: if parent_qual.overridable is False: # None means True # Child can change this qualifier because it is not # overridable by subclasses raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER, 'Parent class qualifier %s does not have OVERRIDABLE ' 'flavor. Child cannot override it' % child_qual_name) quals[child_qual_name] = child_qual else: quals[child_qual_name] = child_qual child_class.qualifiers = quals # Sync properties parent_has_keys = None props = pywbem.NocaseDict() for child_prop_name, child_prop in child_class.properties.iteritems(): if child_prop_name not in parent_class.properties: # This property was introduced by the child class. # If ref and this is not an association, raise and exception if not is_association and child_prop.type == 'reference': raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER, 'Reference property an only be defined in an Association ' 'class') # If it is a key property, make sure no keys have been defined # in any superclass if 'key' in child_prop.qualifiers: if parent_has_keys is None: parent_has_keys = False for p in parent_class.properties.itervalues(): if 'key' in p.qualifiers: parent_has_keys = True break if parent_has_keys: raise pywbem.CIMError(pywbem.CIM_ERR_INVALID_PARAMETER, 'Parent class has keys. Child cannot define ' 'additional key properties. Child class: %s ' 'property name: %s' % \ (child_class.classname, child_prop.name)) child_prop.class_origin = child_class.classname child_prop.propagated = False props[child_prop_name] = child_prop continue # This property is in the parent and child class parent_prop = parent_class.properties[child_prop_name] # If child did not override this property, then don't # save it unless it has a different default value if 'override' not in child_prop.qualifiers: if child_prop.value: if parent_prop.value and child_prop.value != parent_prop.value: child_prop.propagated = True child_prop.class_origin = parent_prop.class_origin props[child_prop_name] = parent_prop props[child_prop_name].value = child_prop.value continue # Child as specified 'override' on this property # Turn off 'tosubclass' for the override qual so children of # this class don't automatically get it (They have to # explicitly specify 'override' child_prop.qualifiers['override'].tosubclass = False if not child_prop.class_origin: child_prop.class_origin = child_class.classname child_prop.propagated = False else: child_prop.class_origin = parent_class.classname child_prop.propagated = True # Make sure the quals are set on the overridden property for parent_prop_qual_name, parent_prop_qual in \ parent_prop.qualifiers.iteritems(): if not parent_prop_qual.overridable: if parent_prop_qual_name not in child_prop.qualifiers: child_prop.qualifiers[parent_prop_qual_name] = \ parent_prop_qual else: # Qual is not overridable but it is on the child # property. Probably need to tell the caller that # this condition exists, but we're going to # silently set it to the parent's qual child_prop.qualifiers[parent_prop_qual_name] = \ parent_prop_qual # Parent does allow override of qualifier. # If the qual has tosubclass (not restricted), then only # propagate it down if it's not overridden in the subclass elif parent_prop_qual.tosubclass: if parent_prop_qual_name not in child_prop.qualifiers: child_prop.qualifiers[parent_prop_qual_name] = \ parent_prop_qual props[child_prop_name] = child_prop child_class.properties = props # properties now sync'ed. Now sync methods # TODO. Should do more validation here meths = pywbem.NocaseDict() for child_meth_name, child_method in child_class.methods.iteritems(): if child_meth_name not in parent_class.methods: child_method.propagated = False child_method.class_origin = child_class.classname meths[child_meth_name] = child_method continue if child_method.qualifiers.has_key('override'): child_method.propagated = True child_method.class_origin = parent_class.classname child_class.methods = meths return child_class
import unittest import pywbem from pywbem_yawn.inputparse import iname_str2pywbem props = pywbem.NocaseDict({ "Uint16Property" : { 'name':'Uint16Property', 'type':'uint16' , 'is_key':True }, "StringProperty" : { 'name':'StringProperty', 'type':'string' , 'is_key':True }, "BooleanProperty": { 'name':'BooleanProperty', 'type':'boolean' , 'is_key':True }, "Sint32Property" : { 'name':'Sint32Property', 'type':'sint32' , 'is_key':True } }) class InputParseTest(unittest.TestCase): def test_iname_str2pywbem_correct_path1(self): path = iname_str2pywbem(props, """root/cimv2:LMI_YawnTest.StringProperty="short string",""" """Uint16Property=10,BooleanProperty=TRUE""") self.assertIsInstance(path, pywbem.CIMInstanceName) self.assertEqual("root/cimv2", path.namespace) self.assertEqual("LMI_YawnTest", path.classname) self.assertIsInstance(path.keybindings, (dict, pywbem.NocaseDict)) self.assertEqual(3, len(path.keybindings)) self.assertIn("StringProperty", path) self.assertIn("Uint16Property", path) self.assertIn("BooleanProperty", path) self.assertEqual("short string", path["StringProperty"]) self.assertIsInstance(path['Uint16Property'], pywbem.Uint16) self.assertEqual(pywbem.Uint16(10), path['Uint16Property'])