modm_data.utils.xml

 1# Copyright 2013, Niklas Hauser
 2# Copyright 2016, Fabian Greif
 3# SPDX-License-Identifier: MPL-2.0
 4
 5import os
 6import re
 7import logging
 8
 9from lxml import etree
10from pathlib import Path
11
12LOGGER = logging.getLogger(__file__)
13PARSER = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8')
14
15
16class XmlReader:
17    def __init__(self, path):
18        self.filename = path
19        self.tree = self._openDeviceXML(self.filename)
20
21    def __repr__(self):
22        return f"XMLReader({os.path.basename(self.filename)})"
23
24    def _openDeviceXML(self, filename):
25        LOGGER.debug("Opening XML file '%s'", os.path.basename(filename))
26        xml_file = Path(filename).read_text("utf-8", errors="replace")
27        xml_file = re.sub(' xmlns="[^"]+"', '', xml_file, count=1).encode("utf-8")
28        xmltree = etree.fromstring(xml_file, parser=PARSER)
29        return xmltree
30
31    def queryTree(self, query):
32        """
33        This tries to apply the query to the device tree and returns either
34        - an array of element nodes,
35        - an array of strings or
36        - None, if the query failed.
37        """
38        response = None
39        try:
40            response = self.tree.xpath(query)
41        except:
42            LOGGER.error(f"Query failed for '{query}'", )
43
44        return response
45
46    def query(self, query, default=[]):
47        result = self.queryTree(query)
48        if result is not None:
49            sorted_results = []
50            for r in result:
51                if r not in sorted_results:
52                    sorted_results.append(r)
53            return sorted_results
54
55        return default
56
57    def compactQuery(self, query):
58        return self.query(query, None)
LOGGER = <Logger /opt/hostedtoolcache/Python/3.12.2/x64/lib/python3.12/site-packages/modm_data/utils/xml.py (WARNING)>
PARSER = <lxml.etree.XMLParser object>
class XmlReader:
17class XmlReader:
18    def __init__(self, path):
19        self.filename = path
20        self.tree = self._openDeviceXML(self.filename)
21
22    def __repr__(self):
23        return f"XMLReader({os.path.basename(self.filename)})"
24
25    def _openDeviceXML(self, filename):
26        LOGGER.debug("Opening XML file '%s'", os.path.basename(filename))
27        xml_file = Path(filename).read_text("utf-8", errors="replace")
28        xml_file = re.sub(' xmlns="[^"]+"', '', xml_file, count=1).encode("utf-8")
29        xmltree = etree.fromstring(xml_file, parser=PARSER)
30        return xmltree
31
32    def queryTree(self, query):
33        """
34        This tries to apply the query to the device tree and returns either
35        - an array of element nodes,
36        - an array of strings or
37        - None, if the query failed.
38        """
39        response = None
40        try:
41            response = self.tree.xpath(query)
42        except:
43            LOGGER.error(f"Query failed for '{query}'", )
44
45        return response
46
47    def query(self, query, default=[]):
48        result = self.queryTree(query)
49        if result is not None:
50            sorted_results = []
51            for r in result:
52                if r not in sorted_results:
53                    sorted_results.append(r)
54            return sorted_results
55
56        return default
57
58    def compactQuery(self, query):
59        return self.query(query, None)
XmlReader(path)
18    def __init__(self, path):
19        self.filename = path
20        self.tree = self._openDeviceXML(self.filename)
filename
tree
def queryTree(self, query):
32    def queryTree(self, query):
33        """
34        This tries to apply the query to the device tree and returns either
35        - an array of element nodes,
36        - an array of strings or
37        - None, if the query failed.
38        """
39        response = None
40        try:
41            response = self.tree.xpath(query)
42        except:
43            LOGGER.error(f"Query failed for '{query}'", )
44
45        return response

This tries to apply the query to the device tree and returns either

  • an array of element nodes,
  • an array of strings or
  • None, if the query failed.
def query(self, query, default=[]):
47    def query(self, query, default=[]):
48        result = self.queryTree(query)
49        if result is not None:
50            sorted_results = []
51            for r in result:
52                if r not in sorted_results:
53                    sorted_results.append(r)
54            return sorted_results
55
56        return default
def compactQuery(self, query):
58    def compactQuery(self, query):
59        return self.query(query, None)