Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change linter to Ruff #29

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 30 additions & 19 deletions dissect/clfs/blf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import namedtuple
from typing import BinaryIO, Iterator
from __future__ import annotations

from typing import TYPE_CHECKING, BinaryIO, NamedTuple

from dissect.clfs.c_clfs import CLFS_CONTROL_RECORD_MAGIC_VALUE, BlockHeader, c_clfs
from dissect.clfs.exceptions import (
Expand All @@ -8,23 +9,33 @@
InvalidRecordBlockError,
)

Context = namedtuple("Context", ["symbol_table", "type"])
Container = namedtuple("Container", ["name", "size", "id", "type"])
Stream = namedtuple(
"Stream",
[
"name",
"id",
"file_attributes",
"type",
"lsn_archive_tail",
"lsn_base",
"lsn_last",
"lsn_flush",
"lsn_physical_base",
"offset",
],
)
if TYPE_CHECKING:
from collections.abc import Iterator

Check warning on line 13 in dissect/clfs/blf.py

View check run for this annotation

Codecov / codecov/patch

dissect/clfs/blf.py#L13

Added line #L13 was not covered by tests


class Context(NamedTuple):
symbol_table: list
type: int


class Container(NamedTuple):
name: str
size: int
id: int
type: int


class Stream(NamedTuple):
name: str
id: int
file_attributes: int
type: int
lsn_archive_tail: int
lsn_base: int
lsn_last: int
lsn_flush: int
lsn_physical_base: int
offset: int


class ControlRecord:
Expand Down
2 changes: 1 addition & 1 deletion dissect/clfs/c_clfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def __init__(self, fh: BinaryIO, offset: int):

self.data = bytes(data)

def open(self):
def open(self) -> io.BytesIO:
"""Return a file-like object of the block header."""

return io.BytesIO(self.data)
12 changes: 8 additions & 4 deletions dissect/clfs/container.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from typing import BinaryIO, Iterator, Tuple
from __future__ import annotations

from typing import TYPE_CHECKING, BinaryIO

# Local import
from dissect.clfs.c_clfs import BlockHeader, c_clfs
from dissect.clfs.exceptions import InvalidRecordBlockError

if TYPE_CHECKING:
from collections.abc import Iterator

Check warning on line 9 in dissect/clfs/container.py

View check run for this annotation

Codecov / codecov/patch

dissect/clfs/container.py#L9

Added line #L9 was not covered by tests


class Container:
"""Main class for parsing the containers that belong to a BLF file parsed in an earlier stage.
Expand All @@ -17,7 +21,7 @@
self.fh = fh
self.offset = offset

def _open_block(self, offset: int) -> Tuple[BinaryIO, int]:
def _open_block(self, offset: int) -> tuple[BinaryIO, int]:
"""Open the blockheader of every block that is present within the given container.

Returns:
Expand All @@ -36,7 +40,7 @@

return buf, cur_record_offset

def records(self) -> Iterator[Tuple[int, bytes, bytes]]:
def records(self) -> Iterator[tuple[int, bytes, bytes]]:
"""Parse the records that are present within the log block."""

log_block_offset = self.offset
Expand Down
53 changes: 48 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,56 @@ dev = [
"dissect.cstruct>=4.0.dev,<5.0.dev",
]

[tool.black]
[tool.ruff]
line-length = 120
required-version = ">=0.9.0"

[tool.isort]
profile = "black"
known_first_party = ["dissect.clfs"]
known_third_party = ["dissect"]
[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]
select = [
"F",
"E",
"W",
"I",
"UP",
"YTT",
"ANN",
"B",
"C4",
"DTZ",
"T10",
"FA",
"ISC",
"G",
"INP",
"PIE",
"PYI",
"PT",
"Q",
"RSE",
"RET",
"SLOT",
"SIM",
"TID",
"TCH",
"PTH",
"PLC",
"TRY",
"FLY",
"PERF",
"FURB",
"RUF",
]
ignore = ["E203", "B904", "UP024", "ANN002", "ANN003", "ANN204", "ANN401", "SIM105", "TRY003"]

[tool.ruff.lint.per-file-ignores]
"tests/docs/**" = ["INP001"]

[tool.ruff.lint.isort]
known-first-party = ["dissect.clfs"]
known-third-party = ["dissect"]

[tool.setuptools]
license-files = ["LICENSE", "COPYRIGHT"]
Expand Down
26 changes: 16 additions & 10 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import os
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, BinaryIO

import pytest

if TYPE_CHECKING:
from collections.abc import Iterator


def absolute_path(filename):
return os.path.join(os.path.dirname(__file__), filename)
def absolute_path(filename: str) -> Path:
return Path(__file__).parent / filename


def open_file(name, mode="rb"):
with open(absolute_path(name), mode) as f:
def open_file(name: str, mode: str = "rb") -> Iterator[BinaryIO]:
with absolute_path(name).open(mode) as f:
yield f


@pytest.fixture
def control_record_blf():
def control_record_blf() -> Iterator[BinaryIO]:
yield from open_file("data/control_record.blf")


@pytest.fixture
def bad_control_record_blf():
def bad_control_record_blf() -> Iterator[BinaryIO]:
yield from open_file("data/bad_control_record.blf")


@pytest.fixture
def invalid_control_record_blf():
def invalid_control_record_blf() -> Iterator[BinaryIO]:
yield from open_file("data/invalid_control_record.blf")


@pytest.fixture
def dummy_blf():
def dummy_blf() -> Iterator[BinaryIO]:
yield from open_file("data/DRIVERS{53b39e70-18c4-11ea-a811-000d3aa4692b}.TM.blf")


@pytest.fixture
def dummy_container():
def dummy_container() -> Iterator[BinaryIO]:
yield from open_file(
"data/DRIVERS{53b39e70-18c4-11ea-a811-000d3aa4692b}.TMContainer00000000000000000001.regtrans-ms"
)
13 changes: 9 additions & 4 deletions tests/test_container.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
from collections import namedtuple
from __future__ import annotations

from typing import BinaryIO, NamedTuple

# Local imports
from dissect.clfs.container import Container

Data = namedtuple("Data", ["offset", "r_data", "b_data"])

class Data(NamedTuple):
offset: int
r_data: bytes
b_data: bytes


def test_container_records(dummy_container):
def test_container_records(dummy_container: BinaryIO) -> None:
records = []

trans = Container(fh=dummy_container, offset=36864)
Expand Down
12 changes: 7 additions & 5 deletions tests/test_control_record.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from __future__ import annotations

from typing import BinaryIO

import pytest

from dissect.clfs.blf import BLF

# Local imports
from dissect.clfs.c_clfs import c_clfs
from dissect.clfs.exceptions import InvalidRecordBlockError


def test_control_record_c_definitions(control_record_blf):
def test_control_record_c_definitions(control_record_blf: BinaryIO) -> None:
# Seek to start of the control record
control_record_blf.seek(0x70)

Expand All @@ -28,7 +30,7 @@ def test_control_record_c_definitions(control_record_blf):
assert c_record.Blocks == 0x6


def test_control_record_blf(control_record_blf):
def test_control_record_blf(control_record_blf: BinaryIO) -> None:
blf = BLF(fh=control_record_blf)

assert blf.c_record.record.RecordHeader.DumpCount == 0x1
Expand All @@ -46,6 +48,6 @@ def test_control_record_blf(control_record_blf):
assert blf.c_record.record.Blocks == 0x6


def test_control_record_fail(bad_control_record_blf):
def test_control_record_fail(bad_control_record_blf: BinaryIO) -> None:
with pytest.raises(InvalidRecordBlockError):
BLF(fh=bad_control_record_blf)
9 changes: 6 additions & 3 deletions tests/test_record_header.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Local imports
from __future__ import annotations

from typing import BinaryIO

from dissect.clfs.c_clfs import BlockHeader, c_clfs


def test_record_header_c_definitions(control_record_blf):
def test_record_header_c_definitions(control_record_blf: BinaryIO) -> None:
r_header = c_clfs.CLFS_LOG_BLOCK_HEADER(control_record_blf)

assert r_header.MajorVersion == 0x15
Expand All @@ -21,7 +24,7 @@ def test_record_header_c_definitions(control_record_blf):
assert r_header.FixupOffset == 0x3F8


def test_record_header(control_record_blf):
def test_record_header(control_record_blf: BinaryIO) -> None:
logblock = BlockHeader(fh=control_record_blf, offset=0)

assert logblock.header.MajorVersion == 0x15
Expand Down
9 changes: 6 additions & 3 deletions tests/test_validate_blf.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from __future__ import annotations

from typing import BinaryIO

import pytest

# Local imports
from dissect.clfs.blf import BLF
from dissect.clfs.exceptions import InvalidBLFError


def test_validate(control_record_blf):
def test_validate(control_record_blf: BinaryIO) -> None:
BLF(fh=control_record_blf)


def test_validate_fail(invalid_control_record_blf):
def test_validate_fail(invalid_control_record_blf: BinaryIO) -> None:
with pytest.raises(InvalidBLFError):
BLF(fh=invalid_control_record_blf)
14 changes: 4 additions & 10 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,17 @@ commands =
[testenv:fix]
package = skip
deps =
black==23.1.0
isort==5.11.4
ruff==0.9.2
commands =
black dissect tests
isort dissect tests
ruff format dissect tests

[testenv:lint]
package = skip
deps =
black==23.1.0
flake8
flake8-black
flake8-isort
isort==5.11.4
ruff==0.9.2
vermin
commands =
flake8 dissect tests
ruff check dissect tests
vermin -t=3.9- --no-tips --lint dissect tests

[flake8]
Expand Down
Loading