def decode(representation, version): """Decodes a CIM instance from passed xml representation. Keyword arguments: representation -- an xml representation of a CIM instance (i.e. either a filepath, a url or an etree element). """ # Defensive programming. if representation is None: raise CIMException("Cannot decode null representations.") if version not in CIM_SCHEMAS: raise CIMException("{0} is an unsupported CIM version.".format(version)) # Deserialize xml & associated namespaces. xml, nsmap = get_cim_xml(representation, return_nsmap=True) # Validate xml. if _can_decode_from_xml(xml) == False: raise CIMException("Representation failed CIM XML validation.") # Get cim type. type = xml.tag.split("}")[1] # Get decoder by type. if type not in _decoders[version]: raise CIMException("No decoder exists for the following CIM type :: {0}.".format(type)) decoder = _decoders[version][type] # Return decoded instance injected with type information. obj = decode_xml(decoder, xml, nsmap, None) _set_cim_type_info(obj, version, type) return obj
def _can_decode_from_xml(xml): """Determines a priori whether xml is decodeable. Keyword Arguments: xml - xml representation of a CIM instance. """ # Deserialize xml (if necessary). if isinstance(xml, et._Element) == False: xml = get_cim_xml(xml) # False if representation has not been converted to an lxml.etree. if isinstance(xml, et._Element) == False: return False # False if representation is an invliad cim xml instance. # TODO - implement via call to CIM validator component. # All tests have passed therefore return true. return True