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.

clew/filetypes.py:46

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
lines 48–48from lang_parsing_substrate import classify_filelines 50–50trylines 51–51found = classify_file(str(path))lines 52–52except OSErrorlines 53–53return Nonelines 54–54◇ if found.kind != "binary"lines 55–55return Nonelines 56–56return SkippedFile(Path(path), found.binary_kind or "unknown", found.mime)

Requirements

REQ-DDB-PIPE-011

Flows

binary_files

list[SkippedFile] clew.filetypes.binary_files(list[Path] paths)

Split files into the readable ones and the binaries to skip.

clew/filetypes.py:64

Show on the map →

Called by _drop_binary_files

Tested by

1 teststest_binary_files_logs_each_skip

Calls binary_file

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 skipped

1 more lines, not shown

Activity · 4 steps
lines 71–71skipped = [s for s in (binary_file(p) for p in paths) if s is not None]lines 72–72◇ for s in skippedlines 73–78logger.info( "file typing: skipping %s — %s binary%s, not source", s.path, s.binary_kind, f" ({s.mime})" if s.mime else "", )lines 79–79return skipped

Requirements

REQ-DDB-PIPE-011