cloudairambo / utils /format_registry.py
rihancloud's picture
Deploy clean FastAPI backend without node_modules
99fdb5f
Raw History Blame Contribute Delete
5.32 kB
import subprocess
import shutil
from functools import lru_cache
# -------------------------------------------------
# ImageMagick
# -------------------------------------------------
@lru_cache()
def imagemagick_formats():
magick = shutil.which("magick") or shutil.which("convert")
if not magick:
return set()
out = subprocess.check_output(
[magick, "-list", "format"],
stderr=subprocess.DEVNULL
).decode(errors="ignore")
fmts = set()
for line in out.splitlines():
parts = line.split()
if parts and parts[0].isalpha():
fmts.add(parts[0].lower())
return fmts
# -------------------------------------------------
# FFmpeg
# -------------------------------------------------
@lru_cache()
def ffmpeg_formats():
if not shutil.which("ffmpeg"):
return set()
out = subprocess.check_output(
["ffmpeg", "-formats"],
stderr=subprocess.STDOUT
).decode(errors="ignore")
fmts = set()
for line in out.splitlines():
if line.startswith(" D") or line.startswith(" E"):
fmts.update(line.split()[1].split(","))
return fmts
# -------------------------------------------------
# Pandoc
# -------------------------------------------------
@lru_cache()
def pandoc_formats():
if not shutil.which("pandoc"):
return set()
out = subprocess.check_output(
["pandoc", "--list-output-formats"]
).decode(errors="ignore")
return set(out.split())
# -------------------------------------------------
# LibreOffice
# -------------------------------------------------
@lru_cache()
def libreoffice_formats():
if not (shutil.which("soffice") or shutil.which("libreoffice")):
return set()
return {
"doc", "docx", "ppt", "pptx",
"xls", "xlsx", "odt", "ods", "odp",
"rtf", "pdf", "txt"
}
# -------------------------------------------------
# Calibre (Ebooks)
# -------------------------------------------------
@lru_cache()
def ebook_formats():
if not shutil.which("ebook-convert"):
return set()
return {"epub", "mobi", "azw3", "fb2", "lit", "pdf"}
# -------------------------------------------------
# Archives
# -------------------------------------------------
@lru_cache()
def archive_formats():
return {"zip", "7z", "rar", "tar", "gz", "bz2", "xz"}
# -------------------------------------------------
# GDAL / OGR (GIS)
# -------------------------------------------------
@lru_cache()
def gdal_formats():
if not shutil.which("ogr2ogr"):
return set()
out = subprocess.check_output(
["ogrinfo", "--formats"],
stderr=subprocess.DEVNULL
).decode(errors="ignore")
fmts = set()
for line in out.splitlines():
if "-" in line:
name = line.split("-")[0].strip().lower()
fmts.add(name)
return fmts
# -------------------------------------------------
# OpenBabel (Scientific / Chemistry)
# -------------------------------------------------
@lru_cache()
def openbabel_formats():
if not shutil.which("obabel"):
return set()
out = subprocess.check_output(
["obabel", "-L", "formats"],
stderr=subprocess.DEVNULL
).decode(errors="ignore")
fmts = set()
for line in out.splitlines():
if "." in line:
ext = line.split(".")[1].split()[0].lower()
fmts.add(ext)
return fmts
# -------------------------------------------------
# Poppler (Advanced PDF targets)
# -------------------------------------------------
@lru_cache()
def poppler_formats():
if not shutil.which("pdftocairo"):
return set()
return {"svg", "html", "ps", "ppm"}
# -------------------------------------------------
# Whisper (AI transcription targets)
# -------------------------------------------------
@lru_cache()
def whisper_formats():
if not shutil.which("whisper"):
return set()
return {"txt", "srt", "vtt"}
# -------------------------------------------------
# MASTER REGISTRY
# -------------------------------------------------
def all_formats():
return {
"images": imagemagick_formats(),
"audio_video": ffmpeg_formats(),
"documents": pandoc_formats(),
"office": libreoffice_formats(),
"ebooks": ebook_formats(),
"archives": archive_formats(),
"gis": gdal_formats(),
"scientific": openbabel_formats(),
"advanced_pdf": poppler_formats(),
"speech_to_text": whisper_formats(),
}
@lru_cache()
def djvu_formats():
if not shutil.which("ddjvu"):
return set()
return {"djvu"}
@lru_cache()
def graphicsmagick_formats():
if not shutil.which("gm"):
return set()
return {
"bmp", "gif", "jpg", "jpeg", "png", "tiff",
"webp", "pnm", "ppm", "ico", "psd"
}
@lru_cache()
def mupdf_formats():
if not shutil.which("mutool"):
return set()
return {"xps", "cbz", "svg"}
# -------------------------------------------------
# CLI TEST
# -------------------------------------------------
if __name__ == "__main__":
f = all_formats()
for k, v in f.items():
print(f"{k}: {len(v)}")
total = set.union(*[set(v) for v in f.values() if v])
print("TOTAL DISTINCT FORMATS:", len(total))