Skip to content

Commit 0edf4a3

Browse files
authored
chg: ✨ add CLI tool for testing and demo (#48)
1 parent c756f86 commit 0edf4a3

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

mindee-cli.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /usr/bin/env sh
2+
set -e
3+
4+
python3 -m mindee "$@"

mindee/__main__.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#! /usr/bin/env python3
2+
3+
import argparse
4+
import os
5+
import sys
6+
from typing import Dict
7+
8+
from mindee import Client
9+
10+
DOCUMENTS: Dict[str, dict] = {
11+
"invoice": {
12+
"help": "Invoice",
13+
"required_keys": ["invoice"],
14+
"parse_func": "parse_invoice",
15+
"doc_type": "invoice",
16+
},
17+
"receipt": {
18+
"help": "Expense Receipt",
19+
"required_keys": ["expense_receipt"],
20+
"parse_func": "parse_receipt",
21+
"doc_type": "receipt",
22+
},
23+
"passport": {
24+
"help": "Passport",
25+
"required_keys": ["passport"],
26+
"parse_func": "parse_passport",
27+
"doc_type": "passport",
28+
},
29+
"financial": {
30+
"help": "Financial Document (receipt or invoice)",
31+
"required_keys": ["invoice", "expense_receipt"],
32+
"parse_func": "parse_financial_document",
33+
"doc_type": "financial_document",
34+
},
35+
}
36+
37+
38+
def _get_env_token(name: str) -> str:
39+
return os.getenv(f"MINDEE_{name.upper()}_TOKEN", "")
40+
41+
42+
def call_endpoint(args):
43+
"""Call the endpoint given passed arguments."""
44+
info = DOCUMENTS[args.product_name]
45+
kwargs = {
46+
"raise_on_error": args.raise_on_error,
47+
}
48+
for key in info["required_keys"]:
49+
kwargs["%s_token" % key] = getattr(args, "%s_api_key" % key)
50+
client = Client(**kwargs)
51+
parse_func = getattr(client, info["parse_func"])
52+
if args.input_type == "stream":
53+
with open(args.path, "rb", buffering=30) as file_handle:
54+
parsed_data = parse_func(file_handle, args.input_type, cut_pdf=args.cut_pdf)
55+
elif args.input_type == "base64":
56+
with open(args.path, "rt") as file_handle:
57+
parsed_data = parse_func(
58+
file_handle.read(),
59+
args.input_type,
60+
filename=args.filename,
61+
cut_pdf=args.cut_pdf,
62+
)
63+
else:
64+
parsed_data = parse_func(args.path, args.input_type, cut_pdf=args.cut_pdf)
65+
print(getattr(parsed_data, info["doc_type"]))
66+
67+
68+
def parse_args():
69+
"""Parse command line arguments"""
70+
parser = argparse.ArgumentParser(description="Mindee API")
71+
parser.add_argument(
72+
"-e",
73+
"--no-raise-errors",
74+
action="store_false",
75+
dest="raise_on_error",
76+
help="don't raise errors",
77+
)
78+
subparsers = parser.add_subparsers(
79+
dest="product_name",
80+
help="sub-command help",
81+
)
82+
for name, info in DOCUMENTS.items():
83+
subp = subparsers.add_parser(name, help=info["help"])
84+
for key_name in info["required_keys"]:
85+
subp.add_argument(
86+
"--%s-key" % key_name,
87+
dest="%s_api_key" % key_name,
88+
help="API key for %s document endpoint" % key_name,
89+
default=_get_env_token(key_name),
90+
)
91+
subp.add_argument(
92+
"-i",
93+
"--input-type",
94+
dest="input_type",
95+
choices=["path", "stream", "base64"],
96+
default="path",
97+
help="Specify how to handle the input,\n"
98+
"path: open the file.\n"
99+
"stream: open the file in a buffer.\n"
100+
"base64: load the contents as a string.",
101+
)
102+
subp.add_argument(
103+
"-f",
104+
"--filename",
105+
dest="filename",
106+
help="filename (required for base64 inputs)",
107+
)
108+
subp.add_argument(
109+
"-C",
110+
"--no-cut-pdf",
111+
dest="cut_pdf",
112+
action="store_false",
113+
help="Don't cut the PDF",
114+
)
115+
subp.add_argument(
116+
"-p",
117+
"--pdf-pages",
118+
dest="pdf_pages",
119+
type=int,
120+
default=3,
121+
help="Number of PDF pages to cut by, default: 3",
122+
)
123+
subp.add_argument(dest="path", help="Full path to the file")
124+
125+
parsed_args = parser.parse_args()
126+
if not parsed_args.product_name:
127+
parser.print_help()
128+
sys.exit(1)
129+
return parsed_args
130+
131+
132+
if __name__ == "__main__":
133+
call_endpoint(parse_args())

0 commit comments

Comments
 (0)