From abe3f1977cad61cb3aa1009e3803d795479c8227 Mon Sep 17 00:00:00 2001 From: 4gac Date: Mon, 23 Sep 2024 07:51:08 +0200 Subject: [PATCH] feat: reworked cli --- src/main.py | 122 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 37 deletions(-) diff --git a/src/main.py b/src/main.py index b0be6b2..46db991 100644 --- a/src/main.py +++ b/src/main.py @@ -1,64 +1,112 @@ -import base64 -import requests import argparse import os +import shutil import sys +from pathlib import Path from process_pdf import alt_text +def get_config(path: str) -> None: + if path is None: + with open( + os.path.join(Path(__file__).parent.absolute(), "../config.json"), + "r", + encoding="utf-8", + ) as f: + print(f.read()) + else: + src = os.path.join(Path(__file__).parent.absolute(), "../config.json") + dst = path + shutil.copyfile(src, dst) + + def main(): parser = argparse.ArgumentParser( - description="Process a PDF or image file with Tesseract OCR" + description="Process a PDF or image file with Tesseract OCR", + ) + parser.add_argument("--name", type=str, default="", help="Pdfix license name") + parser.add_argument("--key", type=str, default="", help="Pdfix license key") + + subparsers = parser.add_subparsers(dest="subparser") + + # config subparser + pars_config = subparsers.add_parser( + "config", + help="Extract config file for integration", + ) + pars_config.add_argument( + "-o", + "--output", + type=str, + help="Output to save the config JSON file. Application output\ + is used if not provided", ) - parser.add_argument("-i", "--input", type=str, help="The input PDF file") - parser.add_argument( + + pars_detect = subparsers.add_parser( + "detect", + help="Run alternate text description", + ) + + pars_detect.add_argument("-i", "--input", type=str, help="The input PDF file") + pars_detect.add_argument( "-o", "--output", type=str, help="The output PDF file", ) - parser.add_argument("--name", type=str, default="", help="Pdfix license name") - parser.add_argument("--key", type=str, default="", help="Pdfix license key") - parser.add_argument("--openai", type=str, required=True, help="OpenAI API key") - parser.add_argument( + pars_detect.add_argument("--openai", type=str, required=True, help="OpenAI API key") + pars_detect.add_argument( "--overwrite", type=bool, required=False, default=False, help="Overwrite alternate text if already present in the tag", ) - args = parser.parse_args() - - if not args.input or not args.output or not args.openai: - parser.error( - "The following arguments are required: -i/--input, -o/--output, --openai" - ) - - input_file = args.input - output_file = args.output - - if not os.path.isfile(input_file): - sys.exit(f"Error: The input file '{input_file}' does not exist.") - return - - if input_file.lower().endswith(".pdf") and output_file.lower().endswith(".pdf"): - try: - alt_text( - input_file, - output_file, - args.name, - args.key, - args.openai, - args.overwrite, + + try: + args = parser.parse_args() + except SystemExit as e: + if e.code == 0: # This happens when --help is used, exit gracefully + sys.exit(0) + print("Failed to parse arguments. Please check the usage and try again.") + sys.exit(1) + + if args.subparser == "config": + get_config(args.output) + sys.exit(0) + + elif args.subparser == "detect": + if not args.input or not args.output or not args.openai: + parser.error( + "The following arguments are required:\ + -i/--input, -o/--output, --openai", ) - # print(desc) - except Exception as e: - sys.exit("Failed to run alternate description: {}".format(e)) - else: - print("Input and output file must be PDF") + input_file = args.input + output_file = args.output + + if not os.path.isfile(input_file): + sys.exit(f"Error: The input file '{input_file}' does not exist.") + return + + if input_file.lower().endswith(".pdf") and output_file.lower().endswith(".pdf"): + try: + alt_text( + input_file, + output_file, + args.name, + args.key, + args.openai, + args.overwrite, + ) + # print(desc) + except Exception as e: + sys.exit("Failed to run alternate description: {}".format(e)) + + else: + print("Input and output file must be PDF") if __name__ == "__main__":