diff options
Diffstat (limited to 'python/report_ops')
| -rw-r--r-- | python/report_ops/__main__.py | 33 | ||||
| -rw-r--r-- | python/report_ops/cash.py | 77 | ||||
| -rw-r--r-- | python/report_ops/scotia.py | 2 |
3 files changed, 57 insertions, 55 deletions
diff --git a/python/report_ops/__main__.py b/python/report_ops/__main__.py index 487954dd..415cfe07 100644 --- a/python/report_ops/__main__.py +++ b/python/report_ops/__main__.py @@ -1,9 +1,11 @@ -from serenitas.analytics.dates import prev_business_day -from serenitas.utils.db import dbconn -from serenitas.utils.exchange import ExchangeMessage import logging import argparse import datetime + +from serenitas.analytics.dates import prev_business_day +from serenitas.utils.db import dbconn +from serenitas.utils.exchange import ExchangeMessage + from .sma import SMA from .cash import CashReport from .admin import CitcoReport @@ -82,12 +84,17 @@ if args.cash_reports: em = ExchangeMessage() for fund, custodians in _fund_custodians.items(): for custodian in custodians: - try: - get_custodian_download_fun(custodian)(args.date, fund, em=em) - cash_report = CashReport[custodian] - cash_report.to_db(args.date, fund) - except ValueError as e: - logger.warning(e) + get_custodian_download_fun(custodian)(args.date, fund, em=em) + cash_report = CashReport[custodian] + cash_report.to_db(args.date, fund) + +if args.wire_reports: + em = ExchangeMessage() + for fund, custodians in _fund_custodians.items(): + for custodian in custodians: + get_custodian_download_fun(custodian)(args.date, fund, em=em) + wire_report = WireReport[custodian] + wire_report.to_db(args.date, fund) if args.isosel_reports: for fund in ( @@ -101,14 +108,6 @@ if args.isosel_reports: except ValueError as e: logger.warning(e) -if args.wire_reports: - for fund, custodians in _fund_custodians.items(): - for custodian in custodians: - try: - Wire[(fund, custodian)].to_db(args.date) - except ValueError as e: - logger.warning(e) - if args.send_to_custodians: for account in ( "BBH", diff --git a/python/report_ops/cash.py b/python/report_ops/cash.py index ffc5f4c8..94837697 100644 --- a/python/report_ops/cash.py +++ b/python/report_ops/cash.py @@ -1,15 +1,15 @@ -from serenitas.analytics.dates import prev_business_day import datetime import pandas as pd -from serenitas.utils.db import dbconn from typing import ClassVar -from .misc import get_dir, dt_from_fname -from .custodians import get_custodian_download_fun from functools import partial from dataclasses import dataclass - from serenitas.utils.env import DAILY_DIR +from serenitas.utils.db import dbconn +from serenitas.analytics.dates import prev_business_day + +from .misc import get_dir, dt_from_fname +from .custodians import get_custodian_download_fun @dataclass @@ -30,21 +30,19 @@ class CashReport: def __class_getitem__(cls, key): return cls._registry[key] - def get_cash_report(self, report_prefix): - report_dir = get_dir(self.date) + @classmethod + def get_report(cls, date, fund, prefix=None): + report_dir = get_dir(date) report_dir.mkdir(exist_ok=True, parents=True) + prefix = prefix if prefix else f"{cls.custodian}_CASH_{fund}" p = max( - [ - f - for f in get_dir(self.date).iterdir() - if f.name.startswith(report_prefix) - ], - key=partial(dt_from_fname, dt_format=self.dtkey), + [f for f in get_dir(date).iterdir() if f.name.startswith(prefix)], + key=partial(dt_from_fname, dt_format=cls.dtkey), default=None, ) if not p: raise ValueError( - f"No reports found for fund: {self.fund} date: {self.date}" + f"No reports found for fund: {prefix.split('_')[-1]} date: {date}" ) return p @@ -58,13 +56,14 @@ class CashReport: def clear(cls): cls._staging_queue.clear() - def stage_from_row(self, row, fund): + @classmethod + def stage_from_row(cls, row, date, fund): (account, currency), amount = row - self._staging_queue.add( + cls._staging_queue.add( ( - prev_business_day(self.date), - self.fund, - f"{self.custodian} Custody Account {self.fund}", + prev_business_day(date), + fund, + f"{cls.custodian} Custody Account {fund}", account, currency, amount, @@ -72,16 +71,17 @@ class CashReport: ) @classmethod - def to_db(self, fund): - for row in self.yield_rows(): - self.stage_from_row(row, fund) - self.commit() - self.clear() + def to_db(cls, date, fund): + for row in cls.yield_rows(date, fund): + cls.stage_from_row(row, date, fund) + cls.commit() + cls.clear() class NTCashReport(CashReport, custodian="NT", dtkey="%Y%m%d%H%M"): - def yield_rows(self, fund): - p = self.get_cash_report(f"NT_CASH_{fund}") + @classmethod + def yield_rows(cls, date, fund): + p = cls.get_report(date, fund) 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"]] @@ -91,8 +91,9 @@ class NTCashReport(CashReport, custodian="NT", dtkey="%Y%m%d%H%M"): class UMBCashReport(CashReport, custodian="UMB", dtkey="%Y%m%d%H%M"): - def yield_rows(self, fund): - p = self.get_cash_report("UMB_CASH_{fund}") + @classmethod + def yield_rows(cls, date, fund): + p = cls.get_report(date, fund) df = pd.read_excel(p, skiprows=3) yield from df.groupby(["Portfolio #", "Currency"]).sum(numeric_only=True)[ "Current Balance" @@ -100,8 +101,9 @@ class UMBCashReport(CashReport, custodian="UMB", dtkey="%Y%m%d%H%M"): class BNYCashReport(CashReport, custodian="BNY", dtkey="%Y%m%d%H%M%S"): - def yield_rows(self): - p = self.get_cash_report("BNY_CASH_{fund}") + @classmethod + def yield_rows(cls, date, fund): + p = cls.get_report(date, fund) df = pd.read_csv(p) df["Beginning Balance Local"] = df["Beginning Balance Local"].apply( lambda s: "-" + s[1:-1] if s.startswith("(") else s @@ -115,8 +117,9 @@ class BNYCashReport(CashReport, custodian="BNY", dtkey="%Y%m%d%H%M%S"): class ScotiaCashReport(CashReport, custodian="SCOTIA", dtkey="%Y%m%d%H%M%S"): - def yield_rows(self): - p = self.get_cash_report() + @classmethod + def yield_rows(cls, date, fund): + p = cls.get_report(date, fund) df = pd.read_excel(p, skipfooter=1) if df.empty: # No wires, so we can't skip the footer @@ -126,14 +129,14 @@ class ScotiaCashReport(CashReport, custodian="SCOTIA", dtkey="%Y%m%d%H%M%S"): df.loc[0]["Closing Bal."], ) - def get_cash_report(self, prefix=None): - self.download_reports(self.date) + @classmethod + def get_report(cls, date, fund): REPORT_DIR = DAILY_DIR / "Selene" / "Scotia_reports" try: return next( REPORT_DIR.glob( - f"IsoSelene_{prev_business_day(self.date):%d-%b-%Y}_*_xlsx.JOAAPKO3.JOAAPKO1" + f"IsoSelene_{prev_business_day(date):%d-%b-%Y}_*_xlsx.JOAAPKO3.JOAAPKO1" ) ) - except StopIteration as e: # File doesn't exist, let's get it" - raise ValueError(f"No file available for Scotia on {self.date}") from e + except StopIteration as e: + raise ValueError(f"No file available for Scotia on {date}") from e diff --git a/python/report_ops/scotia.py b/python/report_ops/scotia.py index ffd39c70..c20af89c 100644 --- a/python/report_ops/scotia.py +++ b/python/report_ops/scotia.py @@ -67,7 +67,7 @@ def get_attachment_element(driver): @contextmanager def create_driver(download_dir): options = Options() - options.add_argument("--headless") + # options.add_argument("--headless") fp = webdriver.FirefoxProfile() fp.set_preference("browser.download.dir", str(download_dir)) fp.set_preference("browser.download.folderList", 2) |
