Skip to content

Commit

Permalink
feat: add config and tag subparsers
Browse files Browse the repository at this point in the history
  • Loading branch information
4gac committed Dec 20, 2024
1 parent 2e59d79 commit c01b560
Showing 1 changed file with 72 additions and 21 deletions.
93 changes: 72 additions & 21 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,89 @@
from autotag import autotag


def main():
parser = argparse.ArgumentParser(description="Process a PDF file using Paddle layout recognition")
parser.add_argument("-i", "--input", type=str, help="The input PDF file")
parser.add_argument(
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() -> None:
parser = argparse.ArgumentParser(
description="Process a PDF file using Paddle layout recognition",
)

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",
)

pars_tag = subparsers.add_parser(
"tag",
help="Run autotag",
)

pars_tag.add_argument("-i", "--input", type=str, help="The input PDF file")
pars_tag.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")
args = parser.parse_args()
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 not args.input or not args.output:
parser.error("The following arguments are required: -i/--input, -o/--output")
if args.subparser == "config":
get_config(args.output)
sys.exit(0)

input_file = args.input
output_file = args.output
elif args.subparser == "tag":
if not args.input or not args.output:
parser.error(
"The following arguments are required: -i/--input, -o/--output",
)

if not os.path.isfile(input_file):
sys.exit(f"Error: The input file '{input_file}' does not exist")
return
input_file = args.input
output_file = args.output

if input_file.lower().endswith(".pdf") and output_file.lower().endswith(".pdf"):
try:
autotag(input_file, output_file, args.name, args.key)
except Exception as e:
sys.exit("Failed to run Paddle {}".format(e))
if not os.path.isfile(input_file):
sys.exit(f"Error: The input file '{input_file}' does not exist")
return

else:
print("Input and output file must be PDF")
if input_file.lower().endswith(".pdf") and output_file.lower().endswith(".pdf"):
try:
autotag(input_file, output_file, args.name, args.key)
except Exception as e:
sys.exit("Failed to run Paddle {}".format(e))

else:
print("Input and output file must be PDF")


if __name__ == "__main__":
Expand Down

0 comments on commit c01b560

Please sign in to comment.