Skip to content

Commit 5b234bb

Browse files
committed
Change linter to Ruff
1 parent 7ac7a3a commit 5b234bb

26 files changed

+290
-216
lines changed

dissect/util/compression/lz4.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
from __future__ import annotations
2+
13
import io
24
import struct
3-
from typing import BinaryIO, Union
5+
from typing import BinaryIO
46

57
from dissect.util.exceptions import CorruptDataError
68

@@ -23,12 +25,12 @@ def _get_length(src: BinaryIO, length: int) -> int:
2325

2426

2527
def decompress(
26-
src: Union[bytes, BinaryIO],
28+
src: bytes | BinaryIO,
2729
uncompressed_size: int = -1,
2830
max_length: int = -1,
2931
return_bytearray: bool = False,
3032
return_bytes_read: bool = False,
31-
) -> Union[bytes, tuple[bytes, int]]:
33+
) -> bytes | tuple[bytes, int]:
3234
"""LZ4 decompress from a file-like object up to a certain length. Assumes no header.
3335
3436
Args:
@@ -92,5 +94,5 @@ def decompress(
9294

9395
if return_bytes_read:
9496
return dst, src.tell() - start
95-
else:
96-
return dst
97+
98+
return dst

dissect/util/compression/lznt1.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Reference: https://github.com/google/rekall/blob/master/rekall-core/rekall/plugins/filesystems/lznt1.py
2+
from __future__ import annotations
3+
24
import array
35
import io
46
import struct
5-
from typing import BinaryIO, Union
7+
from typing import BinaryIO
68

79

810
def _get_displacement(offset: int) -> int:
@@ -20,10 +22,10 @@ def _get_displacement(offset: int) -> int:
2022
COMPRESSED_MASK = 1 << 15
2123
SIGNATURE_MASK = 3 << 12
2224
SIZE_MASK = (1 << 12) - 1
23-
TAG_MASKS = [(1 << i) for i in range(0, 8)]
25+
TAG_MASKS = [(1 << i) for i in range(8)]
2426

2527

26-
def decompress(src: Union[bytes, BinaryIO]) -> bytes:
28+
def decompress(src: bytes | BinaryIO) -> bytes:
2729
"""LZNT1 decompress from a file-like object or bytes.
2830
2931
Args:
@@ -87,5 +89,4 @@ def decompress(src: Union[bytes, BinaryIO]) -> bytes:
8789
data = src.read(hsize + 1)
8890
dst.write(data)
8991

90-
result = dst.getvalue()
91-
return result
92+
return dst.getvalue()

dissect/util/compression/lzo.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
# - https://github.com/FFmpeg/FFmpeg/blob/master/libavutil/lzo.c
33
# - https://docs.kernel.org/staging/lzo.html
44
# - https://github.com/torvalds/linux/blob/master/lib/lzo/lzo1x_decompress_safe.c
5+
from __future__ import annotations
56

67
import io
78
import struct
8-
from typing import BinaryIO, Union
9+
from typing import BinaryIO
910

1011
MAX_READ_LENGTH = (1 << 32) - 1000
1112

@@ -22,7 +23,7 @@ def _read_length(src: BinaryIO, val: int, mask: int) -> int:
2223
return length + mask + val
2324

2425

25-
def decompress(src: Union[bytes, BinaryIO], header: bool = True, buflen: int = -1) -> bytes:
26+
def decompress(src: bytes | BinaryIO, header: bool = True, buflen: int = -1) -> bytes:
2627
"""LZO decompress from a file-like object or bytes. Assumes no header.
2728
2829
Arguments are largely compatible with python-lzo API.

dissect/util/compression/lzxpress.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Reference: [MS-XCA]
2+
from __future__ import annotations
3+
24
import io
35
import struct
4-
from typing import BinaryIO, Union
6+
from typing import BinaryIO
57

68

7-
def decompress(src: Union[bytes, BinaryIO]) -> bytes:
9+
def decompress(src: bytes | BinaryIO) -> bytes:
810
"""LZXPRESS decompress from a file-like object or bytes.
911
1012
Args:
@@ -62,7 +64,7 @@ def decompress(src: Union[bytes, BinaryIO]) -> bytes:
6264
match_length = struct.unpack("<I", src.read(4))[0]
6365

6466
if match_length < 15 + 7:
65-
raise Exception("wrong match length")
67+
raise ValueError("wrong match length")
6668

6769
match_length -= 15 + 7
6870
match_length += 15

dissect/util/compression/lzxpress_huffman.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-frs2/8cb5bae9-edf3-4833-9f0a-9d7e24218d3d
22
# https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-XCA/[MS-XCA].pdf
3+
from __future__ import annotations
34

45
import io
56
import struct
6-
from collections import namedtuple
7-
from typing import BinaryIO, Optional, Union
7+
from typing import BinaryIO, NamedTuple
88

9-
Symbol = namedtuple("Symbol", ["length", "symbol"])
9+
10+
class Symbol(NamedTuple):
11+
length: int
12+
symbol: int
1013

1114

1215
def _read_16_bit(fh: BinaryIO) -> int:
@@ -16,7 +19,7 @@ def _read_16_bit(fh: BinaryIO) -> int:
1619
class Node:
1720
__slots__ = ("symbol", "is_leaf", "children")
1821

19-
def __init__(self, symbol: Optional[Symbol] = None, is_leaf: bool = False):
22+
def __init__(self, symbol: Symbol | None = None, is_leaf: bool = False):
2023
self.symbol = symbol
2124
self.is_leaf = is_leaf
2225
self.children = [None, None]
@@ -120,7 +123,7 @@ def decode(self, root: Node) -> Symbol:
120123
return node.symbol
121124

122125

123-
def decompress(src: Union[bytes, BinaryIO]) -> bytes:
126+
def decompress(src: bytes | BinaryIO) -> bytes:
124127
"""LZXPRESS decompress from a file-like object or bytes.
125128
126129
Decompresses until EOF of the input data.

dissect/util/compression/sevenbit.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from __future__ import annotations
2+
13
from io import BytesIO
2-
from typing import BinaryIO, Union
4+
from typing import BinaryIO
35

46

5-
def compress(src: Union[bytes, BinaryIO]) -> bytes:
7+
def compress(src: bytes | BinaryIO) -> bytes:
68
"""Sevenbit compress from a file-like object or bytes.
79
810
Args:
@@ -37,7 +39,7 @@ def compress(src: Union[bytes, BinaryIO]) -> bytes:
3739
return bytes(dst)
3840

3941

40-
def decompress(src: Union[bytes, BinaryIO], wide: bool = False) -> bytes:
42+
def decompress(src: bytes | BinaryIO, wide: bool = False) -> bytes:
4143
"""Sevenbit decompress from a file-like object or bytes.
4244
4345
Args:

dissect/util/cpio.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import stat
24
import struct
35
import tarfile
@@ -38,7 +40,7 @@ class CpioInfo(tarfile.TarInfo):
3840
"""
3941

4042
@classmethod
41-
def fromtarfile(cls, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
43+
def fromtarfile(cls, tarfile: tarfile.TarFile) -> CpioInfo:
4244
if tarfile.format not in (
4345
FORMAT_CPIO_BIN,
4446
FORMAT_CPIO_ODC,
@@ -64,7 +66,7 @@ def fromtarfile(cls, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
6466
return obj._proc_member(tarfile)
6567

6668
@classmethod
67-
def frombuf(cls, buf: bytes, format: int, encoding: str, errors: str) -> tarfile.TarInfo:
69+
def frombuf(cls, buf: bytes, format: int, encoding: str, errors: str) -> CpioInfo:
6870
if format in (FORMAT_CPIO_BIN, FORMAT_CPIO_ODC, FORMAT_CPIO_HPBIN, FORMAT_CPIO_HPODC):
6971
obj = cls._old_frombuf(buf, format)
7072
elif format in (FORMAT_CPIO_NEWC, FORMAT_CPIO_CRC):
@@ -78,7 +80,7 @@ def frombuf(cls, buf: bytes, format: int, encoding: str, errors: str) -> tarfile
7880
return obj
7981

8082
@classmethod
81-
def _old_frombuf(cls, buf: bytes, format: int):
83+
def _old_frombuf(cls, buf: bytes, format: int) -> CpioInfo:
8284
if format in (FORMAT_CPIO_BIN, FORMAT_CPIO_HPBIN):
8385
values = list(struct.unpack("<13H", buf))
8486
if values[0] == _swap16(CPIO_MAGIC_OLD):
@@ -131,7 +133,7 @@ def _old_frombuf(cls, buf: bytes, format: int):
131133
return obj
132134

133135
@classmethod
134-
def _new_frombuf(cls, buf: bytes, format: int):
136+
def _new_frombuf(cls, buf: bytes, format: int) -> CpioInfo:
135137
values = struct.unpack("<6s8s8s8s8s8s8s8s8s8s8s8s8s8s", buf)
136138
values = [int(values[0], 8)] + [int(v, 16) for v in values[1:]]
137139
if values[0] not in (CPIO_MAGIC_NEW, CPIO_MAGIC_CRC):
@@ -157,7 +159,7 @@ def _new_frombuf(cls, buf: bytes, format: int):
157159

158160
return obj
159161

160-
def _proc_member(self, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
162+
def _proc_member(self, tarfile: tarfile.TarFile) -> CpioInfo | None:
161163
self.name = tarfile.fileobj.read(self.namesize - 1).decode(tarfile.encoding, tarfile.errors)
162164
if self.name == "TRAILER!!!":
163165
# The last entry in a cpio file has the special name ``TRAILER!!!``, indicating the end of the archive
@@ -177,10 +179,11 @@ def _proc_member(self, tarfile: tarfile.TarFile) -> tarfile.TarInfo:
177179
def _round_word(self, offset: int) -> int:
178180
if self.format in (FORMAT_CPIO_BIN, FORMAT_CPIO_HPBIN):
179181
return (offset + 1) & ~0x01
180-
elif self.format in (FORMAT_CPIO_NEWC, FORMAT_CPIO_CRC):
182+
183+
if self.format in (FORMAT_CPIO_NEWC, FORMAT_CPIO_CRC):
181184
return (offset + 3) & ~0x03
182-
else:
183-
return offset
185+
186+
return offset
184187

185188
def issocket(self) -> bool:
186189
"""Return True if it is a socket."""
@@ -211,13 +214,13 @@ def _swap16(value: int) -> int:
211214
return ((value & 0xFF) << 8) | (value >> 8)
212215

213216

214-
def CpioFile(*args, **kwargs):
217+
def CpioFile(*args, **kwargs) -> tarfile.TarFile:
215218
"""Utility wrapper around ``tarfile.TarFile`` to easily open cpio archives."""
216219
kwargs.setdefault("format", FORMAT_CPIO_UNKNOWN)
217220
return tarfile.TarFile(*args, **kwargs, tarinfo=CpioInfo)
218221

219222

220-
def open(*args, **kwargs):
223+
def open(*args, **kwargs) -> tarfile.TarFile:
221224
"""Utility wrapper around ``tarfile.open`` to easily open cpio archives."""
222225
kwargs.setdefault("format", FORMAT_CPIO_UNKNOWN)
223226
return tarfile.open(*args, **kwargs, tarinfo=CpioInfo)

dissect/util/encoding/surrogateescape.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import codecs
22

33

4-
def error_handler(error):
4+
def error_handler(error: Exception) -> tuple[str, int]:
55
if not isinstance(error, UnicodeDecodeError):
66
raise error
77

dissect/util/feature.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
from __future__ import annotations
2+
13
import functools
24
import os
35
from enum import Enum
4-
from typing import Callable, Optional
6+
from typing import Callable, NoReturn
57

68

79
# Register feature flags in a central place to avoid chaos
@@ -43,7 +45,7 @@ def parse_blob():
4345
return feature in feature_flags()
4446

4547

46-
def feature(flag: Feature, alternative: Optional[Callable] = None) -> Callable:
48+
def feature(flag: Feature, alternative: Callable | None = None) -> Callable:
4749
"""Feature flag decorator allowing you to guard a function behind a feature flag.
4850
4951
Usage::
@@ -57,7 +59,7 @@ def my_func( ... ) -> ...
5759

5860
if alternative is None:
5961

60-
def alternative():
62+
def alternative() -> NoReturn:
6163
raise FeatureException(
6264
"\n".join(
6365
[
@@ -68,7 +70,7 @@ def alternative():
6870
)
6971
)
7072

71-
def decorator(func):
73+
def decorator(func: Callable) -> Callable:
7274
return func if feature_enabled(flag) else alternative
7375

7476
return decorator

0 commit comments

Comments
 (0)