diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/citco_ops/cash.py | 32 |
1 files changed, 14 insertions, 18 deletions
diff --git a/python/citco_ops/cash.py b/python/citco_ops/cash.py index 42e1f76d..8c47744f 100644 --- a/python/citco_ops/cash.py +++ b/python/citco_ops/cash.py @@ -7,6 +7,7 @@ from serenitas.utils.exchange import ExchangeMessage from serenitas.utils.env import DAILY_DIR import pandas as pd from serenitas.utils.db import dbconn, dawn_engine +from typing import ClassVar @dataclass @@ -42,15 +43,13 @@ class IAMDeal(Deal, deal_type=None, table_name="iam_tickets"): @dataclass class CashReport: - fund: str - _conn: dbconn = dbconn("dawndb") + fund: ClassVar[str] + date: datetime.date + _conn: ClassVar[dbconn] = dbconn("dawndb") def __init_subclass__(cls, fund): cls.fund = fund - def __init__(self, date): - self.date = date - def download_report(self): em = ExchangeMessage() for msg in em.get_msgs(path=["SeleneOps", "Passport"]): @@ -60,23 +59,20 @@ class CashReport: attach.name == "Attachment1.pgp" and prev_business_day(message_date) == self.date ): - with attach.fp as fp: + dest = ( + DAILY_DIR + / str(message_date) + / f"{message_date:%Y%m%d}_ISOSEL.csv" + ) + with attach.fp as fp, dest.open("w") as csvfh: plaintext, result, verify_result = gpg.Context().decrypt( - fp.read(), passphrase="Serenitas1" - ) - dest = ( - DAILY_DIR - / message_date.strftime("%Y-%m-%d") - / f"{message_date.strftime('%Y%m%d')}_ISOSEL.csv" + fp.read(), passphrase="Serenitas1", sink=csvfh ) - with open(dest, "w") as csvFile: - text = plaintext.decode("utf-8").replace("\t", ",") - csvFile.write(text) - return dest + return dest raise ValueError(f"No reports found for fund: {self.fund} date: {self.date}") def to_db(self): - df = pd.read_csv(self.download_report(), on_bad_lines="warn") + df = pd.read_csv(self.download_report(), on_bad_lines="warn", sep="\t") 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( @@ -92,7 +88,7 @@ class CashReport: df["fund"] = self.fund with self._conn.cursor() as c: c.execute( - "DELETE from cash_balances where fund=%s and date=%s", + "DELETE FROM cash_balances WHERE fund=%s AND date=%s", (self.fund, self.date), ) self._conn.commit() |
