filetypes.py¶
clew/filetypes.py
Skip files that are not text before anything parses or hashes them.
binary_file
SkippedFile | None clew.filetypes.binary_file(Path|str path)
The binary classification of one file, or None when it is text or unreadable.
Source · 13 lines
def binary_file(path: Path | str) -> SkippedFile | None:
"""@brief Classify one file, returning a SkippedFile only when it is binary."""
from lang_parsing_substrate import classify_file
try:
found = classify_file(str(path))
except OSError:
return None
if found.kind != "binary":
return None
return SkippedFile(Path(path), found.binary_kind or "unknown", found.mime)
Activity · 8 steps
Requirements
Flows
binary_files
list[SkippedFile] clew.filetypes.binary_files(list[Path] paths)
Split files into the readable ones and the binaries to skip.
Source · 16 lines
def binary_files(paths: list[Path]) -> list[SkippedFile]:
"""Each skip is logged at INFO with its classification, because a file silently
missing from an index is the failure this project has recorded most often.
@brief Classify a batch and return the binaries.
@return The skipped files.
"""
skipped = [s for s in (binary_file(p) for p in paths) if s is not None]
for s in skipped:
logger.info(
"file typing: skipping %s — %s binary%s, not source",
s.path,
s.binary_kind,
f" ({s.mime})" if s.mime else "",
)
return skipped1 more lines, not shown