diff --git a/app.py b/app.py index 0ac331e..ca10339 100644 --- a/app.py +++ b/app.py @@ -58,47 +58,66 @@ file_handler.setFormatter(logging.Formatter( class AHKParser: lab = "AHK" - def g(self, pat, txt): - m = re.search(pat, txt, re.I | re.S) - return m.group(1).strip() if m else None + def grab(self, text, labels): + lines = [l.strip() for l in text.splitlines() if l.strip()] + idx = [i for i,l in enumerate(lines) if l in labels] + if not idx: + return {} + + values = [] + start = idx[-1] + 1 + for l in lines[start:]: + if l.startswith(":"): + values.append(l[1:].strip()) + if len(values) == len(labels): + break + + return dict(zip(labels, values)) def parse(self, text): r = empty_weight_report("AHK") - # ---------- report ---------- - r["report"]["reference"] = self.g(r"(AHK\s*/[A-Z0-9/]+)", text) - r["report"]["date"] = self.g(r"Produced On\s*([0-9]{1,2}\s+[A-Za-z]+\s+20\d{2})", text) + # report + r["report"]["reference"] = safe_search(r"(AHK\s*/[A-Z0-9/]+)", text) + r["report"]["date"] = safe_search(r"Produced On\s*([0-9A-Za-z ]+)", text) - # ---------- contract ---------- - r["contract"]["invoice_no"] = self.g(r"Client Reference:\s*([A-Z0-9\- /]+)", text) + # contract + r["contract"]["invoice_no"] = safe_search(r"Client Reference:\s*([A-Z0-9\- /]+)", text) r["contract"]["commodity"] = "Raw Cotton" - r["contract"]["origin"] = self.g(r"Growth\s*:\s*([A-Z ].+?)(?:Arrival Date|First)", text) - # ---------- parties ---------- - r["parties"]["buyer"] = self.g(r"Buyer\s*:\s*([A-Z0-9 ().,-]+)", text) + # parties + r["parties"]["buyer"] = safe_search(r"Buyer:\s*([A-Z0-9 ().,-]+)", text) - # ---------- shipment ---------- - r["shipment"]["bales"] = to_float(self.g(r"Total Bales\s*:\s*(\d+)", text)) - r["shipment"]["vessel"] = self.g(r"Vessel\s*:\s*([A-Z0-9 ]+)", text) - r["shipment"]["bl_no"] = self.g(r"B/L No\.\s*:\s*([A-Z0-9]+)", text) - r["shipment"]["port_destination"] = self.g(r"Destination\s*:\s*([A-Z ,]+)", text) - r["shipment"]["arrival_date"] = self.g(r"Arrival Date\s*:\s*([0-9A-Za-z-]+)", text) - r["shipment"]["weighing_method"] = self.g(r"Weighing method\s*:\s*([A-Za-z ]+)", text) + # shipment block + ship = self.grab(text, [ + "Total Bales","Vessel","Voy. No.","B/L No.","B/L Date","Destination" + ]) + ship2 = self.grab(text, [ + "Growth","Arrival Date","First date of weighing", + "Last Date of Weighing","Weighing method","Tare" + ]) - # ---------- invoice weights ---------- - inv = self.g(r"INVOICE WEIGHTS.*?Net\s*:\s*([\d.]+)\s*kg", text) - r["weights"]["invoice_net_kg"] = to_float(inv) + r["shipment"]["bales"] = to_float(ship.get("Total Bales")) + r["shipment"]["vessel"] = ship.get("Vessel") + r["shipment"]["bl_no"] = ship.get("B/L No.") + r["shipment"]["port_destination"] = ship.get("Destination") + r["shipment"]["arrival_date"] = ship2.get("Arrival Date") + r["shipment"]["weighing_method"] = ship2.get("Weighing method") + r["contract"]["origin"] = ship2.get("Growth") - # ---------- landed weights ---------- - land = self.g(r"Bales Weighed.*?Net\s*:\s*([\d.]+)\s*kg", text) - r["weights"]["net_landed_kg"] = to_float(land) + # weights + inv = self.grab(text, ["Bales","Gross","Tare","Net"]) + land = self.grab(section(text,"Bales Weighed","Outturn"),["Bales","Gross","Tare","Net"]) - r["weights"]["gross_landed_kg"] = to_float(self.g(r"Bales Weighed.*?Gross\s*:\s*([\d.]+)\s*kg", text)) - r["weights"]["tare_kg"] = to_float(self.g(r"Bales Weighed.*?Tare\s*:\s*([\d.]+)\s*kg", text)) + r["weights"]["invoice_net_kg"] = to_float(inv.get("Net")) + r["weights"]["gross_landed_kg"] = to_float(land.get("Gross")) + r["weights"]["tare_kg"] = to_float(land.get("Tare")) + r["weights"]["net_landed_kg"] = to_float(land.get("Net")) - # ---------- loss ---------- - r["weights"]["gain_loss_kg"] = to_float(self.g(r"LOSS.*?(-?\d+\.?\d*)\s*kg", text)) - r["weights"]["gain_loss_percent"] = to_float(self.g(r"Percentage\s*:\s*(-?\d+\.?\d*)", text)) + # loss + loss = section(text,"LOSS","Invoice average") + r["weights"]["gain_loss_kg"] = to_float(safe_search(r"(-?\d+\.?\d*)\s*kg", loss)) + r["weights"]["gain_loss_percent"] = to_float(safe_search(r"Percentage\s*:\s*(-?\d+\.?\d*)", loss)) return r