def _get_value(xml, rtype=unicode): """Returns xml node value.""" # Get xml value. if isinstance(xml, types.ListType): xml = None if len(xml) == 0 else xml[0] if xml is None: return None # Get unicode. if rtype is unicode: if isinstance(xml, types.StringTypes): result = convert.str_to_unicode(xml) else: result = convert.str_to_unicode(et.tostring(xml)) else: if isinstance(xml, types.StringTypes): result = convert.unicode_to_str(xml) else: result = et.tostring(xml) # Format. result = result.strip() result = result.rstrip('|') return result
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 load_xml(xml, return_nsmap=False, default_ns='cim'): """Loads etree xml element. :param string xml: An xml blob. :param bool return_nsmap: Flag indicating whether namespace map will be returned or not. :param str default_ns: Default namespace. :returns: XML element. :rtype: lxml.etree._Element """ # Defensive programming. if xml is None: raise exceptions.DecodingException("XML is undefined.") # ... etree elements. nsmap = None if isinstance(xml, et._Element): nsmap = xml.nsmap # ... etree element trees. elif isinstance(xml, et._ElementTree): xml = xml.getroot() nsmap = xml.nsmap else: # ... files / url's try: xml = et.parse(xml) xml = xml.getroot() nsmap = xml.nsmap except Exception as e: # ... unicode if isinstance(xml, unicode): xml = convert.unicode_to_str(xml) # ... strings if isinstance(xml, str): try: xml = et.fromstring(xml) except Exception as err: raise exceptions.DecodingException("Invalid xml string.") else: nsmap = xml.nsmap # Unsupported else: raise exceptions.DecodingException( "Unsupported xml type, must be either a string, file, url or etree." ) # Set default namespace. if nsmap is not None: nsmap[default_ns] = nsmap.pop(None) # Return either a tuple or single. if return_nsmap: return xml, nsmap else: return xml
def load_xml(xml, return_nsmap=False, default_ns='cim'): """Loads etree xml element. :param string xml: An xml blob. :param bool return_nsmap: Flag indicating whether namespace map will be returned or not. :param str default_ns: Default namespace. :returns: XML element. :rtype: lxml.etree._Element """ # Defensive programming. if xml is None: raise pyesdoc.DecodingException("XML is undefined.") # ... etree elements. nsmap = None if isinstance(xml, et._Element): nsmap = xml.nsmap # ... etree element trees. elif isinstance(xml, et._ElementTree): xml = xml.getroot() nsmap = xml.nsmap else: # ... files / url's try: xml = et.parse(xml) xml = xml.getroot() nsmap = xml.nsmap except Exception as e: # ... unicode if isinstance(xml, unicode): xml = convert.unicode_to_str(xml) # ... strings if isinstance(xml, str): try: xml = et.fromstring(xml) except Exception as err: raise pyesdoc.DecodingException("Invalid xml string.") else: nsmap = xml.nsmap # Unsupported else: raise pyesdoc.DecodingException("Unsupported xml type, must be either a string, file, url or etree.") # Set default namespace. if nsmap is not None: nsmap[default_ns] = nsmap.pop(None) # Return either a tuple or single. if return_nsmap: return xml, nsmap else: return xml
def _set_content(ctx): """Set document content to be written to file system. """ try: ctx.content = convert.unicode_to_str(ctx.download.text) except Exception as err: raise _EncodingException(err)
def _get_value(data, path): """Returns formatted value for document output. """ if data is None: return None def is_collection_reference(attr): """Returns flag indicating whether attribute refers to a collection. """ try: int(attr) except ValueError: return False else: return True # Initialise return value. value = data # Walk attribute path. for attr in path.split("."): # ... collection filter by index if is_collection_reference(attr): value = value[int(attr)] # ... collection filter by attribute elif "=" in attr: left, right = attr.split("=") value = runtime.first(value, left, right.lower(), lambda v: unicode(v).lower()) # ... item attribute filter elif hasattr(value, attr): value = getattr(value, attr) # Otherwise escape. else: break # Escape at dead-end. if value is None: break # Tornado templating requires 'strings'. if isinstance(value, unicode): value = convert.unicode_to_str(value) return None if value == data else value