From a0cb5eb9eac0ddcfa1a3c05acf850b9b270f193a Mon Sep 17 00:00:00 2001 From: Renato Rosa Date: Sat, 26 Sep 2026 14:10:23 -0300 Subject: [PATCH] Adicionar src-py/renatoxsr/utils/pandoc.py --- src-py/renatoxsr/utils/pandoc.py | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src-py/renatoxsr/utils/pandoc.py diff --git a/src-py/renatoxsr/utils/pandoc.py b/src-py/renatoxsr/utils/pandoc.py new file mode 100644 index 0000000..435c139 --- /dev/null +++ b/src-py/renatoxsr/utils/pandoc.py @@ -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 +# 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) \ No newline at end of file