aboutsummaryrefslogtreecommitdiffstats
path: root/python/report_ops/custodians.py
blob: 102f53f312ca7a8d0135e012888bae73bb815120 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from serenitas.utils.exchange import ExchangeMessage
from serenitas.utils.env import DAILY_DIR
import warnings
import datetime
from .misc import get_dir, dt_from_fname
import gpg
import datetime
from serenitas.ops.custodians import upload_to_bbh, upload_to_umb
from typing import ClassVar
from dataclasses import dataclass


@dataclass
class Custodian:
    date: datetime.date
    account: str
    upload_fun: ClassVar

    def __init_subclass__(cls, account, upload_fun=None):
        cls.account = account
        cls.upload_fun = staticmethod(upload_fun)

    def __init__(self, date):
        self.date = date

    def upload_to_custodian(self, upload):
        self.upload_fun(upload, account=self.account, trade_date=self.date)


class NT(Custodian, account="NT"):
    @classmethod
    def download_reports(cls, date=datetime.date.today()):
        em = ExchangeMessage()
        for msg in em.get_msgs(path=["SeleneOps", "Passport"]):
            for attach in msg.attachments:
                message_time = attach.last_modified_time
                if attach.name == "Attachment1.pgp" and message_time.date() == date:
                    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"
                    )
                    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)


class UMB(Custodian, account="UMB", upload_fun=upload_to_umb):
    @classmethod
    def download_reports(cls, date=datetime.date.today()):
        em = ExchangeMessage()
        for msg in em.get_msgs(count=20, path=["NYops", "Powerstation"]):
            for attach in msg.attachments:
                timestamp = attach.last_modified_time
                if (
                    attach.name.startswith("cash_reporting")
                    and timestamp.date() == date
                ):
                    dest = get_dir(timestamp.date(), archived=False)
                    dest.mkdir(exist_ok=True, parents=True)
                    p = dest / f"umb_{timestamp:%Y%m%d%H%M}.xlsx"
                    if not p.exists():
                        p.write_bytes(attach.content)


class BNY(Custodian, account="BONY2"):
    @classmethod
    def download_reports(cls, date=datetime.date.today()):
        em = ExchangeMessage()
        bowdst_wire_recent = True
        for msg in em.get_msgs(
            20,
            path=["BowdoinOps", "Reports"],
            subject__startswith="Document(s) from Reporting",
        ):
            if msg.sender == "notify@bnymellon.com":
                for attach in msg.attachments:
                    fname = attach.name
                    if fname.endswith("csv"):
                        base_name = fname.removesuffix(".csv")
                        file_type, date_part = base_name.split("_")
                        match file_type:
                            case (
                                "Asset Detail"
                                | "Net Investment Earned Income by Security"
                                | "Settled Cash Statement"
                            ):
                                date = datetime.datetime.strptime(
                                    date_part, "%d %b %Y"
                                ).date()
                            case "BowdstWires":
                                try:
                                    date = datetime.datetime.strptime(
                                        date_part, "%Y%m%d%H%M%S"
                                    ).date()
                                except ValueError:
                                    date = datetime.datetime.strptime(
                                        date_part, "%d %b %Y%H%M%S"
                                    ).date()
                            case "Live-cash":
                                date = datetime.datetime.strptime(
                                    date_part, "%Y%m%d%H%M%S"
                                ).date()
                            case _:
                                warnings.warn(f"Unknown report type {file_type}")
                                continue
                        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 BBH(Custodian, account="BBH", upload_fun=upload_to_bbh):
    pass