Coverage for /opt/obspy/update-docs/src/obspy/obspy/core/util/xmlwrapper : 83%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# -*- coding: utf-8 -*- # try using lxml as it is faster except ImportError: from xml.etree import ElementTree as etree # @UnusedImport try: from xml.etree import register_namespace # @UnusedImport except ImportError: def register_namespace(prefix, uri): etree._namespace_map[uri] = prefix
pretty_print=False, __etree=etree): """ Generates a string representation of an XML element, including all subelements.
:param element: Element instance. :type xml_declaration: bool, optional :param xml_declaration: Adds a XML declaration.. Defaults to ``True``. :type encoding: str, optional :param encoding: output encoding. Defaults to ''"utf-8"''. Note that changing the encoding to a non UTF-8 compatible encoding will enable a declaration by default. :type pretty_print: bool, optional :param pretty_print: Enables formatted XML. Defaults to ``False``. :return: Encoded string containing the XML data. """ # use lxml method="xml", encoding=encoding, pretty_print=pretty_print) # use xml
""" Unified wrapper around Python's default xml module and the lxml module. """ """ Initializes a XMLPaser object.
:type xml_doc: str, filename, file-like object, parsed XML document :param xml_doc: XML document :type namespace: str, optional :param namespace: Document-wide default namespace. Defaults to ``''``. """ # some string - check if it starts with <?xml # parse XML file # fixes a problem on debian squeeze default python installation. # xml.etree.parse seems to not rewind the file after parsing, see # http://tests.obspy.org/?id=3430#0 else:
""" Converts XPath-like query into an object given by convert_to.
Only the first element will be converted if multiple elements are returned from the XPath query.
:type xpath: str :param xpath: XPath string, e.g. ``*/event``. :type xml_doc: Element or ElementTree, optional :param xml_doc: XML document to query. Defaults to parsed XML document. :type convert_to: any type :param convert_to: Type to convert to. Defaults to ``str``. :type namespace: str, optional :param namespace: Namespace used by query. Defaults to document-wide namespace set at root. """ return None # handle empty nodes return None # handle bool extra return None # try to convert into requested type except: msg = "Could not convert %s to type %s. Returning None." warnings.warn(msg % (text, convert_to)) return None
""" Very limited XPath-like query.
.. note:: This method does not support the full XPath syntax!
:type xpath: str :param xpath: XPath string, e.g. ``*/event``. :type xml_doc: Element or ElementTree, optional :param xml_doc: XML document to query. Defaults to parsed XML document. :type namespace: str, optional :param namespace: Namespace used by query. Defaults to document-wide namespace set at root. :return: List of elements. """ # namespace handling in lxml as well xml is very limited # preserve prefix prefix = '//' xpath = xpath[1:] else: # add namespace to each node else: else: # restore prefix # lxml # emulate supports for index selectors (only last element)! xpath = selector.groups()[0] list_of_elements = xml_doc.findall(xpath) try: return [list_of_elements[int(selector.groups()[1]) - 1]] except IndexError: return []
except: return None
if __name__ == '__main__': import doctest doctest.testmod(exclude_empty=True) |