File size: 1,444 Bytes
fea1bd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
"""
Path and filesystem related utilities.
"""
from pathlib import Path
from typing import Any, Union

def to_path(obj: Any) -> Path:
    """Converts a string or Path object to a resolved Path."""
    if isinstance(obj, Path):
        return obj.resolve(strict=False)
    if isinstance(obj, str):
        return Path(obj).resolve(strict=False)
    raise TypeError(f"Object of type {type(obj).__name__} cannot be converted to Path")

def is_binary_file(path: Union[str, Path], chunk_size: int = 1024) -> bool:
    """
    Heuristically determines if a file is binary by checking for null bytes.
    Returns True on read errors (assume not plain text).
    """
    try:
        file_path = to_path(path)
        if not file_path.is_file():
            return True
        with file_path.open("rb") as f:
            chunk = f.read(chunk_size)
        return b"\x00" in chunk
    except Exception:
        return True

def safe_glob(base: Union[str, Path], pattern: str) -> list[Path]:
    """
    Performs a glob search relative to a base path, handling exceptions.
    Returns a list of resolved Path objects.
    """
    try:
        base_path = to_path(base)
        if not base_path.is_dir():
            return []
        results = base_path.rglob(pattern) if "**" in pattern else base_path.glob(pattern)
        return [p.resolve(strict=False) for p in results if p.exists()]
    except Exception:
        return []