#!/usr/bin/env python3 """ Update X3 with information from middleware To reprocess a file, move it to the correct folder on the FTP """ import shutil import yamamotoyama import pathlib THIS_DIRECTORY = pathlib.Path(__file__).parent RECEIVED_867_DIRECTORY = THIS_DIRECTORY / "processed_867s" ARCHIVE = RECEIVED_867_DIRECTORY / "archive" INSERT_867s = """\ execute [PROD].[insert_shandex_867_data] :ctrlnum, :credat; """ def main(): """ Do it! """ process_received_867s() remove_completed_867s() def remove_completed_867s(): """ Clean up all of the files received from the FTP """ for file in RECEIVED_867_DIRECTORY.iterdir(): if file.name.endswith('.edi'): shutil.move(file,ARCHIVE / file.name) def import_received_867s(data): with yamamotoyama.get_connection() as data_base: with data_base.transaction(): data_base.bulk_query(INSERT_867s, data) def process_received_867s(): #pull out the control number on the GS line for file in RECEIVED_867_DIRECTORY.iterdir(): control_number = '' transaction_date = '' if file.name.endswith('.edi'): with file.open(encoding="utf-8", newline="") as edi_file: for record in edi_file.read().split("~"): fields = record.split("*") if fields[0] == 'GS': control_number = fields[6] if fields[0] == 'BPT': transaction_date = fields[3] data = { "ctrlnum":control_number, "credat":transaction_date } import_received_867s(data) if __name__ == "__main__": main()