def decode(as_xml): """Decodes a document from an xml string. :param as_xml: Document xml representation. :type as_xml: unicode | str | ET.Element :returns: A pyesdoc document instance. :rtype: object """ # Convert to etree. if isinstance(as_xml, unicode): as_xml = ET.fromstring(convert.unicode_to_str(as_xml)) elif isinstance(as_xml, str): as_xml = ET.fromstring(as_xml) if not isinstance(as_xml, ET.Element): raise TypeError("Cannot decode non xml documents") # Get document type key. doc_type_key = as_xml.find('meta/type').text # Get document type. doc_type = ontologies.get_type_from_key(doc_type_key) if doc_type is None: raise ValueError('meta.type key cannot be mapped to an ontology type.') return _decode(as_xml, doc_type, False)
def decode(as_xml): """Decodes a document from an xml string. :param as_xml: Document xml representation. :type as_xml: unicode | str | ET.Element :returns: A pyesdoc document instance. :rtype: object """ # Convert to etree. if isinstance(as_xml, unicode): as_xml = ET.fromstring(convert.unicode_to_str(as_xml)) elif isinstance(as_xml, str): as_xml = ET.fromstring(as_xml) if not isinstance(as_xml, ET.Element): raise TypeError("Cannot decode non xml documents") # Get document type key. doc_type_key = as_xml.find("meta/type").text # Get document type. doc_type = ontologies.get_type_from_key(doc_type_key) if doc_type is None: raise ValueError("meta.type key cannot be mapped to an ontology type.") return _decode(as_xml, doc_type, False)
def _create_doc(obj, doc_type): """Creates & returns a document to be hydrated from a dictionary. """ try: obj['meta'] # ... only DocMetaInfo objects do not have a meta section. except KeyError: doc_type = ontologies.get_type_from_key('cim.2.shared.DocMetaInfo') else: if ontologies.get_type_key(doc_type) != obj['meta']['type']: doc_type = ontologies.get_type_from_key(obj['meta']['type']) finally: if doc_type is None: raise ValueError('Target decoding type is unrecognized') return doc_type(), ontologies.get_decoder_info(doc_type)
def _get_doc(obj, doc_type): """Returns document. """ if ontologies.get_type_key(doc_type) != obj['ontology_type_key']: doc_type = ontologies.get_type_from_key(obj['ontology_type_key']) if doc_type is None: raise ValueError('Decoding type is unrecognized') return doc_type(), ontologies.get_decoder_info(doc_type)
def _create_doc(elem, doc_type): """Creates & returns a document to be hydrated from an xml element. """ type_key = elem.get("ontologyTypeKey") if type_key and ontologies.get_type_key(doc_type) != type_key: doc_type = ontologies.get_type_from_key(type_key) if doc_type is None: raise ValueError("Decoding type is unrecognized") return doc_type(), ontologies.get_decoder_info(doc_type)
def _create_doc(elem, doc_type): """Creates & returns a document to be hydrated from an xml element. """ type_key = elem.get('ontologyTypeKey') if type_key and ontologies.get_type_key(doc_type) != type_key: doc_type = ontologies.get_type_from_key(type_key) if doc_type is None: raise ValueError('Decoding type is unrecognized') return doc_type(), ontologies.get_decoder_info(doc_type)
def _get_doc(xml, doc_type): """Returns document. """ type_key = xml.get('ontologyTypeKey') if type_key and ontologies.get_type_key(doc_type) != type_key: doc_type = ontologies.get_type_from_key(type_key) if doc_type is None: raise ValueError('Decoding type is unrecognized') return doc_type(), ontologies.get_decoder_info(doc_type)
def decode(as_dict): """Decodes a document from a dictionary. :param dict as_dict: A document in dictionary format. :returns: A pyesdoc document instance. :rtype: object """ # Format keys. as_dict = convert.dict_keys(as_dict, convert.str_to_underscore_case) # Get document type key. try: doc_type_key = as_dict['ontology_type_key'] except KeyError: raise KeyError('ontology_type_key is unspecified and therefore the document cannot be decoded.') # Get document type. doc_type = ontologies.get_type_from_key(doc_type_key) if doc_type is None: raise ValueError('ontology_type_key cannot be mapped to a supported ontology type.') return _decode(as_dict, doc_type, False)
def decode(as_dict): """Decodes a document from a dictionary. :param dict as_dict: A document in dictionary format. :returns: A pyesdoc document instance. :rtype: object """ # Format keys. as_dict = convert.dict_keys(as_dict, convert.str_to_underscore_case) # Get document type key. try: doc_type_key = as_dict['meta']['type'] except KeyError: raise KeyError('Document pyesdoc type key is invalid.') # Get document type. doc_type = ontologies.get_type_from_key(doc_type_key) if doc_type is None: raise ValueError('Document pyesdoc type key is invalid.') return _decode(as_dict, doc_type, False)