Skip to content

Commit

Permalink
utils:functions - add function to check if a file is binary or not
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoCampinoti94 committed Oct 30, 2023
1 parent dae7344 commit 313e128
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions acacore/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
R = TypeVar("R")


_text_bytes: bytes = bytes([7, 8, 9, 10, 12, 13, 27, *range(0x20, 0x7F), *range(0x80, 0x100)])


def or_none(func: Callable[[T], R]) -> Callable[[T], Optional[R]]:
"""Create a lambda function of arity one that will return None if its argument is None.
Expand Down Expand Up @@ -41,3 +44,8 @@ def file_checksum(path: Path) -> str:
file_hash.update(chunk)
chunk = f.read(2**20)
return file_hash.hexdigest()


def is_binary(path: Path, chunk_size: int = 1024):
with path.open("rb") as f:
return bool(f.read(chunk_size).translate(None, _text_bytes))

0 comments on commit 313e128

Please sign in to comment.