Skip to content

Commit

Permalink
feat: reworked cli
Browse files Browse the repository at this point in the history
  • Loading branch information
4gac committed Sep 23, 2024
1 parent 75c2b3e commit abe3f19
Showing 1 changed file with 85 additions and 37 deletions.
122 changes: 85 additions & 37 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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__":
Expand Down

0 comments on commit abe3f19

Please sign in to comment.