Python xml.etree.ElementTree.ElementTree() Examples

The following are 30 code examples of xml.etree.ElementTree.ElementTree(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xml.etree.ElementTree , or try the search function .
Example #1
Source File: e2m3u2bouquet.py    From e2m3u2bouquet with GNU General Public License v3.0 6 votes vote down vote up
def parse_map_xmltvsources_xml(self):
        """Check for a mapping override file and parses it if found
        """
        self._xmltv_sources_list = {}
        mapping_file = self._get_mapping_file()
        if mapping_file:
            try:
                tree = ET.ElementTree(file=mapping_file)
                for group in tree.findall('.//xmltvextrasources/group'):
                    group_name = group.attrib.get('id')
                    urllist = []
                    for url in group:
                        urllist.append(url.text)
                    self._xmltv_sources_list[group_name] = urllist
            except Exception, e:
                msg = 'Corrupt override.xml file'
                print(msg)
                if DEBUG:
                    raise msg 
Example #2
Source File: dspl_model_loader.py    From dspl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def ElementsToAttributes(concept_element):
  """Process the attributes in an an ElementTree concept element.

  Args:
    concept_element: An ElementTree concept element

  Returns:
    A list of dspl_model.Attribute instances, populated with the data from the
    argument concept element.
  """
  attribute_elements = concept_element.findall(
      _DSPL_SCHEMA_PREFIX + 'attribute')

  dspl_attributes = []

  for attribute_element in attribute_elements:
    attribute_concept = attribute_element.get('concept')

    # For now, only handle attributes with a concept reference
    if attribute_concept:
      dspl_attributes.append(
          dspl_model.Attribute(attribute_concept, _GetValue(attribute_element)))

  return dspl_attributes 
Example #3
Source File: parsers.py    From sota-extractor with Apache License 2.0 6 votes vote down vote up
def _unwind(cls, el: ElementTree, keep_links) -> "Text":
        start = el.text or ""
        if el.tag == "a":
            links = [Link(title=el.text, url=el.attrib.get("href", None))]
        else:
            links = []

        children = ""
        for child in el:
            t = cls._unwind(child, keep_links=keep_links)
            children += t.text
            links.extend(t.links)

        end = el.tail or ""

        if el.tag == "a" and keep_links:
            return cls(
                text=f"[{start}]({el.attrib.get('href', '')}){children}{end}",
                links=links,
            )
        return cls(text=start + children + end, links=links) 
Example #4
Source File: parsers.py    From sota-extractor with Apache License 2.0 6 votes vote down vote up
def parse_subdatasets(
    parent: Dataset, pairs: List[Tuple[ElementTree, ElementTree]]
) -> List[Dataset]:
    subdatasets = []
    for p, table in pairs:
        strong = p.find("strong")
        if strong is None:
            continue
        subdatasets.append(
            Dataset(
                name=strong.text.strip().strip(":"),
                is_subdataset=True,
                parent=parent,
                sota=parse_sota(table),
            )
        )
    return subdatasets 
Example #5
Source File: test_xml_etree.py    From BinderFilter with MIT License 6 votes vote down vote up
def parseliteral():
    """
    >>> element = ET.XML("<html><body>text</body></html>")
    >>> ET.ElementTree(element).write(sys.stdout)
    <html><body>text</body></html>
    >>> element = ET.fromstring("<html><body>text</body></html>")
    >>> ET.ElementTree(element).write(sys.stdout)
    <html><body>text</body></html>
    >>> sequence = ["<html><body>", "text</bo", "dy></html>"]
    >>> element = ET.fromstringlist(sequence)
    >>> print ET.tostring(element)
    <html><body>text</body></html>
    >>> print "".join(ET.tostringlist(element))
    <html><body>text</body></html>
    >>> ET.tostring(element, "ascii")
    "<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>"
    >>> _, ids = ET.XMLID("<html><body>text</body></html>")
    >>> len(ids)
    0
    >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
    >>> len(ids)
    1
    >>> ids["body"].tag
    'body'
    """ 
Example #6
Source File: test_xml_etree.py    From BinderFilter with MIT License 6 votes vote down vote up
def file_init():
    """
    >>> import StringIO

    >>> stringfile = StringIO.StringIO(SAMPLE_XML)
    >>> tree = ET.ElementTree(file=stringfile)
    >>> tree.find("tag").tag
    'tag'
    >>> tree.find("section/tag").tag
    'tag'

    >>> tree = ET.ElementTree(file=SIMPLE_XMLFILE)
    >>> tree.find("element").tag
    'element'
    >>> tree.find("element/../empty-element").tag
    'empty-element'
    """ 
Example #7
Source File: dspl_model_loader.py    From dspl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _GetValue(parent_element):
  """Get the text nested inside an XML element's value element.

  Args:
    parent_element: An ElementTree element

  Returns:
    String inside value tag inside parent or, if no such tag can be found, None
  """
  if parent_element is not None:
    value_element = parent_element.find(_DSPL_SCHEMA_PREFIX + 'value')

    if value_element is not None:
      return value_element.text

  return '' 
Example #8
Source File: __init__.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def parse_table_definition_file(file):
    """
    Read an parse the XML of a table-definition file.
    @return: an ElementTree object for the table definition
    """
    logging.info("Reading table definition from '%s'...", file)
    if not os.path.isfile(file):
        handle_error("File '%s' does not exist.", file)

    try:
        tableGenFile = ElementTree.ElementTree().parse(file)
    except OSError as e:
        handle_error("Could not read result file %s: %s", file, e)
    except ElementTree.ParseError as e:
        handle_error("Table file %s is invalid: %s", file, e)
    if "table" != tableGenFile.tag:
        handle_error(
            "Table file %s is invalid: It's root element is not named 'table'.", file
        )
    return tableGenFile 
Example #9
Source File: dspl_model_loader.py    From dspl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _NSParser(input_file):
  """A special ElementTree parser that gets all the imported namespaces.

  Args:
    input_file: A file-like object containing XML

  Returns:
    A list with two elements: the root of the ElementTree, and a list of
    namespace strings imported by the XML file.
  """
  events = ('start', 'start-ns')

  root = None
  namespace_list = []

  for event, element in xml.etree.ElementTree.iterparse(input_file, events):
    if event == 'start-ns':
      namespace_list.append(element)
    elif event == 'start':
      if root is None:
        root = element

  return [xml.etree.ElementTree.ElementTree(root), namespace_list] 
Example #10
Source File: outputhandler.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def store_header_in_xml(self, version, memlimit, timelimit, corelimit):

        # store benchmarkInfo in XML
        self.xml_header = ElementTree.Element(
            "result",
            benchmarkname=self.benchmark.name,
            date=self.benchmark.start_time.strftime("%Y-%m-%d %H:%M:%S %Z"),
            starttime=self.benchmark.start_time.isoformat(),
            tool=self.benchmark.tool_name,
            version=version,
            toolmodule=self.benchmark.tool_module,
            generator="BenchExec " + benchexec.__version__,
        )
        if self.benchmark.display_name:
            self.xml_header.set("displayName", self.benchmark.display_name)

        if memlimit is not None:
            self.xml_header.set(MEMLIMIT, memlimit)
        if timelimit is not None:
            self.xml_header.set(TIMELIMIT, timelimit)
        if corelimit is not None:
            self.xml_header.set(CORELIMIT, corelimit)

        if self.benchmark.description:
            description_tag = ElementTree.Element("description")
            description_tag.text = self.benchmark.description
            self.xml_header.append(description_tag)

        # store columnTitles in XML, this are the default columns, that are shown in a default html-table from table-generator
        columntitlesElem = ElementTree.Element("columns")
        columntitlesElem.append(ElementTree.Element("column", title="status"))
        columntitlesElem.append(ElementTree.Element("column", title="cputime"))
        columntitlesElem.append(ElementTree.Element("column", title="walltime"))
        for column in self.benchmark.columns:
            columnElem = ElementTree.Element("column", title=column.title)
            columntitlesElem.append(columnElem)
        self.xml_header.append(columntitlesElem) 
Example #11
Source File: __init__.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def test_description(self):
        test_description = """
            äöüß     This tests non-ASCII characters, line breaks, whitespace, and
              <>&"'  XML special characters.
            """
        with tempfile.NamedTemporaryFile(
            prefix="description", suffix=".txt", dir=self.tmp, mode="w+"
        ) as desc:
            desc.write(test_description)
            desc.flush()

            self.run_cmd(
                self.benchmark_test_file,
                "--no-compress-results",
                "--description-file",
                desc.name,
            )

        generated_files = glob.glob(os.path.join(self.output_dir, "*.xml"))
        assert generated_files, "error in test, no results generated"

        for f in generated_files:
            result_xml = ElementTree.ElementTree().parse(f)
            actual_description = result_xml.find("description").text
            self.assertEqual(actual_description, test_description.strip()) 
Example #12
Source File: test_xml_etree.py    From oss-ftp with MIT License 6 votes vote down vote up
def parseliteral():
    """
    >>> element = ET.XML("<html><body>text</body></html>")
    >>> ET.ElementTree(element).write(sys.stdout)
    <html><body>text</body></html>
    >>> element = ET.fromstring("<html><body>text</body></html>")
    >>> ET.ElementTree(element).write(sys.stdout)
    <html><body>text</body></html>
    >>> sequence = ["<html><body>", "text</bo", "dy></html>"]
    >>> element = ET.fromstringlist(sequence)
    >>> print ET.tostring(element)
    <html><body>text</body></html>
    >>> print "".join(ET.tostringlist(element))
    <html><body>text</body></html>
    >>> ET.tostring(element, "ascii")
    "<?xml version='1.0' encoding='ascii'?>\\n<html><body>text</body></html>"
    >>> _, ids = ET.XMLID("<html><body>text</body></html>")
    >>> len(ids)
    0
    >>> _, ids = ET.XMLID("<html><body id='body'>text</body></html>")
    >>> len(ids)
    1
    >>> ids["body"].tag
    'body'
    """ 
Example #13
Source File: outputhandler.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def _write_rough_result_xml_to_file(self, xml, filename):
        """Write a rough string version of the XML (for temporary files)."""
        # Write content to temp file first
        error = xml.get("error", None)
        xml.set("error", "incomplete")  # Mark result file as incomplete
        temp_filename = filename + ".tmp"
        with open(temp_filename, "wb") as file:
            ElementTree.ElementTree(xml).write(
                file, encoding="utf-8", xml_declaration=True
            )
        os.rename(temp_filename, filename)
        if error is not None:
            xml.set("error", error)
        else:
            del xml.attrib["error"] 
Example #14
Source File: outputhandler.py    From benchexec with Apache License 2.0 6 votes vote down vote up
def _write_pretty_result_xml_to_file(self, xml, filename):
        """Writes a nicely formatted XML file with DOCTYPE, and compressed if necessary."""
        if self.compress_results:
            actual_filename = filename + ".bz2"
            open_func = bz2.BZ2File
        else:
            # write content to temp file first to prevent losing data
            # in existing file if writing fails
            actual_filename = filename + ".tmp"
            open_func = open

        with io.TextIOWrapper(
            open_func(actual_filename, "wb"), encoding="utf-8"
        ) as file:
            rough_string = ElementTree.tostring(xml, encoding="unicode")
            reparsed = minidom.parseString(rough_string)
            doctype = minidom.DOMImplementation().createDocumentType(
                "result", RESULT_XML_PUBLIC_ID, RESULT_XML_SYSTEM_ID
            )
            reparsed.insertBefore(doctype, reparsed.documentElement)
            reparsed.writexml(
                file, indent="", addindent="  ", newl="\n", encoding="utf-8"
            )

        if self.compress_results:
            # try to delete uncompressed file (would have been overwritten in no-compress-mode)
            try:
                os.remove(filename)
            except OSError:
                pass
            self.all_created_files.discard(filename)
            self.all_created_files.add(actual_filename)
        else:
            os.rename(actual_filename, filename)
            self.all_created_files.add(filename)

        return filename 
Example #15
Source File: test_xml_etree.py    From oss-ftp with MIT License 6 votes vote down vote up
def file_init():
    """
    >>> import StringIO

    >>> stringfile = StringIO.StringIO(SAMPLE_XML)
    >>> tree = ET.ElementTree(file=stringfile)
    >>> tree.find("tag").tag
    'tag'
    >>> tree.find("section/tag").tag
    'tag'

    >>> tree = ET.ElementTree(file=SIMPLE_XMLFILE)
    >>> tree.find("element").tag
    'element'
    >>> tree.find("element/../empty-element").tag
    'empty-element'
    """ 
Example #16
Source File: document.py    From SEM with MIT License 6 votes vote down vote up
def from_xml(cls, xml, chunks_to_load=None, load_subtypes=True, type_separator=u"."):
        if sem.misc.is_string(xml):
            data = ET.parse(xml)
        elif isinstance(xml, ET.ElementTree):
            data = xml
        elif isinstance(xml, type(ET.Element("a"))): # did not ind a better way to do this
            data = xml
        else:
            raise TypeError("Invalid type for loading XML-SEM document: {0}".format(type(xml)))
        
        root = data.getroot()
        if root.tag != "sem":
            raise ValueError("Not sem xml file type: '{0}'".format(root.tag))
        doc_list = []
        for document in list(root):
            doc_list.append(Document.from_xml(document))
        return SEMCorpus(doc_list) 
Example #17
Source File: VisualStudio.py    From bob with GNU General Public License v3.0 6 votes vote down vote up
def generateUser(self):
        groups = []
        if self.runTargets:
            # VS can handle only one target per project
            target = self.runTargets[0]
            g = ElementTree.Element("PropertyGroup", {"Condition" : "'$(Configuration)|$(Platform)'=='Build|Win32'"})
            ElementTree.SubElement(g, "LocalDebuggerCommand").text = str(target)
            ElementTree.SubElement(g, "DebuggerFlavor").text = "WindowsLocalDebugger"
            groups.append(ElementTree.tostring(g, encoding="unicode"))

            n = ElementTree.Element("PropertyGroup", {"Condition" : "'$(Configuration)|$(Platform)'=='Checkout+Build|Win32'"})
            ElementTree.SubElement(g, "LocalDebuggerCommand").text = str(target)
            ElementTree.SubElement(g, "DebuggerFlavor").text = "WindowsLocalDebugger"
            groups.append(ElementTree.tostring(g, encoding="unicode"))
            # FIXME: print warning when more than one taget exists

        return USER_TEMPLATE.format(GROUPS="\n".join(groups)) 
Example #18
Source File: route_manipulation.py    From scenario_runner with MIT License 6 votes vote down vote up
def _get_latlon_ref(world):
    """
    Convert from waypoints world coordinates to CARLA GPS coordinates
    :return: tuple with lat and lon coordinates
    """
    xodr = world.get_map().to_opendrive()
    tree = ET.ElementTree(ET.fromstring(xodr))

    # default reference
    lat_ref = 42.0
    lon_ref = 2.0

    for opendrive in tree.iter("OpenDRIVE"):
        for header in opendrive.iter("header"):
            for georef in header.iter("geoReference"):
                if georef.text:
                    str_list = georef.text.split(' ')
                    for item in str_list:
                        if '+lat_0' in item:
                            lat_ref = float(item.split('=')[1])
                        if '+lon_0' in item:
                            lon_ref = float(item.split('=')[1])
    return lat_ref, lon_ref 
Example #19
Source File: FileUtils.py    From ID2T with MIT License 6 votes vote down vote up
def parse_xml(filepath: str):
    """
    Parses an XML File
    It is assumed, that packets are placed on the second hierarchical level and packet-information is encoded
    as attributes

    :param filepath: the path to the XML file to be parsed
    :return: a List of Dictionaries, each Dictionary contains the information of one packet
    """

    tree = ElementTree.parse(filepath)
    root = tree.getroot()

    # Convert Tree to List of Dictionaries
    packets = []
    for child in root:
        packets.append(child.attrib)

    return packets 
Example #20
Source File: bulk.py    From SalesforceXyTools with Apache License 2.0 6 votes vote down vote up
def create_job_doc(self, object_name=None, operation=None,
                       contentType='CSV', concurrency=None, external_id_name=None):
        root = ET.Element("jobInfo")
        root.set("xmlns", self.jobNS)
        op = ET.SubElement(root, "operation")
        op.text = operation
        obj = ET.SubElement(root, "object")
        obj.text = object_name
        if external_id_name:
            ext = ET.SubElement(root, 'externalIdFieldName')
            ext.text = external_id_name

        if concurrency:
            con = ET.SubElement(root, "concurrencyMode")
            con.text = concurrency
        ct = ET.SubElement(root, "contentType")
        ct.text = contentType

        buf = BytesIO()
        tree = ET.ElementTree(root)
        tree.write(buf, encoding="UTF-8")
        return buf.getvalue().decode("utf-8") 
Example #21
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def check_issue3151():
    """

    >>> e = ET.XML('<prefix:localname xmlns:prefix="${stuff}"/>')
    >>> e.tag
    '{${stuff}}localname'
    >>> t = ET.ElementTree(e)
    >>> ET.tostring(e)
    '<ns0:localname xmlns:ns0="${stuff}" />'

    """ 
Example #22
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_main(module_name='xml.etree.ElementTree'):
    from test import test_xml_etree

    use_py_module = (module_name == 'xml.etree.ElementTree')

    # The same doctests are used for both the Python and the C implementations
    assert test_xml_etree.ET.__name__ == module_name

    # XXX the C module should give the same warnings as the Python module
    with CleanContext(quiet=not use_py_module):
        test_support.run_doctest(test_xml_etree, verbosity=True)

    # The module should not be changed by the tests
    assert test_xml_etree.ET.__name__ == module_name 
Example #23
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def __exit__(self, *args):
        from xml.etree import ElementTree
        # Restore mapping and path cache
        ElementTree._namespace_map = self._nsmap
        ElementTree.ElementPath._cache = self._path_cache
        self.checkwarnings.__exit__(*args) 
Example #24
Source File: test_xml_etree.py    From oss-ftp with MIT License 5 votes vote down vote up
def serialize(elem, to_string=True, **options):
    import StringIO
    file = StringIO.StringIO()
    tree = ET.ElementTree(elem)
    tree.write(file, **options)
    if to_string:
        return file.getvalue()
    else:
        file.seek(0)
        return file 
Example #25
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def bug_xmltoolkit25():
    """

    typo in ElementTree.findtext

    >>> elem = ET.XML(SAMPLE_XML)
    >>> tree = ET.ElementTree(elem)
    >>> tree.findtext("tag")
    'text'
    >>> tree.findtext("section/tag")
    'subtext'

    """ 
Example #26
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def iterators():
    """
    Test iterators.

    >>> e = ET.XML("<html><body>this is a <i>paragraph</i>.</body>..</html>")
    >>> summarize_list(e.iter())
    ['html', 'body', 'i']
    >>> summarize_list(e.find("body").iter())
    ['body', 'i']
    >>> summarize(next(e.iter()))
    'html'
    >>> "".join(e.itertext())
    'this is a paragraph...'
    >>> "".join(e.find("body").itertext())
    'this is a paragraph.'
    >>> next(e.itertext())
    'this is a '

    Method iterparse should return an iterator. See bug 6472.

    >>> sourcefile = serialize(e, to_string=False)
    >>> next(ET.iterparse(sourcefile))  # doctest: +ELLIPSIS
    ('end', <Element 'i' at 0x...>)

    >>> tree = ET.ElementTree(None)
    >>> tree.iter()
    Traceback (most recent call last):
    AttributeError: 'NoneType' object has no attribute 'iter'
    """ 
Example #27
Source File: e2m3u2bouquet.py    From e2m3u2bouquet with GNU General Public License v3.0 5 votes vote down vote up
def _parse_map_bouquet_xml(self):
        """Check for bouquets within mapping override file and applies if found
        """
        category_order = []
        mapping_file = self._get_mapping_file()
        if mapping_file:
            self._update_status('----Parsing custom bouquet order----')
            print('\n'.format(Status.message))

            try:
                tree = ET.ElementTree(file=mapping_file)
                for node in tree.findall(".//category"):
                    dictoption = {}

                    category = node.attrib.get('name')
                    if not type(category) is unicode:
                        category = category.decode("utf-8")
                    cat_title_override = node.attrib.get('nameOverride', '')
                    if not type(cat_title_override) is unicode:
                        cat_title_override = cat_title_override.decode("utf-8")
                    dictoption['nameOverride'] = cat_title_override
                    dictoption['enabled'] = node.attrib.get('enabled', True) == 'true'
                    category_order.append(category)

                    # If this category is marked as custom and doesn't exist in self._dictchannels then add
                    is_custom_category = node.attrib.get('customCategory', False) == 'true'
                    if is_custom_category:
                        dictoption['customCategory'] = True
                        if category not in self._dictchannels:
                            self._dictchannels[category] = []

                    self._category_options[category] = dictoption

                self._update_status('custom bouquet order applied...')
                print(Status.message)
            except Exception, e:
                msg = 'Corrupt override.xml file'
                print(msg)
                if DEBUG:
                    raise msg 
Example #28
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def simplefind():
    """
    Test find methods using the elementpath fallback.

    >>> from xml.etree import ElementTree

    >>> CurrentElementPath = ElementTree.ElementPath
    >>> ElementTree.ElementPath = ElementTree._SimpleElementPath()
    >>> elem = ElementTree.XML(SAMPLE_XML)
    >>> elem.find("tag").tag
    'tag'
    >>> ElementTree.ElementTree(elem).find("tag").tag
    'tag'
    >>> elem.findtext("tag")
    'text'
    >>> elem.findtext("tog")
    >>> elem.findtext("tog", "default")
    'default'
    >>> ElementTree.ElementTree(elem).findtext("tag")
    'text'
    >>> summarize_list(elem.findall("tag"))
    ['tag', 'tag']
    >>> summarize_list(elem.findall(".//tag"))
    ['tag', 'tag', 'tag']

    Path syntax doesn't work in this case.

    >>> elem.find("section/tag")
    >>> elem.findtext("section/tag")
    >>> summarize_list(elem.findall("section/tag"))
    []

    >>> ElementTree.ElementPath = CurrentElementPath
    """ 
Example #29
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def xinclude_loader(href, parse="xml", encoding=None):
    try:
        data = XINCLUDE[href]
    except KeyError:
        raise IOError("resource not found")
    if parse == "xml":
        from xml.etree.ElementTree import XML
        return XML(data)
    return data 
Example #30
Source File: test_xml_etree.py    From BinderFilter with MIT License 5 votes vote down vote up
def serialize(elem, to_string=True, **options):
    import StringIO
    file = StringIO.StringIO()
    tree = ET.ElementTree(elem)
    tree.write(file, **options)
    if to_string:
        return file.getvalue()
    else:
        file.seek(0)
        return file