diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/report_ops/__main__.py | 2 | ||||
| -rw-r--r-- | python/report_ops/cash.py | 1 | ||||
| -rw-r--r-- | python/report_ops/custodians.py | 189 |
3 files changed, 100 insertions, 92 deletions
diff --git a/python/report_ops/__main__.py b/python/report_ops/__main__.py index 513683d5..ce712745 100644 --- a/python/report_ops/__main__.py +++ b/python/report_ops/__main__.py @@ -81,7 +81,7 @@ if args.cash_reports: for fund, custodians in _fund_custodians.items(): for custodian in custodians: try: - CashReport[(fund, custodian)](args.date).to_db() + cash_report = CashReport[(fund, custodian)](args.date).to_db() except ValueError as e: logger.warning(e) diff --git a/python/report_ops/cash.py b/python/report_ops/cash.py index 41a4dd61..b1823465 100644 --- a/python/report_ops/cash.py +++ b/python/report_ops/cash.py @@ -38,7 +38,6 @@ class CashReport: return cls._registry[key] def get_cash_report(self, report_prefix): - self.download_reports(self.date) report_dir = get_dir(self.date) report_dir.mkdir(exist_ok=True, parents=True) p = max( diff --git a/python/report_ops/custodians.py b/python/report_ops/custodians.py index 16940e1a..ca03eccf 100644 --- a/python/report_ops/custodians.py +++ b/python/report_ops/custodians.py @@ -104,118 +104,127 @@ def upload_to_custodian(account, trade_date, upload): conn.commit() -@dataclass -class Custodian: - date: datetime.date - account: ClassVar[str] - em = ExchangeMessage() +_fund_folder_mapping = { + "SERCGMAST": "NYOps", + "BOWDST": "BowdoinOps", + "ISOSEL": "SeleneOps", +} - def __init_subclass__(cls, account): - cls.account = account - -class NT(Custodian, account="NT"): - @classmethod - def download_reports(cls, date=datetime.date.today()): - for msg in cls.em.get_msgs( - path=["SeleneOps", "Passport"], start_date=date, end_date=date - ): - for attach in msg.attachments: - message_time = attach.last_modified_time.replace( - tzinfo=cls.em._account.default_timezone - ) - if attach.name == "Attachment1.pgp": - dest = get_dir(message_time.date(), archived=False) - dest.mkdir(exist_ok=True, parents=True) - with attach.fp as fp: - plaintext, result, verify_result = gpg.Context().decrypt( - fp.read(), passphrase="Serenitas1" - ) - fname = ( - "custodian_wires" - if "custodian" in verify_result.file_name - else "cash" +def download_nt_reports(date, fund, em): + for msg in em.get_msgs( + path=[_fund_folder_mapping[fund], "Passport"], start_date=date, end_date=date + ): + for attach in msg.attachments: + message_time = attach.last_modified_time.replace( + tzinfo=cls.em._account.default_timezone + ) + if attach.name == "Attachment1.pgp": + dest = get_dir(message_time.date(), archived=False) + dest.mkdir(exist_ok=True, parents=True) + with attach.fp as fp: + plaintext, result, verify_result = gpg.Context().decrypt( + fp.read(), passphrase="Serenitas1" ) - dest = dest / f"{fname}_{message_time:%Y%m%d%H%M}.csv" + if "custodian" in verify_result.file_name: + fname = "NtWires_{fund}" + elif "cash" in verify_result.filename: + fname = "NtCash_{fund}" + else: + pass + p = dest / f"{fname}_{message_time:%Y%m%d%H%M}.csv" + if not p.exists(): with open(dest, "w") as csvFile: text = plaintext.decode("utf-8").replace("\t", ",") csvFile.write(text) -class UMB(Custodian, account="UMB"): - @classmethod - def download_reports(cls, date=datetime.date.today()): - for msg in cls.em.get_msgs( - count=20, path=["NYops", "Powerstation"], start_date=date, end_date=date - ): - for attach in msg.attachments: +def download_umb_reports(date, fund, em): + for msg in em.get_msgs( + count=20, + path=[_fund_folder_mapping[fund], "Powerstation"], + start_date=date, + end_date=date, + ): + for attach in msg.attachments: + if fname.endswith("xlsx"): ts = attach.last_modified_time.replace( tzinfo=cls.em._account.default_timezone ) dest = get_dir(date, archived=False) dest.mkdir(exist_ok=True, parents=True) if attach.name.startswith("cash_balances_umb"): - p = dest / f"umb_{ts:%Y%m%d%H%M}.xlsx" + p = dest / f"UmbCash_{ts:%Y%m%d%H%M}.xlsx" elif attach.name.startswith("umb_serenitas_wires"): - p = dest / f"umbwires_{ts:%Y%m%d%H%M}.xlsx" + p = dest / f"UmbWires_{ts:%Y%m%d%H%M}.xlsx" else: pass if not p.exists(): p.write_bytes(attach.content) -class BNY(Custodian, account="BONY2"): - @classmethod - def download_reports(cls, date=datetime.date.today()): - for msg in cls.em.get_msgs( - 20, - path=["BowdoinOps", "Reports"], - subject__startswith="Document(s) from Reporting", - start_date=date, - end_date=date, - ): - if msg.sender == "notify@bnymellon.com": - for attach in msg.attachments: - fname = attach.name - if fname.endswith("csv"): - file_type, date_part = fname.split("_") - date = dp.parse(date_part.removesuffix(".csv")).date() - p = DAILY_DIR / str(date) / "Reports" / fname - if not p.parent.exists(): - p.parent.mkdir(parents=True, exist_ok=True) - if not p.exists(): - p.write_bytes(attach.content) +def download_bny_reports(date, fund, em): + for msg in cls.em.get_msgs( + 20, + path=[_fund_folder_mapping[fund], "Reports"], + subject__startswith="Document(s) from Reporting", + sender="notify@bnymellon.com", + start_date=date, + end_date=date, + ): + for attach in msg.attachments: + fname = attach.name + if fname.endswith("csv"): + file_type, timestamp = fname.split("_") + date = dp.parse(timestamp.removesuffix(".csv")).date() + p = DAILY_DIR / str(date) / "Reports" / fname + if not p.parent.exists(): + p.parent.mkdir(parents=True, exist_ok=True) + if not p.exists(): + p.write_bytes(attach.content) -class SCOTIA(Custodian, account="SCOTIA"): - @staticmethod - def download_reports(date=datetime.date.today()): - try: - return download_scotia_report(date) - except ( - ElementNotInteractableException, - TimeoutException, - ) as e: - print(e) - raise ValueError(f"Scotia report not ready yet for {date}") +def download_scotia_reports(date, fund): + try: + return download_scotia_report(date) + except ( + ElementNotInteractableException, + TimeoutException, + ) as e: + print(e) + raise ValueError(f"No Scotia report {fund}:{date}") -class GSTXB(Custodian, account="GSTXB"): - @staticmethod - def download_reports(date=datetime.date(2023, 5, 17)): - sftp = Client.from_creds("gstx") - pattern = r"(\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}\.\d{6})" - for f in sftp.client.listdir(): - match = re.search(pattern, f).group(1) - ts = datetime.datetime.strptime(match, "%Y-%m-%dT%H_%M_%S.%f") - if ts.date() == date: - with sftp.client.open(f) as fh: - plaintext, result, verify_result = gpg.Context().decrypt( - fh.read(), passphrase="Serenitas;1" - ) - dest = DAILY_DIR / "Serenitas" / "GSTX_reports" / f"bai_{date}.csv" - with open(dest, "w") as csvFile: - text = plaintext.decode("utf-8") - csvFile.write(text) - return - raise ValueError(f"GS TXB report not ready yet for {date}") +def download_gstx_reports(date, fund): + sftp = Client.from_creds("gstx") + pattern = r"(\d{4}-\d{2}-\d{2}T\d{2}_\d{2}_\d{2}\.\d{6})" + for f in sftp.client.listdir(): + match = re.search(pattern, f).group(1) + ts = datetime.datetime.strptime(match, "%Y-%m-%dT%H_%M_%S.%f") + if ts.date() == date: + with sftp.client.open(f) as fh: + plaintext, result, verify_result = gpg.Context().decrypt( + fh.read(), passphrase="Serenitas;1" + ) + dest = DAILY_DIR / "Serenitas" / "GSTX_reports" / f"bai_{date}.csv" + with open(dest, "w") as csvFile: + text = plaintext.decode("utf-8") + csvFile.write(text) + return + raise ValueError(f"No Gstx report {fund}:{date}") + + +def get_custodian_download_function(custodian): + match custodian: + case "SCOTIA": + return download_scotia_reports + case "GSTX": + return download_gstx_reports + case "BNY": + return download_bny_reports + case "UMB": + return download_umb_reports + case "NT": + return download_nt_reports + case _: + raise ValueError(f"Custodian Value {custodian} not valid") |
