Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 8cb0261

Browse files
authored
Change linter to Ruff (#42)
1 parent b052185 commit 8cb0261

21 files changed

+227
-145
lines changed

dissect/esedb/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
)
88

99
__all__ = [
10-
"EseDB",
11-
"Error",
1210
"CompressedTaggedDataError",
11+
"Error",
12+
"EseDB",
1313
"InvalidDatabase",
1414
"KeyNotFoundError",
1515
"NoNeighbourPageError",

dissect/esedb/c_esedb.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
from __future__ import annotations
2+
13
import datetime
24
import struct
35
import uuid
4-
from collections import namedtuple
5-
from typing import Union
6+
from typing import NamedTuple, Union
67

78
from dissect.cstruct import cstruct
89

@@ -493,7 +494,12 @@ def checksum_xor(data: bytes, initial: int = 0x89ABCDEF) -> int:
493494
return digest
494495

495496

496-
ColumnType = namedtuple("ColumnType", ["value", "name", "size", "parse"])
497+
class ColumnType(NamedTuple):
498+
value: int
499+
name: str
500+
size: int | None
501+
parse: callable | None
502+
497503

498504
COLUMN_TYPES = [
499505
ColumnType(JET_coltyp.Nil, "NULL", 0, None),

dissect/esedb/compression.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
2+
13
import struct
2-
from typing import Optional
34

45
from dissect.util.compression import lzxpress, sevenbit
56

@@ -18,18 +19,17 @@ def decompress(buf: bytes) -> bytes:
1819
identifier = buf[0] >> 3
1920
if identifier == COMPRESSION_SCHEME.COMPRESS_7BITASCII:
2021
return sevenbit.decompress(buf[1:])
21-
elif identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
22+
if identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
2223
return sevenbit.decompress(buf[1:], wide=True)
23-
elif identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
24+
if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
2425
return lzxpress.decompress(buf[3:])
25-
elif identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
26+
if identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
2627
raise NotImplementedError(f"Compression not yet implemented: {COMPRESSION_SCHEME(identifier)}")
27-
else:
28-
# Not compressed
29-
return buf
28+
# Not compressed
29+
return buf
3030

3131

32-
def decompress_size(buf: bytes) -> Optional[int]:
32+
def decompress_size(buf: bytes) -> int | None:
3333
"""Return the decompressed size of the given bytes according to the encoded compression scheme.
3434
3535
Args:
@@ -41,10 +41,10 @@ def decompress_size(buf: bytes) -> Optional[int]:
4141
identifier = buf[0] >> 3
4242
if identifier == COMPRESSION_SCHEME.COMPRESS_7BITASCII:
4343
return ((buf[0] & 7) + (8 * len(buf))) // 7
44-
elif identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
44+
if identifier == COMPRESSION_SCHEME.COMPRESS_7BITUNICODE:
4545
return 2 * (((buf[0] & 7) + (8 * len(buf))) // 7)
46-
elif identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
46+
if identifier == COMPRESSION_SCHEME.COMPRESS_XPRESS:
4747
return struct.unpack("<H", buf[1:2])[0]
48-
elif identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
48+
if identifier in (COMPRESSION_SCHEME.COMPRESS_XPRESS9, COMPRESSION_SCHEME.COMPRESS_XPRESS10):
4949
raise NotImplementedError(f"Compression not yet implemented: {COMPRESSION_SCHEME(identifier)}")
5050
return None

dissect/esedb/cursor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING, Union
3+
from typing import TYPE_CHECKING
44

55
from dissect.esedb.exceptions import KeyNotFoundError, NoNeighbourPageError
6-
from dissect.esedb.page import Node, Page
76

87
if TYPE_CHECKING:
98
from dissect.esedb.esedb import EseDB
9+
from dissect.esedb.page import Node, Page
1010

1111

1212
class Cursor:
@@ -17,7 +17,7 @@ class Cursor:
1717
page: The page to open a cursor on.
1818
"""
1919

20-
def __init__(self, esedb: EseDB, page: Union[int, Page]):
20+
def __init__(self, esedb: EseDB, page: int | Page):
2121
self.esedb = esedb
2222

2323
if isinstance(page, int):

dissect/esedb/esedb.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
# Extensible Storage Engine (ESE) Database implementation
22
# Combination of pre-source release reverse engineering and post-source release cleanup
33
# Reference: https://github.com/microsoft/Extensible-Storage-Engine
4+
from __future__ import annotations
45

6+
import io
57
from functools import cached_property, lru_cache
6-
from typing import BinaryIO, Iterator
8+
from typing import TYPE_CHECKING, BinaryIO
79

810
from dissect.esedb.c_esedb import c_esedb, pgnoFDPMSO, ulDAEMagic
911
from dissect.esedb.exceptions import InvalidDatabase
1012
from dissect.esedb.page import Page
1113
from dissect.esedb.table import Catalog, Table
1214

15+
if TYPE_CHECKING:
16+
from collections.abc import Iterator
17+
1318

1419
class EseDB:
1520
"""EseDB class.
@@ -98,10 +103,6 @@ def page(self, num: int) -> Page:
98103

99104
def pages(self) -> Iterator[Page]:
100105
"""Iterate over all pages."""
101-
num = 1
102-
while True:
103-
try:
104-
yield self.page(num)
105-
num += 1
106-
except IndexError:
107-
break
106+
size = self.fh.seek(0, io.SEEK_END)
107+
for i in range(1, (size // self.page_size) - 1):
108+
yield self.page(i)

dissect/esedb/index.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import struct
44
import uuid
55
from functools import cached_property
6-
from typing import TYPE_CHECKING, Union
6+
from typing import TYPE_CHECKING
77

88
from dissect.esedb.c_esedb import CODEPAGE, JET_bitIndex, JET_coltyp, RecordValue
99
from dissect.esedb.cursor import Cursor
1010
from dissect.esedb.lcmapstring import map_string
11-
from dissect.esedb.page import Node, Page
1211
from dissect.esedb.record import Record
1312

1413
if TYPE_CHECKING:
14+
from dissect.esedb.page import Node, Page
1515
from dissect.esedb.table import Column, Table
1616

1717

1818
JET_cbKeyMost_OLD = 255
1919

2020

21-
class Index(object):
21+
class Index:
2222
"""Represents an index on a table.
2323
2424
This is still very much WIP but works for basic indexes.
@@ -266,7 +266,7 @@ def _encode_text(index: Index, column: Column, value: str, max_size: int) -> byt
266266
return bytes(key)
267267

268268

269-
def _encode_guid(value: Union[str, uuid.UUID]) -> bytes:
269+
def _encode_guid(value: str | uuid.UUID) -> bytes:
270270
if isinstance(value, str):
271271
value = uuid.UUID(value)
272272
guid_bytes = value.bytes_le
@@ -278,6 +278,5 @@ def _flip_bits(value: int, size: int) -> int:
278278
if value & (1 << (size - 1)):
279279
# If the high bit is set, all bits are flipped
280280
return ~value & ((1 << size) - 1)
281-
else:
282-
# Otherwise only the high bit is flipped
283-
return value ^ (1 << (size - 1))
281+
# Otherwise only the high bit is flipped
282+
return value ^ (1 << (size - 1))

dissect/esedb/lcmapstring.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def map_string(value: str, flags: MapFlags, locale: str) -> bytes:
181181
key_case.append(case_weight)
182182
continue
183183

184-
if script_member == SCRIPT.DIGIT and 0:
184+
if script_member == SCRIPT.DIGIT:
185185
raise NotImplementedError(SCRIPT.DIGIT)
186186

187187
# else
@@ -191,10 +191,7 @@ def map_string(value: str, flags: MapFlags, locale: str) -> bytes:
191191
key_case.append(case_weight)
192192

193193
key_diacritic = _filter_weights(key_diacritic)
194-
if flags & (MapFlags.NORM_IGNORECASE | MapFlags.NORM_IGNOREWIDTH):
195-
key_case = []
196-
else:
197-
key_case = _filter_weights(key_case)
194+
key_case = [] if flags & (MapFlags.NORM_IGNORECASE | MapFlags.NORM_IGNOREWIDTH) else _filter_weights(key_case)
198195

199196
return bytes(
200197
[
@@ -212,7 +209,7 @@ def map_string(value: str, flags: MapFlags, locale: str) -> bytes:
212209
)
213210

214211

215-
def _filter_weights(weights):
212+
def _filter_weights(weights: list[int]) -> list[int]:
216213
i = len(weights)
217214
while i > 0:
218215
if weights[i - 1] > 2:

dissect/esedb/page.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import struct
44
from functools import cached_property
5-
from typing import TYPE_CHECKING, Iterator, Optional, Union
5+
from typing import TYPE_CHECKING
66

77
from dissect.esedb.c_esedb import PAGE_FLAG, TAG_FLAG, c_esedb
88

99
if TYPE_CHECKING:
10+
from collections.abc import Iterator
11+
1012
from dissect.esedb.esedb import EseDB
1113

1214

@@ -81,10 +83,12 @@ def is_branch(self) -> bool:
8183
return not self.is_leaf
8284

8385
@cached_property
84-
def key_prefix(self) -> Optional[bytes]:
86+
def key_prefix(self) -> bytes | None:
8587
if not self.is_root:
8688
return bytes(self.tag(0).data)
8789

90+
return None
91+
8892
def tag(self, num: int) -> Tag:
8993
"""Retrieve a tag by index.
9094
@@ -104,7 +108,7 @@ def tags(self) -> Iterator[Tag]:
104108
for i in range(1, self.tag_count):
105109
yield self.tag(i)
106110

107-
def node(self, num: int) -> Union[BranchNode, LeafNode]:
111+
def node(self, num: int) -> BranchNode | LeafNode:
108112
"""Retrieve a node by index.
109113
110114
Nodes are just tags, but indexed from the first tag.
@@ -123,7 +127,7 @@ def node(self, num: int) -> Union[BranchNode, LeafNode]:
123127

124128
return self._node_cache[num]
125129

126-
def nodes(self) -> Iterator[Union[BranchNode, LeafNode]]:
130+
def nodes(self) -> Iterator[BranchNode | LeafNode]:
127131
"""Yield all nodes."""
128132
for i in range(self.node_count):
129133
yield self.node(i)
@@ -152,8 +156,7 @@ def iter_leaf_nodes(self) -> Iterator[LeafNode]:
152156
yield leaf
153157

154158
if self.is_root and leaf and leaf.tag.page.next_page:
155-
for leaf in esedb.page(leaf.tag.page.next_page).iter_leaf_nodes():
156-
yield leaf
159+
yield from esedb.page(leaf.tag.page.next_page).iter_leaf_nodes()
157160

158161
def __repr__(self) -> str:
159162
return f"<Page num={self.num:d}>"
@@ -167,7 +170,7 @@ class Tag:
167170
num: The tag number to parse.
168171
"""
169172

170-
__slots__ = ("page", "num", "tag", "offset", "size", "data", "flags")
173+
__slots__ = ("data", "flags", "num", "offset", "page", "size", "tag")
171174

172175
def __init__(self, page: Page, num: int):
173176
self.page = page
@@ -210,7 +213,7 @@ class Node:
210213
tag: The :class:`Tag` to parse a node from.
211214
"""
212215

213-
__slots__ = ("tag", "num", "key", "key_prefix", "key_suffix", "data")
216+
__slots__ = ("data", "key", "key_prefix", "key_suffix", "num", "tag")
214217

215218
def __init__(self, tag: Tag):
216219
self.tag = tag

0 commit comments

Comments
 (0)