11.01.26
This commit is contained in:
47
app.py
47
app.py
@@ -58,21 +58,30 @@ file_handler.setFormatter(logging.Formatter(
|
|||||||
class AHKParser:
|
class AHKParser:
|
||||||
lab = "AHK"
|
lab = "AHK"
|
||||||
|
|
||||||
def grab(self, text, labels):
|
def extract_table(self, text, headers):
|
||||||
lines = [l.strip() for l in text.splitlines() if l.strip()]
|
lines = [l.strip() for l in text.splitlines() if l.strip()]
|
||||||
idx = [i for i,l in enumerate(lines) if l in labels]
|
out = {}
|
||||||
if not idx:
|
for h in headers:
|
||||||
return {}
|
for i,l in enumerate(lines):
|
||||||
|
if l == h:
|
||||||
|
for j in range(i+1, i+8):
|
||||||
|
if j < len(lines) and lines[j].startswith(":"):
|
||||||
|
out[h] = lines[j][1:].strip()
|
||||||
|
break
|
||||||
|
return out
|
||||||
|
|
||||||
values = []
|
def extract_weights(self, text):
|
||||||
start = idx[-1] + 1
|
lines = [l.strip() for l in text.splitlines() if l.strip()]
|
||||||
for l in lines[start:]:
|
res = {}
|
||||||
if l.startswith(":"):
|
for i,l in enumerate(lines):
|
||||||
values.append(l[1:].strip())
|
if l == "Bales Weighed":
|
||||||
if len(values) == len(labels):
|
headers = ["Bales","Gross","Tare","Net"]
|
||||||
break
|
for h in headers:
|
||||||
|
for j in range(i, i+20):
|
||||||
return dict(zip(labels, values))
|
if j < len(lines) and lines[j].startswith(":"):
|
||||||
|
res[h] = lines[j][1:].replace("kg","").strip()
|
||||||
|
break
|
||||||
|
return res
|
||||||
|
|
||||||
def parse(self, text):
|
def parse(self, text):
|
||||||
r = empty_weight_report("AHK")
|
r = empty_weight_report("AHK")
|
||||||
@@ -85,14 +94,14 @@ class AHKParser:
|
|||||||
r["contract"]["invoice_no"] = safe_search(r"Client Reference:\s*([A-Z0-9\- /]+)", text)
|
r["contract"]["invoice_no"] = safe_search(r"Client Reference:\s*([A-Z0-9\- /]+)", text)
|
||||||
r["contract"]["commodity"] = "Raw Cotton"
|
r["contract"]["commodity"] = "Raw Cotton"
|
||||||
|
|
||||||
# parties
|
# buyer
|
||||||
r["parties"]["buyer"] = safe_search(r"Buyer:\s*([A-Z0-9 ().,-]+)", text)
|
r["parties"]["buyer"] = safe_search(r"Buyer:\s*([A-Z0-9 ().,-]+)", text)
|
||||||
|
|
||||||
# shipment block
|
# shipment tables
|
||||||
ship = self.grab(text, [
|
ship = self.extract_table(text, [
|
||||||
"Total Bales","Vessel","Voy. No.","B/L No.","B/L Date","Destination"
|
"Total Bales","Vessel","Voy. No.","B/L No.","B/L Date","Destination"
|
||||||
])
|
])
|
||||||
ship2 = self.grab(text, [
|
ship2 = self.extract_table(text, [
|
||||||
"Growth","Arrival Date","First date of weighing",
|
"Growth","Arrival Date","First date of weighing",
|
||||||
"Last Date of Weighing","Weighing method","Tare"
|
"Last Date of Weighing","Weighing method","Tare"
|
||||||
])
|
])
|
||||||
@@ -106,8 +115,8 @@ class AHKParser:
|
|||||||
r["contract"]["origin"] = ship2.get("Growth")
|
r["contract"]["origin"] = ship2.get("Growth")
|
||||||
|
|
||||||
# weights
|
# weights
|
||||||
inv = self.grab(text, ["Bales","Gross","Tare","Net"])
|
inv = self.extract_table(text, ["Bales","Gross","Tare","Net"])
|
||||||
land = self.grab(section(text,"Bales Weighed","Outturn"),["Bales","Gross","Tare","Net"])
|
land = self.extract_weights(text)
|
||||||
|
|
||||||
r["weights"]["invoice_net_kg"] = to_float(inv.get("Net"))
|
r["weights"]["invoice_net_kg"] = to_float(inv.get("Net"))
|
||||||
r["weights"]["gross_landed_kg"] = to_float(land.get("Gross"))
|
r["weights"]["gross_landed_kg"] = to_float(land.get("Gross"))
|
||||||
|
|||||||
Reference in New Issue
Block a user