shandex_edi_2024/master_contoller.py

106 lines
3.1 KiB
Python
Raw Permalink Normal View History

2024-03-13 14:53:19 -07:00
#!/usr/bin/env python3
"""
Control middleware
"""
import contextlib
import shutil
import pathlib
import paramiko # type: ignore
import pprint
2024-03-14 15:46:57 -07:00
import edi_997_inbound
import edi_944
import edi_947
import edi_846
import edi_867_to_table
2024-03-20 09:42:04 -07:00
import edi_997_outbound
2024-03-14 15:46:57 -07:00
import update_shandex_dashboard
2024-03-20 09:42:04 -07:00
import edi_943
import unprocessed_files_report
import import_867s
2024-03-13 14:53:19 -07:00
THIS_DIRECTORY = pathlib.Path(__file__).parent
X12_SHANDEX_OUTGOING = THIS_DIRECTORY / "outgoing"
X12_SHANDEX_INCOMING = THIS_DIRECTORY / "incoming"
def main():
"""
Do it!
"""
2024-03-14 15:46:57 -07:00
#pick up files from Shandex
2024-03-20 09:42:04 -07:00
retrieve_x12_edi_files_shandex()
2024-03-14 15:46:57 -07:00
#report on anything not handled
unprocessed_files_report.main()
2024-03-14 15:46:57 -07:00
#process all EDIs that started with Shandex
edi_997_inbound.main()
edi_944.main()
edi_947.main()
edi_846.main()
edi_867_to_table.main()
import_867s.main()
2024-03-14 15:46:57 -07:00
# process all EDIs that start with us
2024-03-20 09:42:04 -07:00
edi_943.main()
edi_997_outbound.main()
2024-03-14 15:46:57 -07:00
2024-03-13 14:53:19 -07:00
#send them to Shandex
send_x12_edi_files_shandex()
#update dashboard - no longer needed now that it has been moved to the staging database 2024-08-20
#update_shandex_dashboard.main()
2024-03-14 15:46:57 -07:00
#report on anything not handled
unprocessed_files_report.main()
2024-03-14 15:46:57 -07:00
2024-03-13 14:53:19 -07:00
SSH_DIRECTORY = THIS_DIRECTORY / "ssh"
SSH_KNOWN_HOSTS_FILE = str(SSH_DIRECTORY / "known_hosts")
SSH_KEY_FILENAME = str(SSH_DIRECTORY / "id_ed25519")
SHANDEX_SFTP_HOST = "ftp.shandex.com"
SHANDEX_SFTP_USERNAME = "Stash"
SHANDEX_SFTP_PASSWORD = "ST@Pass2024$$"
def send_x12_edi_files_shandex():
"""
Connect to FTP & send files.
"""
2024-03-20 09:42:04 -07:00
with paramiko.SSHClient() as ssh_client:
ssh_client.load_system_host_keys()
ssh_client.load_host_keys(SSH_KNOWN_HOSTS_FILE)
ssh_client.set_missing_host_key_policy(paramiko.client.RejectPolicy)
ssh_client.connect(
hostname=SHANDEX_SFTP_HOST, username=SHANDEX_SFTP_USERNAME, password=SHANDEX_SFTP_PASSWORD
)
with ssh_client.open_sftp() as sftp_connection:
sftp_connection.chdir("/Stash/Prod/ToShandex")
2024-03-20 09:42:04 -07:00
for filename in X12_SHANDEX_OUTGOING.glob("*.edi"):
sftp_connection.put(filename, str(filename.name))
shutil.move(filename, X12_SHANDEX_OUTGOING / "archive" / filename.name)
2024-03-13 14:53:19 -07:00
def retrieve_x12_edi_files_shandex():
with paramiko.SSHClient() as ssh_client:
ssh_client.load_system_host_keys()
ssh_client.load_host_keys(SSH_KNOWN_HOSTS_FILE)
ssh_client.set_missing_host_key_policy(paramiko.client.RejectPolicy)
ssh_client.connect(
hostname=SHANDEX_SFTP_HOST, username=SHANDEX_SFTP_USERNAME, password=SHANDEX_SFTP_PASSWORD
)
with ssh_client.open_sftp() as sftp_connection:
sftp_connection.chdir("/Stash/Prod/FromShandex")
2024-03-13 14:53:19 -07:00
for filename in sftp_connection.listdir():
if filename.endswith(".edi"):
sftp_connection.get(filename, X12_SHANDEX_INCOMING / filename)
new_filename = f"/Stash/Archive/{filename}"
2024-03-20 09:42:04 -07:00
sftp_connection.rename(filename, new_filename)
2024-03-13 14:53:19 -07:00
if __name__ == "__main__":
main()