diff options
| -rw-r--r-- | python/citco_ops/bowdst.py | 53 | ||||
| -rw-r--r-- | python/custodian_wire.py | 50 |
2 files changed, 52 insertions, 51 deletions
diff --git a/python/citco_ops/bowdst.py b/python/citco_ops/bowdst.py index 59f46769..9f1965fd 100644 --- a/python/citco_ops/bowdst.py +++ b/python/citco_ops/bowdst.py @@ -2,6 +2,10 @@ from dataclasses import dataclass import datetime from serenitas.ops.trade_dataclasses import Deal, Ccy from typing import ClassVar +from .custodians import NT, BNY +from .misc import get_dir +from dataclasses import field +from csv import DictReader _nt_to_currency = {"EURO - EUR": "EUR", "U.S. DOLLARS - USD": "USD"} @@ -17,13 +21,15 @@ class Wire(Deal, table_name="custodian_wires", deal_type="custodian_wires"): amount: float wire_details: str unique_ref: str + dtkey: ClassVar = field(metadata={"insert": False, "select": False}) - def __init_subclass__(cls, fund, **kwargs): + def __init_subclass__(cls, fund, dtkey, **kwargs): cls._sql_insert = ( cls._sql_insert.removesuffix("RETURNING *") + "ON CONFLICT (unique_ref) DO NOTHING RETURNING *" ) cls.fund = fund + cls.dtkey = dtkey def __post_init__(self): self.amount = self.amount.replace(",", "") @@ -32,10 +38,30 @@ class Wire(Deal, table_name="custodian_wires", deal_type="custodian_wires"): else: self.amount = float(self.amount) + @classmethod + def to_db(cls, fname, date): + cls.download_reports(date) + p = max( + [f for f in get_dir(date).iterdir() if fname in f.name], + key=cls.dtkey_fun(), + default=None, + ) + return p + + @classmethod + def dtkey_fun(cls): + def dtkey_fun(f): + return datetime.datetime.strptime( + f.name.removesuffix(".csv").removesuffix(".xlsx").rsplit("_")[-1], + cls.dtkey, + ) + + return dtkey_fun -class BowdstWire(Wire, fund="BOWDST"): + +class BowdstWire(Wire, BNY, fund="BOWDST", dtkey="%Y%m%d%H%M%S"): @classmethod - def from_nexen_line(cls, line: dict): + def from_report_line(cls, line: dict): return cls( date=line["Report Run Date"], entry_date=line["Cash Entry Date"], @@ -49,8 +75,17 @@ class BowdstWire(Wire, fund="BOWDST"): unique_ref=line["Reference Number"], ) + @classmethod + def to_db(cls, date): + p = super().to_db("BowdstWires", date) + with open(p) as fh: + reader = DictReader(fh) + for line in reader: + cls.from_report_line(line).stage() + cls.commit() + -class IsoselWire(Wire, fund="ISOSEL"): +class NTWire(Wire, NT, fund="ISOSEL", dtkey="%Y%m%d%H%M"): @classmethod def from_passport_line(cls, line: dict): return cls( @@ -63,3 +98,13 @@ class IsoselWire(Wire, fund="ISOSEL"): wire_details=line["narrative"], unique_ref=line["C-EXTL-SYS-TRN-DSC-3"], ) + + @classmethod + def to_db(cls, date): + p = super().to_db("custodian_wires", date) + with open(p) as fh: + reader = DictReader(fh) + for line in reader: + if "sponsor" in line["narrative"].lower(): + cls.from_preport_line(line).stage() + cls.commit() diff --git a/python/custodian_wire.py b/python/custodian_wire.py index 7c246696..e7b0a5bd 100644 --- a/python/custodian_wire.py +++ b/python/custodian_wire.py @@ -1,47 +1,6 @@ -from bowdst import get_dir, download_messages -from csv import DictReader -from citco_ops.bowdst import BowdstWire, IsoselWire -from citco_ops.cash import IsoselCashReport, dt_from_fname as nt_key +from citco_ops.bowdst import BowdstWire, NTWire import datetime - -def dt_from_fname(f): - return datetime.datetime.strptime( - f.name.split("_")[1].split(".")[0], "%Y%m%d%H%M%S" - ) - - -def load_bowdst_wire_report(workdate: datetime.date): - p = max( - [f for f in get_dir(workdate).iterdir() if "BowdstWires" in f.name], - key=dt_from_fname, - default=None, - ) - if not p: # No files available - return - with open(p) as fh: - reader = DictReader(fh) - for line in reader: - BowdstWire.from_nexen_line(line).stage() - BowdstWire.commit() - - -def load_isosel_wire_report(workdate: datetime.date): - p = max( - [f for f in get_dir(workdate).iterdir() if "custodian_wires" in f.name], - key=nt_key, - default=None, - ) - if not p: # No files available - return - with open(p) as fh: - reader = DictReader(fh) - for line in reader: - if "sponsor" in line["narrative"].lower(): - IsoselWire.from_passport_line(line).stage() - IsoselWire.commit() - - if __name__ == "__main__": import argparse from serenitas.utils.exchange import ExchangeMessage @@ -55,8 +14,5 @@ if __name__ == "__main__": help="working date", ) args = parser.parse_args() - em = ExchangeMessage() - download_messages(em) - IsoselCashReport.download_reports(args.workdate) - load_bowdst_wire_report(args.workdate) - load_isosel_wire_report(args.workdate) + for wire_report in (BowdstWire, NTWire): + wire_report.to_db(args.workdate) |
