-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_main.py
More file actions
530 lines (445 loc) · 15.2 KB
/
_main.py
File metadata and controls
530 lines (445 loc) · 15.2 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from __future__ import annotations
import argparse
import asyncio
import json
import os
import sqlite3
from dataclasses import dataclass
from importlib import resources
from importlib.metadata import version
from pathlib import Path
from typing import Any
from typing import ClassVar
from typing import Literal
from typing import overload
from typing import Protocol
from typing import TYPE_CHECKING
from urllib.parse import urlencode
from urllib.parse import urljoin
from urllib.parse import urlunparse
import aiohttp
from tqdm import tqdm
from sqlite_export_for_ynab import ddl
if TYPE_CHECKING:
from collections.abc import Awaitable, Sequence
from typing import Never
_EntryTable = (
Literal["accounts"]
| Literal["account_periodic_values"]
| Literal["category_groups"]
| Literal["categories"]
| Literal["payees"]
| Literal["transactions"]
| Literal["subtransactions"]
| Literal["scheduled_transactions"]
| Literal["scheduled_subtransactions"]
)
_ALL_RELATIONS = frozenset(
("budgets", "flat_transactions", "scheduled_flat_transactions")
+ tuple(lit.__args__[0] for lit in _EntryTable.__args__)
)
_ENV_TOKEN = "YNAB_PERSONAL_ACCESS_TOKEN"
_PACKAGE = "sqlite-export-for-ynab"
async def async_main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser(prog=_PACKAGE)
parser.add_argument(
"--db",
help="The path to the SQLite database file.",
type=Path,
default=default_db_path(),
)
parser.add_argument(
"--full-refresh",
action="store_true",
help="**DROP ALL TABLES** and fetch all budget data again.",
)
parser.add_argument(
"--version", action="version", version=f"%(prog)s {version(_PACKAGE)}"
)
args = parser.parse_args(argv)
db: Path = args.db
full_refresh: bool = args.full_refresh
token = os.environ.get(_ENV_TOKEN)
if not token:
raise ValueError(
f"Must set YNAB access token as {_ENV_TOKEN!r} "
"environment variable. See "
"https://api.ynab.com/#personal-access-tokens"
)
await sync(token, db, full_refresh)
return 0
def default_db_path() -> Path:
return (
(
Path(xdg_data_home)
if (xdg_data_home := os.environ.get("XDG_DATA_HOME"))
else Path.home() / ".local" / "share"
)
/ _PACKAGE
/ "db.sqlite"
)
async def sync(token: str, db: Path, full_refresh: bool) -> None:
async with aiohttp.ClientSession() as session:
budgets = (await YnabClient(token, session)("budgets"))["budgets"]
budget_ids = [b["id"] for b in budgets]
if not db.exists():
db.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(db) as con:
con.row_factory = _row_factory
cur = con.cursor()
if full_refresh:
print("Dropping relations...")
cur.executescript(contents("drop-relations.sql"))
con.commit()
print("Done")
relations = get_relations(cur)
if relations != _ALL_RELATIONS:
print("Recreating relations...")
cur.executescript(contents("create-relations.sql"))
con.commit()
print("Done")
print("Fetching budget data...")
lkos = get_last_knowledge_of_server(cur)
async with aiohttp.ClientSession() as session:
with tqdm(desc="Budget Data", total=len(budgets) * 5) as pbar:
yc = ProgressYnabClient(YnabClient(token, session), pbar)
account_jobs = jobs(yc, "accounts", budget_ids, lkos)
cat_jobs = jobs(yc, "categories", budget_ids, lkos)
payee_jobs = jobs(yc, "payees", budget_ids, lkos)
txn_jobs = jobs(yc, "transactions", budget_ids, lkos)
sched_txn_jobs = jobs(yc, "scheduled_transactions", budget_ids, lkos)
data = await asyncio.gather(
*account_jobs, *cat_jobs, *payee_jobs, *txn_jobs, *sched_txn_jobs
)
la = len(account_jobs)
lc = len(cat_jobs)
lp = len(payee_jobs)
lt = len(txn_jobs)
all_account_data = data[:la]
all_cat_data = data[la : la + lc]
all_payee_data = data[la + lc : la + lc + lp]
all_txn_data = data[la + lc + lp : la + lc + lp + lt]
all_sched_txn_data = data[la + lc + lp + lt :]
new_lkos = {
bid: t["server_knowledge"]
for bid, t in zip(budget_ids, all_txn_data, strict=True)
}
print("Done")
if (
not any(t["accounts"] for t in all_account_data)
and not any(t["category_groups"] for t in all_cat_data)
and not any(p["payees"] for p in all_payee_data)
and not any(t["transactions"] for t in all_txn_data)
and not any(s["scheduled_transactions"] for s in all_sched_txn_data)
):
print("No new data fetched")
else:
print("Inserting budget data...")
insert_budgets(cur, budgets, new_lkos)
for bid, account_data in zip(budget_ids, all_account_data, strict=True):
insert_accounts(cur, bid, account_data["accounts"])
for bid, cat_data in zip(budget_ids, all_cat_data, strict=True):
insert_category_groups(cur, bid, cat_data["category_groups"])
for bid, payee_data in zip(budget_ids, all_payee_data, strict=True):
insert_payees(cur, bid, payee_data["payees"])
for bid, txn_data in zip(budget_ids, all_txn_data, strict=True):
insert_transactions(cur, bid, txn_data["transactions"])
for bid, sched_txn_data in zip(budget_ids, all_sched_txn_data, strict=True):
insert_scheduled_transactions(
cur, bid, sched_txn_data["scheduled_transactions"]
)
print("Done")
def _row_factory(c: sqlite3.Cursor, row: tuple[Any, ...]) -> dict[str, Any]:
return {d[0]: r for d, r in zip(c.description, row, strict=True)}
def contents(filename: str) -> str:
return (resources.files(ddl) / filename).read_text()
def get_relations(cur: sqlite3.Cursor) -> set[str]:
return {
t["name"]
for t in cur.execute(
"SELECT name FROM sqlite_master WHERE type='table' OR type='view'"
).fetchall()
}
def get_last_knowledge_of_server(cur: sqlite3.Cursor) -> dict[str, int]:
return {
r["id"]: r["last_knowledge_of_server"]
for r in cur.execute(
"SELECT id, last_knowledge_of_server FROM budgets",
).fetchall()
}
def insert_budgets(
cur: sqlite3.Cursor, budgets: list[dict[str, Any]], lkos: dict[str, int]
) -> None:
cur.executemany(
"""
INSERT OR REPLACE INTO budgets (
id
, name
, currency_format_currency_symbol
, currency_format_decimal_digits
, currency_format_decimal_separator
, currency_format_display_symbol
, currency_format_group_separator
, currency_format_iso_code
, currency_format_symbol_first
, last_knowledge_of_server
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
(
bid := b["id"],
b["name"],
b["currency_format"]["currency_symbol"],
b["currency_format"]["decimal_digits"],
b["currency_format"]["decimal_separator"],
b["currency_format"]["display_symbol"],
b["currency_format"]["group_separator"],
b["currency_format"]["iso_code"],
b["currency_format"]["symbol_first"],
lkos[bid],
)
for b in budgets
),
)
_LOAN_ACCOUNT_PERIODIC_VALUES = frozenset(
("debt_escrow_amounts", "debt_interest_rates", "debt_minimum_payments")
)
def insert_accounts(
cur: sqlite3.Cursor, budget_id: str, accounts: list[dict[str, Any]]
) -> None:
# YNAB's LoanAccountPeriodValues are untyped dicts so we need to turn them into a more standard sub-entry view
updated_accounts = [
{
"account_periodic_values": [
{
"name": key,
"account_id": account["id"],
"date": apvk,
"amount": apvv,
}
for key in _LOAN_ACCOUNT_PERIODIC_VALUES
for apvk, apvv in account[key].items()
]
}
| {k: v for k, v in account.items() if k not in _LOAN_ACCOUNT_PERIODIC_VALUES}
for account in accounts
]
return insert_nested_entries(
cur,
budget_id,
updated_accounts,
"Accounts",
"accounts",
"account_periodic_values",
"account_periodic_values",
)
def insert_category_groups(
cur: sqlite3.Cursor, budget_id: str, category_groups: list[dict[str, Any]]
) -> None:
return insert_nested_entries(
cur,
budget_id,
category_groups,
"Categories",
"category_groups",
"categories",
"categories",
)
def insert_payees(
cur: sqlite3.Cursor, budget_id: str, payees: list[dict[str, Any]]
) -> None:
if not payees:
return
for payee in tqdm(payees, desc="Payees"):
insert_entry(cur, "payees", budget_id, payee)
def insert_transactions(
cur: sqlite3.Cursor, budget_id: str, transactions: list[dict[str, Any]]
) -> None:
return insert_nested_entries(
cur,
budget_id,
transactions,
"Transactions",
"transactions",
"subtransactions",
"subtransactions",
)
def insert_scheduled_transactions(
cur: sqlite3.Cursor, budget_id: str, scheduled_transactions: list[dict[str, Any]]
) -> None:
return insert_nested_entries(
cur,
budget_id,
scheduled_transactions,
"Scheduled Transactions",
"scheduled_transactions",
"subtransactions",
"scheduled_subtransactions",
)
@overload
def insert_nested_entries(
cur: sqlite3.Cursor,
budget_id: str,
entries: list[dict[str, Any]],
desc: Literal["Accounts"],
entries_name: Literal["accounts"],
subentries_name: Literal["account_periodic_values"],
subentries_table_name: Literal["account_periodic_values"],
) -> None: ...
@overload
def insert_nested_entries(
cur: sqlite3.Cursor,
budget_id: str,
entries: list[dict[str, Any]],
desc: Literal["Categories"],
entries_name: Literal["category_groups"],
subentries_name: Literal["categories"],
subentries_table_name: Literal["categories"],
) -> None: ...
@overload
def insert_nested_entries(
cur: sqlite3.Cursor,
budget_id: str,
entries: list[dict[str, Any]],
desc: Literal["Transactions"],
entries_name: Literal["transactions"],
subentries_name: Literal["subtransactions"],
subentries_table_name: Literal["subtransactions"],
) -> None: ...
@overload
def insert_nested_entries(
cur: sqlite3.Cursor,
budget_id: str,
entries: list[dict[str, Any]],
desc: Literal["Scheduled Transactions"],
entries_name: Literal["scheduled_transactions"],
subentries_name: Literal["subtransactions"],
subentries_table_name: Literal["scheduled_subtransactions"],
) -> None: ...
def insert_nested_entries(
cur: sqlite3.Cursor,
budget_id: str,
entries: list[dict[str, Any]],
desc: (
Literal["Accounts"]
| Literal["Categories"]
| Literal["Transactions"]
| Literal["Scheduled Transactions"]
),
entries_name: (
Literal["accounts"]
| Literal["category_groups"]
| Literal["transactions"]
| Literal["scheduled_transactions"]
),
subentries_name: (
Literal["account_periodic_values"]
| Literal["categories"]
| Literal["subtransactions"]
),
subentries_table_name: (
Literal["account_periodic_values"]
| Literal["categories"]
| Literal["subtransactions"]
| Literal["scheduled_subtransactions"]
),
) -> None:
if not entries:
return
with tqdm(
total=sum(1 + len(e[subentries_name]) for e in entries),
desc=desc,
) as pbar:
for entry in entries:
insert_entry(
cur,
entries_name,
budget_id,
{k: v for k, v in entry.items() if k != subentries_name},
)
pbar.update()
for subentry in entry[subentries_name]:
insert_entry(cur, subentries_table_name, budget_id, subentry)
pbar.update()
def insert_entry(
cur: sqlite3.Cursor,
table: _EntryTable,
budget_id: str,
entry: dict[str, Any],
) -> None:
ekeys, evalues = zip(*entry.items(), strict=True)
keys, values = ekeys + ("budget_id",), evalues + (budget_id,)
cur.execute(
f"INSERT OR REPLACE INTO {table} ({', '.join(keys)}) VALUES ({', '.join('?' * len(values))})",
values,
)
def jobs(
yc: SupportsYnabClient,
endpoint: (
Literal["accounts"]
| Literal["categories"]
| Literal["payees"]
| Literal["transactions"]
| Literal["scheduled_transactions"]
),
budget_ids: list[str],
lkos: dict[str, int],
) -> list[Awaitable[dict[str, Any]]]:
return [
yc(f"budgets/{bid}/{endpoint}", last_knowledge_of_server=lkos.get(bid))
for bid in budget_ids
]
class SupportsYnabClient(Protocol):
async def __call__(
self, path: str, last_knowledge_of_server: int | None = None
) -> dict[str, Any]: ...
@dataclass
class ProgressYnabClient:
yc: YnabClient
pbar: tqdm[Never]
async def __call__(
self, path: str, last_knowledge_of_server: int | None = None
) -> dict[str, Any]:
try:
return await self.yc(path, last_knowledge_of_server)
finally:
self.pbar.update()
@dataclass
class YnabClient:
BASE_SCHEME: ClassVar[str] = "https"
BASE_NETLOC: ClassVar[str] = "api.ynab.com"
BASE_PATH: ClassVar[str] = "v1/"
token: str
session: aiohttp.ClientSession
async def __call__(
self, path: str, last_knowledge_of_server: int | None = None
) -> dict[str, Any]:
headers = {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json",
}
url = urlunparse(
(
self.BASE_SCHEME,
self.BASE_NETLOC,
urljoin(self.BASE_PATH, path),
"",
urlencode(
{"last_knowledge_of_server": last_knowledge_of_server}
if last_knowledge_of_server
else {}
),
"",
)
)
for i in range(3):
try:
async with self.session.get(url, headers=headers) as resp:
body = await resp.text()
return json.loads(body)["data"]
except Exception:
if i == 2:
raise
raise AssertionError("unreachable")
def main(argv: Sequence[str] | None = None) -> int:
return asyncio.run(async_main(argv))