-
-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
dedfaaf
commit 692efcc
Showing
2 changed files
with
112 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters