Skip to content

Commit bef01b5

Browse files
committed
Add type-hints
1 parent 3079988 commit bef01b5

File tree

13 files changed

+89
-35
lines changed

13 files changed

+89
-35
lines changed

dissect/util/_build.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from typing import Any
5+
6+
import setuptools.build_meta
7+
from setuptools.build_meta import * # noqa: F403
8+
9+
10+
def get_requires_for_build_wheel(config_settings: dict[str, Any] | None = None) -> list[str]:
11+
base = setuptools.build_meta.get_requires_for_build_wheel(config_settings)
12+
13+
if os.getenv("BUILD_RUST", "").lower() in {"1", "true"} or (config_settings and "--build-rust" in config_settings):
14+
return [*base, "setuptools-rust"]
15+
16+
return base

dissect/util/_native.src/src/compression/lz4.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ use pyo3::types::{PyByteArray, PyBytes};
44

55
const MAX_DISCOVER_OUTPUT_SIZE: usize = 1024 * 1024 * 1024;
66

7+
/// LZ4 decompress bytes up to a certain length. Assumes no header.
8+
///
9+
/// Unlike the Python implementation, this function does not support streaming decompression
10+
/// (i.e. reading from a file-like object).
11+
///
12+
/// Args:
13+
/// src: Bytes to decompress.
14+
/// uncompressed_size: The uncompressed data size. If not provided or ``-1``, will try to discover it.
15+
/// return_bytearray: Whether to return ``bytearray`` or ``bytes``.
16+
///
17+
/// Returns:
18+
/// The decompressed data or a tuple of the decompressed data and the amount of bytes read.
19+
///
720
#[pyfunction]
821
#[pyo3(signature = (src, uncompressed_size=-1, return_bytearray=false))]
922
fn decompress(

dissect/util/_native.src/src/compression/lzo.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@ use pyo3::exceptions::PyValueError;
22
use pyo3::prelude::*;
33
use pyo3::types::PyBytes;
44

5+
/// LZO decompress from bytes. Assumes no header.
6+
///
7+
/// Arguments are largely compatible with python-lzo API.
8+
///
9+
/// Unlike the Python implementation, this function does not support streaming decompression
10+
/// (i.e. reading from a file-like object).
11+
/// Args:
12+
/// src: Bytes to decompress.
13+
/// header: Whether the metadata header is included in the input.
14+
/// buflen: If ``header`` is ``False``, a buffer length in bytes must be given that will fit the output.
15+
///
16+
/// Returns:
17+
/// The decompressed data.
18+
///
519
#[pyfunction]
620
#[pyo3(signature = (src, header=true, buflen=-1))]
721
fn decompress(

dissect/util/_native.src/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use pyo3::prelude::*;
22

33
mod compression;
44

5-
65
#[pymodule]
76
fn _native(m: &Bound<'_, PyModule>) -> PyResult<()> {
87
compression::create_submodule(m)?;

dissect/util/_native/__init__.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from dissect.util._native import compression
2+
3+
__all__ = ["compression"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from dissect.util._native.compression import lz4, lzo
2+
3+
__all__ = ["lz4", "lzo"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from typing import BinaryIO
2+
3+
def decompress(
4+
src: bytes | BinaryIO,
5+
uncompressed_size: int = -1,
6+
return_bytearray: bool = False,
7+
) -> bytes | tuple[bytes, int]: ...
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import BinaryIO
2+
3+
def decompress(src: bytes | BinaryIO, header: bool = True, buflen: int = -1) -> bytes: ...

dissect/util/compression/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
lz4 = lz4_native = _native.compression.lz4
2727
lzo = lzo_native = _native.compression.lzo
28-
except ImportError:
28+
except (ImportError, AttributeError):
2929
lz4_native = lzo_native = None
3030

3131
__all__ = [

dissect/util/compression/lz4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ def decompress(
2929
uncompressed_size: int = -1,
3030
return_bytearray: bool = False,
3131
) -> bytes | tuple[bytes, int]:
32-
"""LZ4 decompress from a file-like object up to a certain length. Assumes no header.
32+
"""LZ4 decompress from a file-like object or bytes up to a certain length. Assumes no header.
3333
3434
Args:
35-
src: File-like object to decompress from.
35+
src: File-like object or bytes to decompress from.
3636
uncompressed_size: Ignored, present for compatibility with native lz4.
3737
return_bytearray: Whether to return ``bytearray`` or ``bytes``.
3838

0 commit comments

Comments
 (0)