Skip to content

Commit f3c0dac

Browse files
Add stricter Ruff linting (#79)
1 parent 8007d52 commit f3c0dac

9 files changed

Lines changed: 34 additions & 17 deletions

File tree

dissect/hypervisor/__init__.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.hypervisor.descriptor import hyperv, ovf, pvs, vbox, vmx
24
from dissect.hypervisor.disk import asif, hdd, qcow2, vdi, vhd, vhdx, vmdk
35
from dissect.hypervisor.util import envelope, vmtar

dissect/hypervisor/descriptor/vmx.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def unlock_with_phrase(self, passphrase: str) -> None:
101101
self.attr.update(**_parse_dictionary(decrypted.decode()))
102102

103103
def disks(self) -> list[str]:
104-
"""Return a list of paths to disk files"""
104+
"""Return a list of paths to disk files."""
105105
dev_classes = ("scsi", "sata", "ide", "nvme")
106106
devices = {}
107107

@@ -179,7 +179,6 @@ def unseal_with_phrase(self, passphrase: str) -> bytes:
179179
@classmethod
180180
def from_text(cls, text: str) -> KeySafe:
181181
"""Parse a ``KeySafe`` from a string."""
182-
183182
# Key safes are a list of key locators. It's a key locator string with a specific prefix
184183
identifier, _, remainder = text.partition("/")
185184
if identifier != "vmware:key":
@@ -262,7 +261,6 @@ def _parse_key_locator(locator_string: str) -> Pair | Phrase | list[Pair | Phras
262261
263262
Interally called ``KeyLocator``.
264263
"""
265-
266264
identifier, _, remainder = locator_string.partition("/")
267265

268266
if identifier == "list":
@@ -303,7 +301,6 @@ def _split_list(value: str) -> list[str]:
303301
Lists are wrapped by braces and separated by comma. They can contain nested lists/pairs,
304302
so we need to separate at the correct nest level.
305303
"""
306-
307304
if not (match := re.match(r"\((.+)\)", value)):
308305
raise ValueError("Invalid list string")
309306

@@ -337,7 +334,6 @@ def _parse_crypto_dict(dict_string: str) -> dict[str, str]:
337334
338335
Internally called ``CryptoDict``.
339336
"""
340-
341337
crypto_dict = {}
342338
for part in dict_string.split(":"):
343339
key, _, value = part.partition("=")
@@ -351,7 +347,6 @@ def _decrypt_hmac(key: bytes, data: bytes, digest: str) -> bytes:
351347
First 16 bytes of the ciphertext are the IV and the last N bytes are the HMAC digest.
352348
The cleartext is padded using PKCS#7.
353349
"""
354-
355350
digest, digest_size = HMAC_MAP[digest]
356351

357352
iv, encrypted, mac = data[:16], data[16:-digest_size], data[-digest_size:]
@@ -374,7 +369,6 @@ def _create_cipher(key: bytes, iv: bytes) -> AES.CbcMode:
374369
375370
Dynamic based on the available crypto module.
376371
"""
377-
378372
if HAS_PYSTANDALONE:
379373
if len(key) == 32:
380374
cipher = "aes-256-cbc"

dissect/hypervisor/disk/vmdk.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929

3030
class VMDK(AlignedStream):
3131
def __init__(self, fh: BinaryIO | Path | str | list[BinaryIO | Path | str]):
32-
"""
33-
Input can be a file handle to a Disk Descriptor file or a list of file handles to multiple VMDK files.
34-
"""
32+
"""Input can be a file handle to a Disk Descriptor file or a list of file handles to multiple VMDK files."""
3533
fhs = [fh] if not isinstance(fh, list) else fh
3634

3735
self.disks = []
@@ -462,7 +460,6 @@ def parse(cls, vmdk_config: str) -> DiskDescriptor:
462460
Resources:
463461
- https://github.com/libyal/libvmdk/blob/main/documentation/VMWare%20Virtual%20Disk%20Format%20(VMDK).asciidoc
464462
"""
465-
466463
descriptor_settings = {}
467464
extents: list[ExtentDescriptor] = []
468465
disk_db = {}

dissect/hypervisor/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from __future__ import annotations
2+
3+
14
class Error(Exception):
25
pass
36

dissect/hypervisor/tools/vmtar.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
import tarfile
24

35
from dissect.hypervisor.util import vmtar

dissect/hypervisor/util/vmtar.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ def visoropen(cls, name: str, mode: str = "r", fileobj: BinaryIO | None = None,
6262
raise tarfile.TarError("visor currently only supports read mode")
6363

6464
try:
65-
from gzip import GzipFile # noqa: PLC0415
65+
from gzip import GzipFile
6666
except ImportError:
6767
raise tarfile.CompressionError("gzip module is not available") from None
6868

6969
try:
70-
from lzma import LZMAError, LZMAFile # noqa: PLC0415
70+
from lzma import LZMAError, LZMAFile
7171
except ImportError:
7272
raise tarfile.CompressionError("lzma module is not available") from None
7373

pyproject.toml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,33 @@ select = [
105105
"SLOT",
106106
"SIM",
107107
"TID",
108-
"TCH",
108+
"TC",
109109
"PTH",
110110
"PLC",
111111
"TRY",
112112
"FLY",
113113
"PERF",
114114
"FURB",
115115
"RUF",
116+
"D"
116117
]
117-
ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003"]
118+
ignore = [
119+
"E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003", "PLC0415",
120+
# Ignore some pydocstyle rules for now as they require a larger cleanup
121+
"D1",
122+
"D205",
123+
"D301",
124+
"D417",
125+
# Seems bugged: https://github.com/astral-sh/ruff/issues/16824
126+
"D402",
127+
]
128+
future-annotations = true
129+
130+
[tool.ruff.lint.pydocstyle]
131+
convention = "google"
132+
133+
[tool.ruff.lint.flake8-type-checking]
134+
strict = true
118135

119136
[tool.ruff.lint.per-file-ignores]
120137
"tests/_docs/**" = ["INP001"]
@@ -123,6 +140,7 @@ ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM1
123140
[tool.ruff.lint.isort]
124141
known-first-party = ["dissect.hypervisor"]
125142
known-third-party = ["dissect"]
143+
required-imports = ["from __future__ import annotations"]
126144

127145
[tool.setuptools.packages.find]
128146
include = ["dissect.*"]

tests/_docs/conf.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
project = "dissect.hypervisor"
24

35
extensions = [

tests/disk/test_vmdk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,11 @@ def test_vmdk_sesparse() -> None:
197197
),
198198
)
199199
def test_vmdk_extent_description(extent_description: str, expected_extents: list[ExtentDescriptor]) -> None:
200-
"""test if we correctly parse VMDK sparse and flat extent descriptions.
200+
"""Test if we correctly parse VMDK sparse and flat extent descriptions.
201201
202202
Resources:
203203
- https://github.com/libyal/libvmdk/blob/main/documentation/VMWare%20Virtual%20Disk%20Format%20(VMDK).asciidoc#22-extent-descriptions
204204
"""
205-
206205
descriptor = DiskDescriptor.parse(extent_description)
207206
assert descriptor.extents == expected_extents
208207

0 commit comments

Comments
 (0)