Adicionar src-py/renatoxsr/utils/pandoc.py

This commit is contained in:
2026-09-26 14:10:23 -03:00
parent c14c62b297
commit a0cb5eb9ea
+42
View File
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
# vim: set ft=python ts=4 sw=4 et encoding=utf-8 :
# SPDX-FileCopyrightText: 2025-present RenatoXSR <renatoxsr@gmail.com>
# SPDX-License-Identifier: BSD-3-Clause-Clear
import errno, os, shutil, sys
from pathlib import Path
from renatoxsr.logger import set_logger
logger = set_logger(__name__)
if "--debug" in sys.argv:
logger.setLevel("DEBUG")
def pandoc(
input_file: str | Path,
bin: str | Path = "pandoc",
output: str | Path = "-",
overwrite = False,
) -> str:
logger.info("%s: from '%s' to '%s'", bin, input_file, output)
if not Path(bin).is_file():
which_bin = shutil.which(bin)
if not Path(bin).is_file():
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), bin)
fpath = Path(input_file)
if not fpath.is_file():
raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), input_file)
outf = Path(output)
if all(output != "-", outf.is_file(), not overwrite):
raise FileExistsError(errno.EEXIST, os.strerror(errno.EEXIST), outf)
cmd = [bin, "--output", "-" if output=="-" else outf.resolve(), fpath]
logger.debug("CMD %s", str(cmd))
proc = subprocess.run(cmd, text=True)
if proc.stderr:
logger.error(proc.stderr)
if proc.stdout and output != "-":
logger.info(proc.stdout)
else:
return(proc.stdout)