def test_make_datetime_json_serializable(self): self.assertEqual( make_datetime_json_serializable("2020-01-05"), "2020-01-05T00:00:00Z" ) self.assertIs(make_datetime_json_serializable("2020-31-05"), None) self.assertEqual( make_datetime_json_serializable("2020-01-05", "12:59"), "2020-01-05T12:59:00Z", )
def parse_coe(coe): date = coe.get("DA") time = coe.get("TI") effort_zone_entry_datetime_utc = make_datetime_json_serializable( date, time) children = tagged_children(coe) value = { "effortZoneEntryDatetimeUtc": effort_zone_entry_datetime_utc, "targetSpeciesOnEntry": coe.get("TS"), } if "RAS" in children: assert len(children["RAS"]) == 1 ras = children["RAS"][0] ras_data = parse_ras(ras) value["faoZoneEntered"] = ras_data["faoZone"] value["economicZoneEntered"] = ras_data["economicZone"] value["statisticalRectangleEntered"] = ras_data["statisticalRectangle"] value["effortZoneEntered"] = ras_data["effortZone"] if "POS" in children: assert len(children["POS"]) == 1 pos = children["POS"][0] lat, lon = parse_pos(pos) value["latitudeEntered"] = try_float(lat) value["longitudeEntered"] = try_float(lon) data = {"log_type": "COE", "value": value} return data
def parse_far(far): date = far.get("DA") time = far.get("TI") far_datetime_utc = make_datetime_json_serializable(date, time) value = {"farDatetimeUtc": far_datetime_utc} children = tagged_children(far) if "GEA" in children: assert len(children["GEA"]) == 1 gear_el = children["GEA"][0] gear = parse_gea(gear_el) value = {**value, **gear} if "SPE" in children: catches = [parse_spe(spe) for spe in children["SPE"]] value["catches"] = catches if "POS" in children: assert len(children["POS"]) == 1 pos = children["POS"][0] lat, lon = parse_pos(pos) value["latitude"] = try_float(lat) value["longitude"] = try_float(lon) data = {"log_type": "FAR", "value": value} return data
def parse_eof(eof): date = eof.get("DA") time = eof.get("TI") end_of_fishing_datetime_utc = make_datetime_json_serializable(date, time) value = {"endOfFishingDatetimeUtc": end_of_fishing_datetime_utc} data = {"log_type": "EOF", "value": value} return data
def parse_dep(dep): date = dep.get("DA") time = dep.get("TI") # cannot use DateTime because the data needs to be json serializable departure_datetime_utc = make_datetime_json_serializable(date, time) value = { "departureDatetimeUtc": departure_datetime_utc, "departurePort": dep.get("PO"), "anticipatedActivity": dep.get("AA"), } children = tagged_children(dep) if "GEA" in children: gear = [parse_gea(gea) for gea in children["GEA"]] value["gearOnboard"] = gear if "SPE" in children: species_onboard = [parse_spe(spe) for spe in children["SPE"]] value["speciesOnboard"] = species_onboard data = {"log_type": "DEP", "value": value} return data
def parse_pno(pno): date = pno.get("PD") time = pno.get("PT") predicted_arrival_datetime_utc = make_datetime_json_serializable( date, time) start_date = pno.get("DS") trip_start_date = make_datetime_json_serializable(start_date, None) children = tagged_children(pno) value = { "predictedArrivalDatetimeUtc": predicted_arrival_datetime_utc, "port": pno.get("PO"), "purpose": pno.get("PC"), "tripStartDate": trip_start_date, } if "RAS" in children: assert len(children["RAS"]) == 1 ras = children["RAS"][0] ras_data = parse_ras(ras) value = {**value, **ras_data} if "SPE" in children: catches = [parse_spe(spe) for spe in children["SPE"]] value["catchOnboard"] = catches if "POS" in children: assert len(children["POS"]) == 1 pos = children["POS"][0] lat, lon = parse_pos(pos) value["latitude"] = try_float(lat) value["longitude"] = try_float(lon) data = {"log_type": "PNO", "value": value} return data
def parse_dis(dis): date = dis.get("DA") time = dis.get("TI") discard_datetime_utc = make_datetime_json_serializable(date, time) value = {"discardDatetimeUtc": discard_datetime_utc} children = tagged_children(dis) if "SPE" in children: catches = [parse_spe(spe) for spe in children["SPE"]] value["catches"] = catches data = {"log_type": "DIS", "value": value} return data
def parse_rtp(rtp): date = rtp.get("DA") time = rtp.get("TI") return_datetime_utc = make_datetime_json_serializable(date, time) value = { "returnDatetimeUtc": return_datetime_utc, "port": rtp.get("PO"), "reasonOfReturn": rtp.get("RE"), } children = tagged_children(rtp) if "GEA" in children: gear = [parse_gea(gea) for gea in children["GEA"]] value["gearOnboard"] = gear data = {"log_type": "RTP", "value": value} return data
def parse_lan(lan): date = lan.get("DA") time = lan.get("TI") landing_datetime_utc = make_datetime_json_serializable(date, time) value = { "landingDatetimeUtc": landing_datetime_utc, "port": lan.get("PO"), "sender": lan.get("TS"), } children = tagged_children(lan) if "SPE" in children: catches = [parse_spe(spe) for spe in children["SPE"]] value["catchLanded"] = catches data = {"log_type": "LAN", "value": value} return data