diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/citco_ops/cash.py | 60 | ||||
| -rw-r--r-- | python/citco_ops/misc.py | 16 |
2 files changed, 56 insertions, 20 deletions
diff --git a/python/citco_ops/cash.py b/python/citco_ops/cash.py index 7d98d3cf..78f26330 100644 --- a/python/citco_ops/cash.py +++ b/python/citco_ops/cash.py @@ -1,6 +1,6 @@ from dataclasses import field, dataclass from serenitas.ops.trade_dataclasses import Deal, Fund -from serenitas.analytics.dates import prev_business_day +from serenitas.analytics.dates import prev_business_day, next_business_day import datetime import gpg from serenitas.utils.exchange import ExchangeMessage @@ -8,6 +8,13 @@ from serenitas.utils.env import DAILY_DIR import pandas as pd from serenitas.utils.db import dbconn, dawn_engine from typing import ClassVar +from .misc import get_dir + + +def dt_from_fname(f): + return datetime.datetime.strptime( + f.name.removesuffix(".csv").rsplit("_")[-1], "%Y%m%d%H%M" + ) @dataclass @@ -52,31 +59,48 @@ class CashReport: cls.fund = fund cls.account_number = account_number - def download_report(self): + +class IsoselCashReport(CashReport, fund="ISOSEL", account_number="ISOS01"): + def download_reports(self): em = ExchangeMessage() for msg in em.get_msgs(path=["SeleneOps", "Passport"]): for attach in msg.attachments: - message_date = attach.last_modified_time.date() + message_time = attach.last_modified_time if ( attach.name == "Attachment1.pgp" - and prev_business_day(message_date) == self.date + and prev_business_day(message_time.date()) == self.date ): - dest = ( - DAILY_DIR - / str(message_date) - / "Reports" - / f"{message_date:%Y%m%d}_ISOSEL.csv" - ) - dest.parent.mkdir(exist_ok=True, parents=True) - with attach.fp as fp, dest.open("w") as csvfh: + dest = get_dir(message_time.date()) + dest.mkdir(exist_ok=True, parents=True) + with attach.fp as fp: plaintext, result, verify_result = gpg.Context().decrypt( - fp.read(), passphrase="Serenitas1", sink=csvfh + fp.read(), passphrase="Serenitas1" ) - return dest - raise ValueError(f"No reports found for fund: {self.fund} date: {self.date}") + fname = ( + "custodian_wires" + if "custodian" in verify_result.file_name + else "cash" + ) + dest = dest / f"{fname}_{message_time:%Y%m%d%H%M}.csv" + with open(dest, "w") as csvFile: + text = plaintext.decode("utf-8").replace("\t", ",") + csvFile.write(text) def to_db(self): - df = pd.read_csv(self.download_report(), on_bad_lines="warn", sep="\t") + self.download_reports() + p = max( + [ + f + for f in get_dir(next_business_day(self.date)).iterdir() + if "cash" in f.name + ], + key=dt_from_fname, + ) + if not p: + raise ValueError( + f"No reports found for fund: {self.fund} date: {self.date}" + ) + df = pd.read_csv(p, on_bad_lines="warn") df = df[df["T-NARR-LONG"] == "CLOSING BALANCE"] df = df[["Consolidation", "Currency code", "A-TRAN-AMT", "Account name"]] df = df.reset_index(drop=True).rename( @@ -101,7 +125,3 @@ class CashReport: ) self._conn.commit() df.to_sql("cash_balances", index=False, if_exists="append", con=dawn_engine) - - -class IsoselCashReport(CashReport, fund="ISOSEL", account_number="ISOS01"): - pass diff --git a/python/citco_ops/misc.py b/python/citco_ops/misc.py new file mode 100644 index 00000000..1e02dc9a --- /dev/null +++ b/python/citco_ops/misc.py @@ -0,0 +1,16 @@ +import pathlib +import datetime +from serenitas.utils.env import DAILY_DIR + + +def get_dir(workdate: datetime.date) -> pathlib.Path: + p = DAILY_DIR / str(workdate) / "Reports" + if not p.exists(): + p = ( + DAILY_DIR + / str(workdate.year) + / f"{workdate:%Y_%m}" + / str(workdate) + / "Reports" + ) + return p |
