binary_extensions.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Binary file extensions to skip for text-based operations (ported from
  2. free-code src/constants/files.ts)."""
  3. # Images, video, audio, archives, executables, documents (.pdf deliberately
  4. # excluded — text-based, agents may want to inspect), fonts, bytecode/VM,
  5. # databases, design/3D, Flash, lock/profiling data.
  6. BINARY_EXTENSIONS = frozenset({
  7. ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".ico", ".webp", ".tiff", ".tif",
  8. ".mp4", ".mov", ".avi", ".mkv", ".webm", ".wmv", ".flv", ".m4v", ".mpeg", ".mpg",
  9. ".mp3", ".wav", ".ogg", ".flac", ".aac", ".m4a", ".wma", ".aiff", ".opus",
  10. ".zip", ".tar", ".gz", ".bz2", ".7z", ".rar", ".xz", ".z", ".tgz", ".iso",
  11. ".exe", ".dll", ".so", ".dylib", ".bin", ".o", ".a", ".obj", ".lib", ".app", ".msi", ".deb", ".rpm",
  12. ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".odt", ".ods", ".odp",
  13. ".ttf", ".otf", ".woff", ".woff2", ".eot",
  14. ".pyc", ".pyo", ".class", ".jar", ".war", ".ear", ".node", ".wasm", ".rlib",
  15. ".sqlite", ".sqlite3", ".db", ".mdb", ".idx",
  16. ".psd", ".ai", ".eps", ".sketch", ".fig", ".xd", ".blend", ".3ds", ".max",
  17. ".swf", ".fla", ".lockb", ".dat", ".data",
  18. })
  19. # Container documents (OOXML/ODF/EPUB zips, OLE, RTF) a plain-text write can
  20. # NEVER produce validly: read_file auto-extracts them, so writing the text back
  21. # via write_file/patch silently destroys the document. PDF is deliberately
  22. # absent — raw PDF syntax is text-authorable, so only overwrites are dangerous
  23. # (the write guard handles that via is_pdf_path).
  24. OPAQUE_DOCUMENT_EXTENSIONS = frozenset({
  25. ".doc", ".docx", ".docm", ".xls", ".xlsx", ".xlsm", ".xlsb",
  26. ".ppt", ".pps", ".pot", ".pptx", ".pptm", ".ppsx", ".ppsm",
  27. ".odt", ".ods", ".odp", ".rtf", ".epub",
  28. })
  29. # SQLite journal sidecars (``x.db-wal``, ``x.sqlite3-shm``, ``x.db-journal``)
  30. # hang their marker off the database's own extension, so the final ``.suffix``
  31. # is ".db-wal" — never in any extension set — and both the read guard and the
  32. # write guard would treat the raw page bytes as text.
  33. _SQLITE_SIDECAR_MARKERS = ("-wal", "-shm", "-journal")
  34. # Only these suffixes take a sidecar marker; ``report.docx-wal`` is not a
  35. # document and must not be treated as one.
  36. _SQLITE_EXTENSIONS = frozenset({".db", ".sqlite", ".sqlite3"})
  37. assert _SQLITE_EXTENSIONS <= BINARY_EXTENSIONS
  38. def _lower_suffix(path: str) -> str:
  39. """Lower-cased final ``.suffix`` of ``path`` ("" when there is no dot)."""
  40. dot = path.rfind(".")
  41. return "" if dot == -1 else path[dot:].lower()
  42. def _strip_sidecar_marker(suffix: str) -> str | None:
  43. """Return the database suffix a lower-cased sidecar suffix hangs off
  44. (``.db-wal`` -> ``.db``), or None when ``suffix`` is not a SQLite sidecar."""
  45. for marker in _SQLITE_SIDECAR_MARKERS:
  46. if suffix.endswith(marker):
  47. base = suffix[: -len(marker)]
  48. if base in _SQLITE_EXTENSIONS:
  49. return base
  50. return None
  51. def _has_extension_in(path: str, extensions: frozenset) -> bool:
  52. """Case-insensitive check on the final ``.suffix``; pure string, no I/O.
  53. A SQLite sidecar counts as its database's extension (only SQLite suffixes
  54. are stripped, so ``x.docx-wal`` stays unrecognised)."""
  55. suffix = _lower_suffix(path)
  56. return (_strip_sidecar_marker(suffix) or suffix) in extensions
  57. def is_sqlite_sidecar(path: str) -> bool:
  58. """True for ``x.db-wal`` / ``x.sqlite3-shm`` / ``x.db-journal`` paths.
  59. Pure string, no I/O — a sidecar path is never a legitimate text target
  60. even when no sidecar exists on disk (a checkpointed db has none)."""
  61. return _strip_sidecar_marker(_lower_suffix(path)) is not None
  62. def has_binary_extension(path: str) -> bool:
  63. return _has_extension_in(path, BINARY_EXTENSIONS)
  64. def has_opaque_document_extension(path: str) -> bool:
  65. return _has_extension_in(path, OPAQUE_DOCUMENT_EXTENSIONS)
  66. def is_pdf_path(path: str) -> bool:
  67. return path.lower().endswith(".pdf")