modm_data.utils

 1# Copyright 2022, Niklas Hauser
 2# SPDX-License-Identifier: MPL-2.0
 3
 4from .math import Point, Line, VLine, HLine, Rectangle, Region
 5from .helper import list_lstrip, list_rstrip, list_strip, pkg_file_exists, pkg_apply_patch, apply_patch
 6from .anytree import ReversePreOrderIter
 7from .path import root_path, ext_path, cache_path, patch_path
 8from .xml import XmlReader
 9
10__all__ = [
11    "Point",
12    "Line",
13    "VLine",
14    "HLine",
15    "Rectangle",
16    "Region",
17    "list_lstrip",
18    "list_rstrip",
19    "list_strip",
20    "pkg_file_exists",
21    "pkg_apply_patch",
22    "apply_patch",
23    "ReversePreOrderIter",
24    "root_path",
25    "ext_path",
26    "cache_path",
27    "patch_path",
28    "XmlReader",
29]
class Point:
13class Point:
14    def __init__(self, *xy, type: Enum = None):
15        if isinstance(xy[0], tuple):
16            self.x = xy[0][0]
17            self.y = xy[0][1]
18        else:
19            self.x = xy[0]
20            self.y = xy[1]
21        self.type = type
22
23    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
24        return math.isclose(self.x, other.x, rel_tol=rtol, abs_tol=atol) and math.isclose(
25            self.y, other.y, rel_tol=rtol, abs_tol=atol
26        )
27
28    def distance_squared(self, other) -> float:
29        return math.pow(self.x - other.x, 2) + math.pow(self.y - other.y, 2)
30
31    def distance(self, other) -> float:
32        return math.sqrt(self.distance_squared(other))
33
34    def __neg__(self):
35        return Point(-self.x, -self.y)
36
37    def __hash__(self):
38        return hash(f"{self.x} {self.y}")
39
40    def __repr__(self) -> str:
41        x = f"{self.x:.1f}" if isinstance(self.x, float) else self.x
42        y = f"{self.y:.1f}" if isinstance(self.y, float) else self.y
43        out = [x, y] if self.type is None else [x, y, self.type.name]
44        return f"({','.join(out)})"
Point(*xy, type: enum.Enum = None)
14    def __init__(self, *xy, type: Enum = None):
15        if isinstance(xy[0], tuple):
16            self.x = xy[0][0]
17            self.y = xy[0][1]
18        else:
19            self.x = xy[0]
20            self.y = xy[1]
21        self.type = type
type
def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
23    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
24        return math.isclose(self.x, other.x, rel_tol=rtol, abs_tol=atol) and math.isclose(
25            self.y, other.y, rel_tol=rtol, abs_tol=atol
26        )
def distance_squared(self, other) -> float:
28    def distance_squared(self, other) -> float:
29        return math.pow(self.x - other.x, 2) + math.pow(self.y - other.y, 2)
def distance(self, other) -> float:
31    def distance(self, other) -> float:
32        return math.sqrt(self.distance_squared(other))
class Line:
 47class Line:
 48    class Direction(Enum):
 49        ANGLE = 0
 50        VERTICAL = 1
 51        HORIZONTAL = 2
 52
 53    def __init__(self, *r, width: float = None, type: Enum = None):
 54        if isinstance(r[0], Rectangle):
 55            self.p0 = r[0].p0
 56            self.p1 = r[0].p1
 57        elif isinstance(r[0], Point):
 58            self.p0 = r[0]
 59            self.p1 = r[1]
 60        elif isinstance(r[0], tuple):
 61            self.p0 = Point(r[0][0], r[0][1])
 62            self.p1 = Point(r[1][0], r[1][1])
 63        else:
 64            self.p0 = Point(r[0], r[1])
 65            self.p1 = Point(r[2], r[3])
 66
 67        self.width = 0.1 if width is None else width
 68        self.type = type
 69
 70    @cached_property
 71    def bbox(self):
 72        return Rectangle(
 73            min(self.p0.x, self.p1.x), min(self.p0.y, self.p1.y), max(self.p0.x, self.p1.x), max(self.p0.y, self.p1.y)
 74        )
 75
 76    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
 77        return self.p0.isclose(other.p0, rtol, atol) and self.p1.isclose(other.p1, rtol, atol)
 78
 79    def contains(self, point, atol: float = 0.0) -> bool:
 80        # if the point lies on the line (A-C---B), the distance A-C + C-B = A-B
 81        ac = self.p0.distance_squared(point)
 82        cb = self.p1.distance_squared(point)
 83        ab = self.p0.distance_squared(self.p1)
 84        return (ac + cb + math.pow(atol, 2)) <= ab
 85
 86    @property
 87    def direction(self):
 88        if math.isclose(self.p0.x, self.p1.x):
 89            return Line.Direction.VERTICAL
 90        if math.isclose(self.p0.y, self.p1.y):
 91            return Line.Direction.HORIZONTAL
 92        return Line.Direction.ANGLE
 93
 94    def specialize(self):
 95        if self.direction == Line.Direction.VERTICAL:
 96            return VLine(self.p0.x, self.p0.y, self.p1.y, self.width)
 97        if self.direction == Line.Direction.HORIZONTAL:
 98            return HLine(self.p0.y, self.p0.x, self.p1.x, self.width)
 99        return Line(self.p0, self.p1, width=self.width)
100
101    def __repr__(self) -> str:
102        data = [repr(self.p0), repr(self.p1)]
103        if self.width:
104            data += [f"{self.width:.1f}"]
105        if self.type is not None:
106            data += [self.type.name]
107        return f"<{','.join(data)}>"
Line(*r, width: float = None, type: enum.Enum = None)
53    def __init__(self, *r, width: float = None, type: Enum = None):
54        if isinstance(r[0], Rectangle):
55            self.p0 = r[0].p0
56            self.p1 = r[0].p1
57        elif isinstance(r[0], Point):
58            self.p0 = r[0]
59            self.p1 = r[1]
60        elif isinstance(r[0], tuple):
61            self.p0 = Point(r[0][0], r[0][1])
62            self.p1 = Point(r[1][0], r[1][1])
63        else:
64            self.p0 = Point(r[0], r[1])
65            self.p1 = Point(r[2], r[3])
66
67        self.width = 0.1 if width is None else width
68        self.type = type
width
type
bbox
70    @cached_property
71    def bbox(self):
72        return Rectangle(
73            min(self.p0.x, self.p1.x), min(self.p0.y, self.p1.y), max(self.p0.x, self.p1.x), max(self.p0.y, self.p1.y)
74        )
def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
76    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
77        return self.p0.isclose(other.p0, rtol, atol) and self.p1.isclose(other.p1, rtol, atol)
def contains(self, point, atol: float = 0.0) -> bool:
79    def contains(self, point, atol: float = 0.0) -> bool:
80        # if the point lies on the line (A-C---B), the distance A-C + C-B = A-B
81        ac = self.p0.distance_squared(point)
82        cb = self.p1.distance_squared(point)
83        ab = self.p0.distance_squared(self.p1)
84        return (ac + cb + math.pow(atol, 2)) <= ab
direction
86    @property
87    def direction(self):
88        if math.isclose(self.p0.x, self.p1.x):
89            return Line.Direction.VERTICAL
90        if math.isclose(self.p0.y, self.p1.y):
91            return Line.Direction.HORIZONTAL
92        return Line.Direction.ANGLE
def specialize(self):
94    def specialize(self):
95        if self.direction == Line.Direction.VERTICAL:
96            return VLine(self.p0.x, self.p0.y, self.p1.y, self.width)
97        if self.direction == Line.Direction.HORIZONTAL:
98            return HLine(self.p0.y, self.p0.x, self.p1.x, self.width)
99        return Line(self.p0, self.p1, width=self.width)
class Line.Direction(enum.Enum):
48    class Direction(Enum):
49        ANGLE = 0
50        VERTICAL = 1
51        HORIZONTAL = 2
ANGLE = <Direction.ANGLE: 0>
VERTICAL = <Direction.VERTICAL: 1>
HORIZONTAL = <Direction.HORIZONTAL: 2>
Inherited Members
enum.Enum
name
value
class VLine(modm_data.utils.Line):
110class VLine(Line):
111    def __init__(self, x: float, y0: float, y1: float, width: float = None):
112        if y0 > y1:
113            y0, y1 = y1, y0
114        super().__init__(Point(x, y0), Point(x, y1), width=width)
115        self.length = y1 - y0
116
117    @property
118    def direction(self):
119        return Line.Direction.VERTICAL
120
121    def __repr__(self) -> str:
122        x = f"{self.p0.x:.1f}" if isinstance(self.p0.x, float) else self.p0.x
123        y0 = f"{self.p0.y:.1f}" if isinstance(self.p0.y, float) else self.p0.y
124        y1 = f"{self.p1.y:.1f}" if isinstance(self.p1.y, float) else self.p1.y
125        out = f"<X{x}:{y0},{y1}"
126        if self.width:
127            out += f"|{self.width:.1f}"
128        return out + ">"
VLine(x: float, y0: float, y1: float, width: float = None)
111    def __init__(self, x: float, y0: float, y1: float, width: float = None):
112        if y0 > y1:
113            y0, y1 = y1, y0
114        super().__init__(Point(x, y0), Point(x, y1), width=width)
115        self.length = y1 - y0
length
direction
117    @property
118    def direction(self):
119        return Line.Direction.VERTICAL
class HLine(modm_data.utils.Line):
131class HLine(Line):
132    def __init__(self, y: float, x0: float, x1: float, width: float = None):
133        if x0 > x1:
134            x0, x1 = x1, x0
135        super().__init__(Point(x0, y), Point(x1, y), width=width)
136        self.length = x1 - x0
137
138    @property
139    def direction(self):
140        return Line.Direction.HORIZONTAL
141
142    def __repr__(self) -> str:
143        y = f"{self.p0.y:.1f}" if isinstance(self.p0.y, float) else self.p0.y
144        x0 = f"{self.p0.x:.1f}" if isinstance(self.p0.x, float) else self.p0.x
145        x1 = f"{self.p1.x:.1f}" if isinstance(self.p1.x, float) else self.p1.x
146        out = f"<Y{y}:{x0},{x1}"
147        if self.width:
148            out += f"|{self.width:.1f}"
149        return out + ">"
HLine(y: float, x0: float, x1: float, width: float = None)
132    def __init__(self, y: float, x0: float, x1: float, width: float = None):
133        if x0 > x1:
134            x0, x1 = x1, x0
135        super().__init__(Point(x0, y), Point(x1, y), width=width)
136        self.length = x1 - x0
length
direction
138    @property
139    def direction(self):
140        return Line.Direction.HORIZONTAL
class Rectangle:
152class Rectangle:
153    def __init__(self, *r):
154        # P0 is left, bottom
155        # P1 is right, top
156        if isinstance(r[0], pp.raw.FS_RECTF):
157            self.p0 = Point(r[0].left, r[0].bottom)
158            self.p1 = Point(r[0].right, r[0].top)
159        elif isinstance(r[0], Point):
160            self.p0 = r[0]
161            self.p1 = r[1]
162        elif isinstance(r[0], tuple):
163            self.p0 = Point(r[0][0], r[0][1])
164            self.p1 = Point(r[1][0], r[1][1])
165        else:
166            self.p0 = Point(r[0], r[1])
167            self.p1 = Point(r[2], r[3])
168
169        # Ensure the correct ordering of point values
170        if self.p0.x > self.p1.x:
171            self.p0.x, self.p1.x = self.p1.x, self.p0.x
172        if self.p0.y > self.p1.y:
173            self.p0.y, self.p1.y = self.p1.y, self.p0.y
174
175        # assert self.p0.x <= self.p1.x
176        # assert self.p0.y <= self.p1.y
177
178        self.x = self.p0.x
179        self.y = self.p0.y
180        self.left = self.p0.x
181        self.bottom = self.p0.y
182
183        self.right = self.p1.x
184        self.top = self.p1.y
185
186        self.width = self.p1.x - self.p0.x
187        self.height = self.p1.y - self.p0.y
188
189    def contains(self, other) -> bool:
190        if isinstance(other, Point):
191            return self.bottom <= other.y <= self.top and self.left <= other.x <= self.right
192        # Comparing y-axis first may be faster for "content areas filtering"
193        # when doing subparsing of page content (like in tables)
194        return (
195            self.bottom <= other.bottom
196            and other.top <= self.top
197            and self.left <= other.left
198            and other.right <= self.right
199        )
200
201    def overlaps(self, other) -> bool:
202        return self.contains(other.p0) or self.contains(other.p1)
203
204    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
205        return self.p0.isclose(other.p0, rtol, atol) and self.p1.isclose(other.p1, rtol, atol)
206
207    @cached_property
208    def midpoint(self) -> Point:
209        return Point((self.p1.x + self.p0.x) / 2, (self.p1.y + self.p0.y) / 2)
210
211    @cached_property
212    def points(self) -> list[Point]:
213        return [self.p0, Point(self.right, self.bottom), self.p1, Point(self.left, self.top)]
214
215    def offset(self, offset):
216        return Rectangle(self.p0.x - offset, self.p0.y - offset, self.p1.x + offset, self.p1.y + offset)
217
218    def offset_x(self, offset):
219        return Rectangle(self.p0.x - offset, self.p0.y, self.p1.x + offset, self.p1.y)
220
221    def offset_y(self, offset):
222        return Rectangle(self.p0.x, self.p0.y - offset, self.p1.x, self.p1.y + offset)
223
224    def translated(self, point):
225        return Rectangle(self.p0.x + point.x, self.p0.y + point.y, self.p1.x + point.x, self.p1.y + point.y)
226
227    def rotated(self, rotation):
228        cos = math.cos(math.radians(rotation))
229        sin = math.sin(math.radians(rotation))
230        return Rectangle(
231            self.p0.x * cos - self.p0.y * sin,
232            self.p0.x * sin + self.p0.y * cos,
233            self.p1.x * cos - self.p1.y * sin,
234            self.p1.x * sin + self.p1.y * cos,
235        )
236
237    def joined(self, other):
238        return Rectangle(
239            min(self.p0.x, other.p0.x),
240            min(self.p0.y, other.p0.y),
241            max(self.p1.x, other.p1.x),
242            max(self.p1.y, other.p1.y),
243        )
244
245    def round(self, accuracy=0):
246        return Rectangle(
247            round(self.p0.x, accuracy),
248            round(self.p0.y, accuracy),
249            round(self.p1.x, accuracy),
250            round(self.p1.y, accuracy),
251        )
252
253    def __hash__(self):
254        return hash(self.p0) + hash(self.p1)
255
256    def __repr__(self) -> str:
257        return f"[{repr(self.p0)},{repr(self.p1)}]"
Rectangle(*r)
153    def __init__(self, *r):
154        # P0 is left, bottom
155        # P1 is right, top
156        if isinstance(r[0], pp.raw.FS_RECTF):
157            self.p0 = Point(r[0].left, r[0].bottom)
158            self.p1 = Point(r[0].right, r[0].top)
159        elif isinstance(r[0], Point):
160            self.p0 = r[0]
161            self.p1 = r[1]
162        elif isinstance(r[0], tuple):
163            self.p0 = Point(r[0][0], r[0][1])
164            self.p1 = Point(r[1][0], r[1][1])
165        else:
166            self.p0 = Point(r[0], r[1])
167            self.p1 = Point(r[2], r[3])
168
169        # Ensure the correct ordering of point values
170        if self.p0.x > self.p1.x:
171            self.p0.x, self.p1.x = self.p1.x, self.p0.x
172        if self.p0.y > self.p1.y:
173            self.p0.y, self.p1.y = self.p1.y, self.p0.y
174
175        # assert self.p0.x <= self.p1.x
176        # assert self.p0.y <= self.p1.y
177
178        self.x = self.p0.x
179        self.y = self.p0.y
180        self.left = self.p0.x
181        self.bottom = self.p0.y
182
183        self.right = self.p1.x
184        self.top = self.p1.y
185
186        self.width = self.p1.x - self.p0.x
187        self.height = self.p1.y - self.p0.y
x
y
left
bottom
right
top
width
height
def contains(self, other) -> bool:
189    def contains(self, other) -> bool:
190        if isinstance(other, Point):
191            return self.bottom <= other.y <= self.top and self.left <= other.x <= self.right
192        # Comparing y-axis first may be faster for "content areas filtering"
193        # when doing subparsing of page content (like in tables)
194        return (
195            self.bottom <= other.bottom
196            and other.top <= self.top
197            and self.left <= other.left
198            and other.right <= self.right
199        )
def overlaps(self, other) -> bool:
201    def overlaps(self, other) -> bool:
202        return self.contains(other.p0) or self.contains(other.p1)
def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
204    def isclose(self, other, rtol: float = 1e-09, atol: float = 0.0) -> bool:
205        return self.p0.isclose(other.p0, rtol, atol) and self.p1.isclose(other.p1, rtol, atol)
midpoint: Point
207    @cached_property
208    def midpoint(self) -> Point:
209        return Point((self.p1.x + self.p0.x) / 2, (self.p1.y + self.p0.y) / 2)
points: list[Point]
211    @cached_property
212    def points(self) -> list[Point]:
213        return [self.p0, Point(self.right, self.bottom), self.p1, Point(self.left, self.top)]
def offset(self, offset):
215    def offset(self, offset):
216        return Rectangle(self.p0.x - offset, self.p0.y - offset, self.p1.x + offset, self.p1.y + offset)
def offset_x(self, offset):
218    def offset_x(self, offset):
219        return Rectangle(self.p0.x - offset, self.p0.y, self.p1.x + offset, self.p1.y)
def offset_y(self, offset):
221    def offset_y(self, offset):
222        return Rectangle(self.p0.x, self.p0.y - offset, self.p1.x, self.p1.y + offset)
def translated(self, point):
224    def translated(self, point):
225        return Rectangle(self.p0.x + point.x, self.p0.y + point.y, self.p1.x + point.x, self.p1.y + point.y)
def rotated(self, rotation):
227    def rotated(self, rotation):
228        cos = math.cos(math.radians(rotation))
229        sin = math.sin(math.radians(rotation))
230        return Rectangle(
231            self.p0.x * cos - self.p0.y * sin,
232            self.p0.x * sin + self.p0.y * cos,
233            self.p1.x * cos - self.p1.y * sin,
234            self.p1.x * sin + self.p1.y * cos,
235        )
def joined(self, other):
237    def joined(self, other):
238        return Rectangle(
239            min(self.p0.x, other.p0.x),
240            min(self.p0.y, other.p0.y),
241            max(self.p1.x, other.p1.x),
242            max(self.p1.y, other.p1.y),
243        )
def round(self, accuracy=0):
245    def round(self, accuracy=0):
246        return Rectangle(
247            round(self.p0.x, accuracy),
248            round(self.p0.y, accuracy),
249            round(self.p1.x, accuracy),
250            round(self.p1.y, accuracy),
251        )
class Region:
260class Region:
261    def __init__(self, v0, v1, obj=None):
262        if v0 > v1:
263            v0, v1 = v1, v0
264        self.v0 = v0
265        self.v1 = v1
266        self.objs = [] if obj is None else [obj]
267        self.subregions = []
268
269    def overlaps(self, o0, o1, atol=0) -> bool:
270        if o0 > o1:
271            o0, o1 = o1, o0
272        # if reg top is lower then o0
273        if (self.v1 + atol) <= o0:
274            return False
275        # if reg bottom is higher than o1
276        if o1 <= (self.v0 - atol):
277            return False
278        return True
279
280    def contains(self, v, atol=0) -> bool:
281        return self.v0 - atol <= v <= self.v1 + atol
282
283    @property
284    def delta(self) -> float:
285        return self.v1 - self.v0
286
287    def __repr__(self):
288        r = f"<{int(self.v0)}->{int(self.v1)}"
289        if self.objs:
290            r += f"|{len(self.objs)}"
291        if self.subregions:
292            r += f"|{repr(self.subregions)}"
293        return r + ">"
Region(v0, v1, obj=None)
261    def __init__(self, v0, v1, obj=None):
262        if v0 > v1:
263            v0, v1 = v1, v0
264        self.v0 = v0
265        self.v1 = v1
266        self.objs = [] if obj is None else [obj]
267        self.subregions = []
v0
v1
objs
subregions
def overlaps(self, o0, o1, atol=0) -> bool:
269    def overlaps(self, o0, o1, atol=0) -> bool:
270        if o0 > o1:
271            o0, o1 = o1, o0
272        # if reg top is lower then o0
273        if (self.v1 + atol) <= o0:
274            return False
275        # if reg bottom is higher than o1
276        if o1 <= (self.v0 - atol):
277            return False
278        return True
def contains(self, v, atol=0) -> bool:
280    def contains(self, v, atol=0) -> bool:
281        return self.v0 - atol <= v <= self.v1 + atol
delta: float
283    @property
284    def delta(self) -> float:
285        return self.v1 - self.v0
def list_lstrip(input_values: list, strip_fn) -> list:
10def list_lstrip(input_values: list, strip_fn) -> list:
11    ii = 0
12    for value in input_values:
13        if strip_fn(value):
14            ii += 1
15        else:
16            break
17    return input_values[ii:]
def list_rstrip(input_values: list, strip_fn) -> list:
20def list_rstrip(input_values: list, strip_fn) -> list:
21    ii = 0
22    for value in reversed(input_values):
23        if strip_fn(value):
24            ii += 1
25        else:
26            break
27    return input_values[: len(input_values) - ii]
def list_strip(input_values: list, strip_fn) -> list:
30def list_strip(input_values: list, strip_fn) -> list:
31    return list_rstrip(list_lstrip(input_values, strip_fn), strip_fn)
def pkg_file_exists(pkg, file: pathlib.Path) -> bool:
34def pkg_file_exists(pkg, file: Path) -> bool:
35    return files(pkg).joinpath(file).is_file()
def pkg_apply_patch(pkg, patch: pathlib.Path, base_dir: pathlib.Path) -> bool:
38def pkg_apply_patch(pkg, patch: Path, base_dir: Path) -> bool:
39    with as_file(files(pkg).joinpath(patch)) as patch_file:
40        return apply_patch(patch_file, base_dir)
def apply_patch(patch_file: pathlib.Path, base_dir: pathlib.Path) -> bool:
43def apply_patch(patch_file: Path, base_dir: Path) -> bool:
44    cmds = [
45        "patch",
46        "--strip=1",
47        "--silent",
48        "--ignore-whitespace",
49        "--reject-file=-",
50        "--forward",
51        "--posix",
52        "--directory",
53        Path(base_dir).absolute(),
54        "--input",
55        Path(patch_file).absolute(),
56    ]
57    if subprocess.run(cmds + ["--dry-run"]).returncode:
58        return False
59    return subprocess.run(cmds).returncode == 0
class ReversePreOrderIter(anytree.iterators.abstractiter.AbstractIter):
 8class ReversePreOrderIter(AbstractIter):
 9    @staticmethod
10    def _iter(children, filter_, stop, maxlevel):
11        for child_ in reversed(children):
12            if not AbstractIter._abort_at_level(2, maxlevel):
13                descendantmaxlevel = maxlevel - 1 if maxlevel else None
14                yield from ReversePreOrderIter._iter(child_.children, filter_, stop, descendantmaxlevel)
15                if stop(child_):
16                    continue
17                if filter_(child_):
18                    yield child_

Iterate over tree starting at node.

Base class for all iterators.

Keyword Args: filter_: function called with every node as argument, node is returned if True. stop: stop iteration at node if stop function returns True for node. maxlevel (int): maximum descending in the node hierarchy.

Inherited Members
anytree.iterators.abstractiter.AbstractIter
AbstractIter
node
filter_
stop
maxlevel
def root_path(path) -> pathlib.Path:
8def root_path(path) -> Path:
9    return Path(__file__).parents[2] / path
def ext_path(path) -> pathlib.Path:
12def ext_path(path) -> Path:
13    return root_path(Path("ext") / path)
def cache_path(path) -> pathlib.Path:
16def cache_path(path) -> Path:
17    return ext_path(Path("cache") / path)
def patch_path(path) -> pathlib.Path:
20def patch_path(path) -> Path:
21    return root_path(Path("patches") / path)
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 Exception:
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 Exception:
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)