Skip to content

Commit 05bffac

Browse files
committed
✨ add a basic logger
1 parent 04caae3 commit 05bffac

File tree

13 files changed

+210
-206
lines changed

13 files changed

+210
-206
lines changed

mindee/client.py

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import Dict
23

34
from mindee.inputs import (
45
InputDocument,
@@ -7,13 +8,21 @@
78
FileDocument,
89
PathDocument,
910
)
11+
from mindee.logger import logger
1012
from mindee.response import format_response, DocumentResponse
11-
from mindee.http import HTTPException
13+
from mindee.http import (
14+
HTTPException,
15+
Endpoint,
16+
InvoiceEndpoint,
17+
ReceiptEndpoint,
18+
PassportEndpoint,
19+
)
1220
from mindee.document_config import DocumentConfig, DocumentConfigDict
1321
from mindee.documents.receipt import Receipt
1422
from mindee.documents.financial_document import FinancialDocument
1523
from mindee.documents.invoice import Invoice
1624
from mindee.documents.passport import Passport
25+
from mindee.documents.custom_document import CustomDocument
1726

1827

1928
class DocumentClient:
@@ -39,6 +48,8 @@ def parse(
3948
:param username:
4049
:param include_words: Bool, extract all words into http_response
4150
"""
51+
logger.debug("Parsing document as '%s'", document_type)
52+
4253
found = []
4354
for k in self.doc_configs.keys():
4455
if k[1] == document_type:
@@ -107,12 +118,7 @@ def __init__(self, raise_on_error: bool = True):
107118
"""
108119
:param raise_on_error: Raise an Exception on HTTP errors
109120
"""
110-
self._doc_configs = {
111-
("mindee", "receipt"): Receipt.get_document_config(),
112-
("mindee", "invoice"): Invoice.get_document_config(),
113-
("mindee", "financial_doc"): FinancialDocument.get_document_config(),
114-
("mindee", "passport"): Passport.get_document_config(),
115-
}
121+
self._doc_configs: Dict[tuple, DocumentConfig] = {}
116122
self.raise_on_error = raise_on_error
117123

118124
def config_custom_doc(
@@ -138,14 +144,18 @@ def config_custom_doc(
138144
If not set, use the latest version of the model.
139145
"""
140146
self._doc_configs[(account_name, document_type)] = DocumentConfig(
141-
{
142-
"document_type": document_type,
143-
"singular_name": singular_name,
144-
"plural_name": plural_name,
145-
"account_name": account_name,
146-
"api_key": api_key,
147-
"interface_version": version,
148-
}
147+
document_type=document_type,
148+
singular_name=singular_name,
149+
plural_name=plural_name,
150+
constructor=CustomDocument,
151+
endpoints=[
152+
Endpoint(
153+
owner=account_name,
154+
url_name=document_type,
155+
version=version,
156+
api_key=api_key,
157+
),
158+
],
149159
)
150160
return self
151161

@@ -157,8 +167,14 @@ def config_invoice(self, api_key: str = None):
157167
158168
:param api_key: Invoice API key
159169
"""
160-
if api_key:
161-
self._doc_configs[("mindee", "invoice")].endpoints[0].api_key = api_key
170+
config = DocumentConfig(
171+
document_type="invoice",
172+
singular_name="invoice",
173+
plural_name="invoices",
174+
constructor=Invoice,
175+
endpoints=[InvoiceEndpoint(api_key=api_key)],
176+
)
177+
self._doc_configs[("mindee", "invoice")] = config
162178
return self
163179

164180
def config_receipt(self, api_key: str = None):
@@ -169,8 +185,14 @@ def config_receipt(self, api_key: str = None):
169185
170186
:param api_key: Expense Receipt API key
171187
"""
172-
if api_key:
173-
self._doc_configs[("mindee", "receipt")].endpoints[0].api_key = api_key
188+
config = DocumentConfig(
189+
document_type="receipt",
190+
singular_name="receipt",
191+
plural_name="receipts",
192+
constructor=Receipt,
193+
endpoints=[ReceiptEndpoint(api_key=api_key)],
194+
)
195+
self._doc_configs[("mindee", "receipt")] = config
174196
return self
175197

176198
def config_financial_doc(
@@ -184,14 +206,17 @@ def config_financial_doc(
184206
:param receipt_api_key: Expense Receipt API key
185207
:param invoice_api_key: Invoice API key
186208
"""
187-
if invoice_api_key:
188-
self._doc_configs[("mindee", "financial_doc")].endpoints[
189-
0
190-
].api_key = invoice_api_key
191-
if receipt_api_key:
192-
self._doc_configs[("mindee", "financial_doc")].endpoints[
193-
1
194-
].api_key = receipt_api_key
209+
config = DocumentConfig(
210+
document_type="financial_doc",
211+
singular_name="financial_doc",
212+
plural_name="financial_docs",
213+
constructor=FinancialDocument,
214+
endpoints=[
215+
InvoiceEndpoint(api_key=invoice_api_key),
216+
ReceiptEndpoint(api_key=receipt_api_key),
217+
],
218+
)
219+
self._doc_configs[("mindee", "financial_doc")] = config
195220
return self
196221

197222
def config_passport(self, api_key: str = None):
@@ -202,8 +227,14 @@ def config_passport(self, api_key: str = None):
202227
203228
:param api_key: Passport API key
204229
"""
205-
if api_key:
206-
self._doc_configs[("mindee", "passport")].endpoints[0].api_key = api_key
230+
config = DocumentConfig(
231+
document_type="passport",
232+
singular_name="passport",
233+
plural_name="passports",
234+
constructor=Passport,
235+
endpoints=[PassportEndpoint(api_key=api_key)],
236+
)
237+
self._doc_configs[("mindee", "passport")] = config
207238
return self
208239

209240
def doc_from_path(

mindee/document_config.py

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,29 @@
1-
from typing import Dict, List, Any, Type
1+
from typing import Dict, List
22

3-
from mindee.http import Endpoint, API_TYPE_OFF_THE_SHELF, API_TYPE_CUSTOM
4-
from mindee.documents.base import Document
5-
from mindee.documents.custom_document import CustomDocument
3+
from mindee.http import Endpoint
4+
from mindee.documents.base import TypeDocument
65

76

87
class DocumentConfig:
98
document_type: str
10-
api_type: str
119
endpoints: List[Endpoint]
1210
singular_name: str
1311
plural_name: str
14-
15-
constructor: Type[Document]
16-
17-
def __init__(self, config: Dict[str, Any], api_type: str = API_TYPE_CUSTOM):
18-
"""
19-
:param config: Object containing config
20-
:param api_type: API_TYPE_OFF_THE_SHELF or API_TYPE_CUSTOM
21-
"""
22-
self.api_type = api_type
23-
self._load_config(config)
24-
25-
def _load_config(self, config):
26-
# Check for document_type, singular_name and plural_name in config
27-
for mandatory_field in ("document_type", "singular_name", "plural_name"):
28-
if mandatory_field not in config.keys():
29-
raise AssertionError(
30-
"%s key is required in custom_document configuration"
31-
% mandatory_field
32-
)
33-
self.document_type = config["document_type"]
34-
self.singular_name = config["singular_name"]
35-
self.plural_name = config["plural_name"]
36-
37-
if self.api_type == API_TYPE_CUSTOM:
38-
self.constructor = CustomDocument
39-
endpoint = Endpoint(
40-
owner=config["account_name"],
41-
url_name=config["document_type"],
42-
version=config["interface_version"],
43-
)
44-
if config["api_key"]:
45-
endpoint.api_key = config["api_key"]
46-
else:
47-
endpoint.set_api_key_from_env()
48-
self.endpoints = [endpoint]
49-
50-
elif self.api_type == API_TYPE_OFF_THE_SHELF:
51-
self.constructor = config["constructor"]
52-
self.endpoints = config["endpoints"]
53-
for endpoint in self.endpoints:
54-
endpoint.set_api_key_from_env()
55-
56-
else:
57-
raise RuntimeError("Unknown API type")
12+
constructor: TypeDocument
13+
14+
def __init__(
15+
self,
16+
document_type: str,
17+
singular_name: str,
18+
plural_name: str,
19+
constructor: TypeDocument,
20+
endpoints: List[Endpoint],
21+
):
22+
self.document_type = document_type
23+
self.singular_name = singular_name
24+
self.plural_name = plural_name
25+
self.constructor = constructor
26+
self.endpoints = endpoints
5827

5928

6029
DocumentConfigDict = Dict[tuple, DocumentConfig]

mindee/documents/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List
1+
from typing import List, Type
22

33
# from mindee.inputs import InputDocument
44
from mindee.http import Endpoint
@@ -36,3 +36,6 @@ def _reconstruct(self, *args):
3636
def all_checks(self):
3737
"""Return all checks"""
3838
return all(self.checklist)
39+
40+
41+
TypeDocument = Type[Document]

mindee/documents/financial_document.py

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
from mindee.fields.orientation import Orientation
77
from mindee.fields.tax import Tax
88
from mindee.fields import Field
9-
from mindee.http import make_api_request, API_TYPE_OFF_THE_SHELF, Endpoint
9+
from mindee.http import make_api_request, Endpoint
1010
from mindee.documents.base import Document
1111
from mindee.documents.invoice import Invoice
1212
from mindee.documents.receipt import Receipt
13-
from mindee.document_config import DocumentConfig
1413

1514

1615
class FinancialDocument(Document):
@@ -128,33 +127,6 @@ def __init__(
128127
# Reconstruct extra fields
129128
self._reconstruct()
130129

131-
@staticmethod
132-
def get_document_config():
133-
""":return: the configuration for financial document"""
134-
return DocumentConfig(
135-
{
136-
"constructor": FinancialDocument,
137-
"endpoints": [
138-
Endpoint(
139-
owner="mindee",
140-
url_name="invoices",
141-
version="2",
142-
key_name="invoice",
143-
),
144-
Endpoint(
145-
owner="mindee",
146-
url_name="expense_receipts",
147-
version="3",
148-
key_name="receipt",
149-
),
150-
],
151-
"document_type": "financial_doc",
152-
"singular_name": "financial_doc",
153-
"plural_name": "financial_docs",
154-
},
155-
api_type=API_TYPE_OFF_THE_SHELF,
156-
)
157-
158130
def build_from_api_prediction(self, api_prediction, input_file, page_n=0):
159131
"""
160132
:param api_prediction: Raw prediction from HTTP response

mindee/documents/invoice.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
from mindee.fields.orientation import Orientation
99
from mindee.fields.payment_details import PaymentDetails
1010
from mindee.fields.tax import Tax
11-
from mindee.http import make_api_request, API_TYPE_OFF_THE_SHELF, Endpoint
12-
from mindee.document_config import DocumentConfig
11+
from mindee.http import make_api_request, Endpoint
1312

1413

1514
class Invoice(Document):
@@ -120,27 +119,6 @@ def __init__(
120119
# Reconstruct extra fields
121120
self._reconstruct()
122121

123-
@staticmethod
124-
def get_document_config() -> DocumentConfig:
125-
""":return: the configuration for invoice"""
126-
return DocumentConfig(
127-
{
128-
"constructor": Invoice,
129-
"endpoints": [
130-
Endpoint(
131-
owner="mindee",
132-
url_name="invoices",
133-
version="2",
134-
key_name="invoice",
135-
)
136-
],
137-
"document_type": "invoice",
138-
"singular_name": "invoice",
139-
"plural_name": "invoices",
140-
},
141-
api_type=API_TYPE_OFF_THE_SHELF,
142-
)
143-
144122
def build_from_api_prediction(self, api_prediction: dict, page_n=0):
145123
"""
146124
:param api_prediction: Raw prediction from HTTP response

mindee/documents/passport.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
from mindee.documents.base import Document
55
from mindee.fields import Field
66
from mindee.fields.date import Date
7-
from mindee.http import make_api_request, API_TYPE_OFF_THE_SHELF, Endpoint
8-
from mindee.document_config import DocumentConfig
7+
from mindee.http import make_api_request, Endpoint
98

109

1110
class Passport(Document):
@@ -108,22 +107,6 @@ def __init__(
108107
# Reconstruct extra fields
109108
self._reconstruct()
110109

111-
@staticmethod
112-
def get_document_config() -> DocumentConfig:
113-
""":return: the configuration for passport"""
114-
return DocumentConfig(
115-
{
116-
"constructor": Passport,
117-
"endpoints": [
118-
Endpoint(owner="mindee", url_name="passport", version="1")
119-
],
120-
"document_type": "passport",
121-
"singular_name": "passport",
122-
"plural_name": "passports",
123-
},
124-
api_type=API_TYPE_OFF_THE_SHELF,
125-
)
126-
127110
def build_from_api_prediction(self, api_prediction, page_n=0):
128111
"""
129112
:param api_prediction: Raw prediction from HTTP response
@@ -159,17 +142,17 @@ def __str__(self) -> str:
159142
return (
160143
"-----Passport data-----\n"
161144
f"Filename: {self.filename}\n"
162-
f"Full name: {self.full_name.value}\n"
145+
f"Full name: {self.full_name}\n"
163146
f"Given names: {given_names}\n"
164-
f"Surname: {self.surname.value}\n"
165-
f"Country: {self.country.value}\n"
166-
f"ID Number: {self.id_number.value}\n"
167-
f"Issuance date: {self.issuance_date.value}\n"
168-
f"Birth date: {self.birth_date.value}\n"
169-
f"Expiry date: {self.expiry_date.value}\n"
170-
f"MRZ 1: {self.mrz1.value}\n"
171-
f"MRZ 2: {self.mrz2.value}\n"
172-
f"MRZ: {self.mrz.value}\n"
147+
f"Surname: {self.surname}\n"
148+
f"Country: {self.country}\n"
149+
f"ID Number: {self.id_number}\n"
150+
f"Issuance date: {self.issuance_date}\n"
151+
f"Birth date: {self.birth_date}\n"
152+
f"Expiry date: {self.expiry_date}\n"
153+
f"MRZ 1: {self.mrz1}\n"
154+
f"MRZ 2: {self.mrz2}\n"
155+
f"MRZ: {self.mrz}\n"
173156
"----------------------"
174157
)
175158

0 commit comments

Comments
 (0)