From 692efcc9f86ed6a674f191f7b1f92ebc351e1f52 Mon Sep 17 00:00:00 2001 From: Jacob Shilitz Date: Wed, 11 Sep 2024 19:10:44 -0400 Subject: [PATCH] Refactor service code generation and update unit mappings Refactored the service code generation logic for eShipper carrier. Updated unit mappings to handle duplicates by appending identifying information, ensuring unique service codes without manual intervention. --- .../connectors/eshipper/carrier_generate.py | 108 ++++++++++++------ .../karrio/providers/eshipper/units.py | 74 ++++++------ 2 files changed, 112 insertions(+), 70 deletions(-) diff --git a/modules/connectors/eshipper/carrier_generate.py b/modules/connectors/eshipper/carrier_generate.py index 38e09a3ec..1c56f6972 100644 --- a/modules/connectors/eshipper/carrier_generate.py +++ b/modules/connectors/eshipper/carrier_generate.py @@ -1,50 +1,92 @@ import json +from collections import Counter -from oauthlib.oauth2.rfc6749.grant_types.authorization_code import code_challenge_method_s256 -# Load the JSON data from the file -with open('schemas/carriers.json') as f: - data = json.load(f) +def clean_carrier_name(name): + return 'Fedex' if name == 'Federal Express' else name -for service in data: - carrier_name = service["carrierDTO"]["name"] - service_name = service["name"] - id = service["id"] - if carrier_name == 'Federal Express': - carrier_name = 'Fedex' +def generate_service_code(carrier_name, service_name): + if carrier_name not in service_name: + return f"{carrier_name} {service_name}" + return service_name - if not carrier_name in service_name: - code = f"{carrier_name} {service_name}" - else: - code = f"{service_name}" - code = ''.join(filter(lambda x: x.isalnum() or x in ['_', ' ', '-'], code.lower())) - code = (code +def clean_code(code): + code = ''.join(char.lower() for char in code if char.isalnum() or char in ['_', ' ', '-']) + return (code .replace(' ', '_') .replace(' ', '_') .replace('-', '_') .replace('e_shipper_', '') ) + +def generate_base_code(service): + carrier_name = clean_carrier_name(service["carrierDTO"]["name"]) + service_name = service["name"] + + code = generate_service_code(carrier_name, service_name) + code = clean_code(code) code = "eshipper_" + code - if id == 609: + if service['id'] == 609: code = "eshipper_ups_next_day_air_saver" - code = code + ' = "' + str(id) + '"' - code = " " + code - print(code) -# -# -# # Generate the Python code -# python_code = 'class ShippingService(lib.StrEnum):\n' -# python_code += ' """Carrier specific services"""\n\n' -# for service in shipping_services_list: -# python_code += f' {service} = "{data[service]}"\n' -# -# # Save the Python code to a file -# with open('shipping_services.py', 'w') as f: -# f.write(python_code) -# -# print('File generated: shipping_services.py') + return code + + +def resolve_duplicate(code, service, iteration): + if iteration == 1 and service["code"] and service["code"].lower() not in code: + return f"{code}_{service['code'].lower()}" + elif iteration == 2 and service["codeUs"] and service["codeUs"].lower() not in code: + return f"{code}_{service['codeUs'].lower()}" + elif iteration == 3 and service["mode"] and str(service["mode"]) not in code: + return f"{code}_{service['mode']}" + return code + + +def generate_codes(data): + codes = [(generate_base_code(service), str(service["id"]), service) for service in data] + duplicates = [code for code, count in Counter(code for code, _, _ in codes).items() if count > 1] + + for iteration in range(1, 5): # Now we go up to 4 iterations + new_codes = [] + duplicate_counts = {} # To keep track of how many times we've seen each duplicate + + for code, id, service in codes: + if code in duplicates: + if iteration < 4: + new_code = resolve_duplicate(code, service, iteration) + else: + # In the 4th iteration, append a number + duplicate_counts[code] = duplicate_counts.get(code, 0) + 1 + new_code = f"{code}_{duplicate_counts[code]}" + else: + new_code = code + new_codes.append((new_code, id, service)) + + codes = new_codes + duplicates = [code for code, count in Counter(code for code, _, _ in codes).items() if count > 1] + + if not duplicates: + break + + return [(code, id) for code, id, _ in codes], duplicates + + +def main(): + with open('schemas/carriers.json') as f: + data = json.load(f) + + final_codes, final_duplicates = generate_codes(data) + + print("Duplicates:", final_duplicates) + + # Uncomment to print all codes + for code, id in final_codes: + print(f' {code} = "{id}"') + + +if __name__ == "__main__": + main() diff --git a/modules/connectors/eshipper/karrio/providers/eshipper/units.py b/modules/connectors/eshipper/karrio/providers/eshipper/units.py index 8abe01a9b..c75f43c18 100644 --- a/modules/connectors/eshipper/karrio/providers/eshipper/units.py +++ b/modules/connectors/eshipper/karrio/providers/eshipper/units.py @@ -68,19 +68,19 @@ class ShippingService(lib.StrEnum): eshipper_trucking_averitt_express = "1818" eshipper_trucking_blue_sky_express = "1852" eshipper_trucking_central_freight = "1819" - eshipper_trucking_central_transport = "1845" - # eshipper_trucking_central_transport = "1850" + eshipper_trucking_central_transport_2029 = "1845" + eshipper_trucking_central_transport_2129 = "1850" eshipper_trucking_chi_cargo = "1842" - eshipper_trucking_conway = "1820" - # eshipper_trucking_conway = "1808" + eshipper_trucking_conway_2004 = "1820" + eshipper_trucking_conway_1908 = "1808" eshipper_trucking_day_and_ross = "1813" eshipper_trucking_day_and_ross_r_and_l = "1814" eshipper_trucking_dayton = "1821" eshipper_trucking_dayton_freight = "1805" eshipper_trucking_dependable_highway_express = "1812" eshipper_trucking_drug_transport = "1822" - eshipper_trucking_estes = "1848" - # eshipper_trucking_estes = "1823" + eshipper_trucking_estes_2107 = "1848" + eshipper_trucking_estes_2007 = "1823" eshipper_trucking_fedex_east = "1844" eshipper_trucking_fedex_national = "1826" eshipper_trucking_fedex_west = "1825" @@ -112,10 +112,10 @@ class ShippingService(lib.StrEnum): eshipper_trucking_tiffany_trucking = "1859" eshipper_trucking_ups = "1815" eshipper_trucking_ups_freight = "1860" - eshipper_trucking_usf_holland = "1827" - # eshipper_trucking_usf_holland = "1811" - eshipper_trucking_usf_reddaway = "1810" - # eshipper_trucking_usf_reddaway = "1835" + eshipper_trucking_usf_holland_2011 = "1827" + eshipper_trucking_usf_holland_1911 = "1811" + eshipper_trucking_usf_reddaway_1910 = "1810" + eshipper_trucking_usf_reddaway_2019 = "1835" eshipper_trucking_ward = "1840" eshipper_trucking_wilson = "1841" eshipper_trucking_wilson_truckingtfc = "1800" @@ -126,12 +126,12 @@ class ShippingService(lib.StrEnum): eshipper_eshipper_ltl_apex_trucking = "4209" eshipper_eshipper_ltl_fastfrate_rail = "4207" eshipper_eshipper_ltl_kindersley_expedited = "4601" - eshipper_eshipper_ltl_kindersley_rail = "4600" - # eshipper_eshipper_ltl_kindersley_rail = "4210" + eshipper_eshipper_ltl_kindersley_rail_2 = "4600" + eshipper_eshipper_ltl_kindersley_rail_3 = "4210" eshipper_eshipper_ltl_kindersley_regular = "4602" eshipper_eshipper_ltl_kindersley_road_ = "4211" - eshipper_eshipper_ltl_mo_rail = "4204" - # eshipper_eshipper_ltl_mo_rail = "4205" + eshipper_eshipper_ltl_mo_rail_3_1 = "4204" + eshipper_eshipper_ltl_mo_rail_3_2 = "4205" eshipper_eshipper_ltl_western_canada_rail = "4203" eshipper_fastfrate_rail = "5929" eshipper_fedex_2day_freight = "33" @@ -150,31 +150,31 @@ class ShippingService(lib.StrEnum): eshipper_fedex_freight_ltl_fedex_freight_economy = "5936" eshipper_fedex_freight_ltl_fedex_freight_priority = "5935" eshipper_project44_a_duie_pyle = "5968" - eshipper_project44_aaa_cooper_transportation = "4030" - # eshipper_project44_aaa_cooper_transportation = "5955" + eshipper_project44_aaa_cooper_transportation_aact_dls = "4030" + eshipper_project44_aaa_cooper_transportation_aact_wwex = "5955" eshipper_project44_aberdeen_express = "4041" eshipper_project44_abf_freight = "5960" eshipper_project44_averitt_express = "4028" eshipper_project44_central_freight_lines = "4015" - eshipper_project44_central_transport = "4034" - # eshipper_project44_central_transport = "5964" + eshipper_project44_central_transport_ctii_dls = "4034" + eshipper_project44_central_transport_ctii_wwex = "5964" eshipper_project44_chicago_suburban_express = "4035" eshipper_project44_clear_lane_freight = "4044" - eshipper_project44_con_way_freight = "4006" - # eshipper_project44_con_way_freight = "5952" + eshipper_project44_con_way_freight_cnwy_dls = "4006" + eshipper_project44_con_way_freight_cnwy_wwex = "5952" eshipper_project44_crosscountry_courier = "4032" eshipper_project44_day_ross = "4048" eshipper_project44_daylight_transport = "5958" eshipper_project44_dayton_freight_lines = "4021" - eshipper_project44_dependable_highway_express = "4040" - # eshipper_project44_dependable_highway_express = "5965" + eshipper_project44_dependable_highway_express_dphe_dls = "4040" + eshipper_project44_dependable_highway_express_dphe_wwex = "5965" eshipper_project44_dohrn_transfer_company = "4027" eshipper_project44_dugan_truck_line = "4025" eshipper_project44_estes_express_lines = "4010" eshipper_project44_estes = "5950" eshipper_project44_expedited_freight_systems = "4026" - eshipper_project44_fedex_freight_canada = "4054" - # eshipper_project44_fedex_freight_canada = "4019" + eshipper_project44_fedex_freight_canada_fxfe = "4054" + eshipper_project44_fedex_freight_canada_fxfc = "4019" eshipper_project44_fedex_freight_east = "4008" eshipper_project44_fedex_freight_usa = "4055" eshipper_project44_fedex_national = "4009" @@ -188,16 +188,16 @@ class ShippingService(lib.StrEnum): eshipper_project44_monroe_transportation_services = "4036" eshipper_project44_mountain_valley_express = "5963" eshipper_project44_nm_transfer = "4039" - eshipper_project44_new_england_motor_freight = "4013" - # eshipper_project44_new_england_motor_freight = "4012" + eshipper_project44_new_england_motor_freight_nemf = "4013" + eshipper_project44_new_england_motor_freight_nemf_dls = "4012" eshipper_project44_new_penn_motor_express = "4003" eshipper_project44_oak_harbor_freight = "5966" eshipper_project44_old_dominion_freight = "5954" - eshipper_project44_pitt_ohio = "4020" - # eshipper_project44_pitt_ohio = "5956" + eshipper_project44_pitt_ohio_pitd_dls = "4020" + eshipper_project44_pitt_ohio_pitd_wwex = "5956" eshipper_project44_rl_carriers = "5953" - eshipper_project44_roadrunner_transportation_services = "4001" - # eshipper_project44_roadrunner_transportation_services = "4017" + eshipper_project44_roadrunner_transportation_services_rdfs = "4001" + eshipper_project44_roadrunner_transportation_services_rdfs_dls = "4017" eshipper_project44_roadrunner_transportation_systems = "5951" eshipper_project44_saia_ltl_freight = "5957" eshipper_project44_saia_motor_freight = "4016" @@ -206,14 +206,14 @@ class ShippingService(lib.StrEnum): eshipper_project44_southwestern_motor_transport = "4033" eshipper_project44_standard_forwarding = "4042" eshipper_project44_total_transportation_distribution = "4046" - eshipper_project44_ups = "4022" - # eshipper_project44_ups = "4004" - # eshipper_project44_ups = "5959" + eshipper_project44_ups_upgf = "4022" + eshipper_project44_ups_upgf_dls = "4004" + eshipper_project44_ups_upgf_wwex = "5959" eshipper_project44_usf_reddaway = "4029" eshipper_project44_valley_cartage = "4038" eshipper_project44_vision_express_ltl = "4014" - eshipper_project44_ward_trucking = "4037" - # eshipper_project44_ward_trucking = "5962" + eshipper_project44_ward_trucking_dls = "4037" + eshipper_project44_ward_trucking_wwex = "5962" eshipper_project44_xpo_logistics = "4059" eshipper_project44_yrc = "4002" eshipper_purolator_express = "4" @@ -236,8 +236,8 @@ class ShippingService(lib.StrEnum): eshipper_sameday_h5_delivery_to_room_of_choice___2_man = "3611" eshipper_sameday_h6_delivery_packaging_removal___2_man = "3612" eshipper_sameday_ltl_service = "3606" - eshipper_skip = "5902" - # eshipper_skip = "5901" + eshipper_skip_est = "5902" + eshipper_skip_epr = "5901" eshipper_tforce_freight_ltl = "5931" eshipper_tforce_freight_ltl___guaranteed = "5932" eshipper_tforce_freight_ltl___guaranteed_am = "5933"