Skip to content

Commit ad8ca2f

Browse files
authored
Change linter to Ruff (#49)
1 parent 54a733b commit ad8ca2f

35 files changed

+447
-306
lines changed

dissect/hypervisor/descriptor/c_hyperv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from dissect.cstruct import cstruct
24

35
hyperv_def = """

dissect/hypervisor/descriptor/hyperv.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from __future__ import annotations
44

55
import struct
6-
from collections.abc import ItemsView, KeysView, ValuesView
7-
from typing import BinaryIO, Optional, Union
6+
from typing import TYPE_CHECKING, BinaryIO
87

98
from dissect.util.stream import RangeStream
109

@@ -16,6 +15,9 @@
1615
)
1716
from dissect.hypervisor.exceptions import InvalidSignature
1817

18+
if TYPE_CHECKING:
19+
from collections.abc import ItemsView, KeysView, ValuesView
20+
1921

2022
class HyperVFile:
2123
"""HyperVFile implementation.
@@ -278,7 +280,7 @@ def __repr__(self) -> str:
278280
return f"<HyperVStorageKeyTableEntry type={self.type} size={self.size}>"
279281

280282
@property
281-
def parent(self) -> Optional[HyperVStorageKeyTableEntry]:
283+
def parent(self) -> HyperVStorageKeyTableEntry | None:
282284
"""Return the entry parent, if there is any.
283285
284286
Requires that all key tables are loaded.
@@ -333,8 +335,8 @@ def data(self) -> memoryview:
333335
file_object = self.get_file_object()
334336
# This memoryview has no purpose, only do it so the return value type is consistent
335337
return memoryview(file_object.read(size))
336-
else:
337-
return self.raw[self.header.data_offset :]
338+
339+
return self.raw[self.header.data_offset :]
338340

339341
@property
340342
def key(self) -> str:
@@ -343,7 +345,7 @@ def key(self) -> str:
343345
return self.raw.tobytes()[: self.header.data_offset - 1].decode("utf-8")
344346

345347
@property
346-
def value(self) -> Union[int, bytes, str]:
348+
def value(self) -> int | bytes | str:
347349
"""Return a Python native value for this entry."""
348350
data = self.data
349351

@@ -369,6 +371,8 @@ def value(self) -> Union[int, bytes, str]:
369371
if self.type == KeyDataType.Bool:
370372
return struct.unpack("<I", data[:4])[0] != 0
371373

374+
raise TypeError(f"Unknown data type: {self.type}")
375+
372376
@property
373377
def data_size(self) -> int:
374378
"""Return the total amount of data bytes, including the key name.
@@ -427,10 +431,7 @@ def as_dict(self) -> dict:
427431

428432
obj = {}
429433
for key, child in self.children.items():
430-
if child.type == KeyDataType.Node:
431-
value = child.as_dict()
432-
else:
433-
value = child.value
434+
value = child.as_dict() if child.type == KeyDataType.Node else child.value
434435

435436
obj[key] = value
436437

@@ -466,13 +467,10 @@ def read(self, n: int = -1) -> bytes:
466467
if n is not None and n < -1:
467468
raise ValueError("invalid number of bytes to read")
468469

469-
if n == -1:
470-
read_length = self.size
471-
else:
472-
read_length = min(n, self.size)
470+
read_length = self.size if n == -1 else min(n, self.size)
473471

474472
self.file.fh.seek(self.offset)
475473
return self.file.fh.read(read_length)
476474

477-
def open(self, size: Optional[int] = None) -> BinaryIO:
475+
def open(self, size: int | None = None) -> BinaryIO:
478476
return RangeStream(self.file.fh, self.offset, size or self.size)

dissect/hypervisor/descriptor/ovf.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
from typing import Iterator, TextIO
2-
from xml.etree.ElementTree import Element
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Final, TextIO
34

45
from defusedxml import ElementTree
56

7+
if TYPE_CHECKING:
8+
from collections.abc import Iterator
9+
from xml.etree.ElementTree import Element
10+
611

712
class OVF:
8-
NS = {
13+
NS: Final[dict[str, str]] = {
914
"ovf": "http://schemas.dmtf.org/ovf/envelope/1",
1015
"rasd": "http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData",
1116
}
@@ -34,8 +39,7 @@ def disks(self) -> Iterator[str]:
3439
for disk in self.xml.findall(self.DISK_DRIVE_XPATH, self.NS):
3540
resource = disk.find("{{{rasd}}}HostResource".format(**self.NS))
3641
xpath = resource.text
37-
if xpath.startswith("ovf:"):
38-
xpath = xpath[4:]
42+
xpath = xpath.removeprefix("ovf:")
3943

4044
if xpath.startswith("/disk/"):
4145
disk_ref = xpath.split("/")[-1]

dissect/hypervisor/descriptor/pvs.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from typing import IO, Iterator
2-
from xml.etree.ElementTree import Element
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, TextIO
34

45
from defusedxml import ElementTree
56

7+
if TYPE_CHECKING:
8+
from collections.abc import Iterator
9+
from xml.etree.ElementTree import Element
10+
611

712
class PVS:
813
"""Parallels VM settings file.
@@ -11,7 +16,7 @@ class PVS:
1116
fh: The file-like object to a PVS file.
1217
"""
1318

14-
def __init__(self, fh: IO):
19+
def __init__(self, fh: TextIO):
1520
self._xml: Element = ElementTree.fromstring(fh.read())
1621

1722
def disks(self) -> Iterator[str]:

dissect/hypervisor/descriptor/vbox.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
from typing import IO, Iterator
2-
from xml.etree.ElementTree import Element
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, TextIO
34

45
from defusedxml import ElementTree
56

7+
if TYPE_CHECKING:
8+
from collections.abc import Iterator
9+
from xml.etree.ElementTree import Element
10+
611

712
class VBox:
813
VBOX_XML_NAMESPACE = "{http://www.virtualbox.org/}"
914

10-
def __init__(self, fh: IO):
15+
def __init__(self, fh: TextIO):
1116
self._xml: Element = ElementTree.fromstring(fh.read())
1217

1318
def disks(self) -> Iterator[str]:

0 commit comments

Comments
 (0)