manager-for-ynab 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (26) hide show
  1. manager_for_ynab-1.0.0/LICENSE +21 -0
  2. manager_for_ynab-1.0.0/PKG-INFO +58 -0
  3. manager_for_ynab-1.0.0/README.md +34 -0
  4. manager_for_ynab-1.0.0/manager_for_ynab/__init__.py +3 -0
  5. manager_for_ynab-1.0.0/manager_for_ynab/__main__.py +4 -0
  6. manager_for_ynab-1.0.0/manager_for_ynab/_auth.py +17 -0
  7. manager_for_ynab-1.0.0/manager_for_ynab/_main.py +71 -0
  8. manager_for_ynab-1.0.0/manager_for_ynab/_version.py +13 -0
  9. manager_for_ynab-1.0.0/manager_for_ynab/pending_income/__init__.py +141 -0
  10. manager_for_ynab-1.0.0/manager_for_ynab/pending_income/pending_income.sql +24 -0
  11. manager_for_ynab-1.0.0/manager_for_ynab/py.typed +0 -0
  12. manager_for_ynab-1.0.0/manager_for_ynab/reconciler/__init__.py +433 -0
  13. manager_for_ynab-1.0.0/manager_for_ynab/zero_out/__init__.py +213 -0
  14. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/PKG-INFO +58 -0
  15. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/SOURCES.txt +25 -0
  16. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/dependency_links.txt +1 -0
  17. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/entry_points.txt +2 -0
  18. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/requires.txt +6 -0
  19. manager_for_ynab-1.0.0/manager_for_ynab.egg-info/top_level.txt +3 -0
  20. manager_for_ynab-1.0.0/pyproject.toml +65 -0
  21. manager_for_ynab-1.0.0/setup.cfg +63 -0
  22. manager_for_ynab-1.0.0/setup.py +3 -0
  23. manager_for_ynab-1.0.0/testing/__init__.py +0 -0
  24. manager_for_ynab-1.0.0/testing/fixtures.py +31 -0
  25. manager_for_ynab-1.0.0/testing/seed.sql +289 -0
  26. manager_for_ynab-1.0.0/tests/__init__.py +0 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Max R
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.4
2
+ Name: manager_for_ynab
3
+ Version: 1.0.0
4
+ Summary: Manager for YNAB
5
+ Home-page: https://github.com/mxr/manager-for-ynab
6
+ Author: Max R
7
+ Author-email: mxr@users.noreply.github.com
8
+ License: MIT
9
+ Keywords: ynab,budget,plan,cli,reconcile,automation,manager
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: Implementation :: CPython
13
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
14
+ Requires-Python: >=3.14
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: Babel
18
+ Requires-Dist: aiohttp
19
+ Requires-Dist: rich
20
+ Requires-Dist: sqlite-export-for-ynab>=2
21
+ Requires-Dist: tldm
22
+ Requires-Dist: ynab>=4
23
+ Dynamic: license-file
24
+
25
+ # manager-for-ynab
26
+
27
+ [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/mxr/manager-for-ynab/main.svg)](https://results.pre-commit.ci/latest/github/mxr/manager-for-ynab/main)
28
+
29
+ Manager for YNAB.
30
+
31
+ ## What This Does
32
+
33
+ This repo is a single CLI for YNAB-focused tools.
34
+
35
+ - `reconciler`: find and automatically reconciles unreconciled transactions
36
+ - `pending-income`: move pending income transactions to today
37
+ - `zero-out`: set a category's planned amount to zero across a month range
38
+
39
+ Tool-specific docs:
40
+
41
+ - [Reconciler](tools/reconciler/README.md)
42
+ - [Pending Income](tools/pending-income/README.md)
43
+ - [Zero Out](tools/zero-out/README.md)
44
+
45
+ ## Installation
46
+
47
+ ```console
48
+ $ pip install manager-for-ynab
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ ```console
54
+ $ manager-for-ynab --help
55
+ $ manager-for-ynab reconciler --help
56
+ $ manager-for-ynab pending-income --help
57
+ $ manager-for-ynab zero-out --help
58
+ ```
@@ -0,0 +1,34 @@
1
+ # manager-for-ynab
2
+
3
+ [![pre-commit.ci status](https://results.pre-commit.ci/badge/github/mxr/manager-for-ynab/main.svg)](https://results.pre-commit.ci/latest/github/mxr/manager-for-ynab/main)
4
+
5
+ Manager for YNAB.
6
+
7
+ ## What This Does
8
+
9
+ This repo is a single CLI for YNAB-focused tools.
10
+
11
+ - `reconciler`: find and automatically reconciles unreconciled transactions
12
+ - `pending-income`: move pending income transactions to today
13
+ - `zero-out`: set a category's planned amount to zero across a month range
14
+
15
+ Tool-specific docs:
16
+
17
+ - [Reconciler](tools/reconciler/README.md)
18
+ - [Pending Income](tools/pending-income/README.md)
19
+ - [Zero Out](tools/zero-out/README.md)
20
+
21
+ ## Installation
22
+
23
+ ```console
24
+ $ pip install manager-for-ynab
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```console
30
+ $ manager-for-ynab --help
31
+ $ manager-for-ynab reconciler --help
32
+ $ manager-for-ynab pending-income --help
33
+ $ manager-for-ynab zero-out --help
34
+ ```
@@ -0,0 +1,3 @@
1
+ from manager_for_ynab._main import main
2
+
3
+ __all__ = [main.__name__]
@@ -0,0 +1,4 @@
1
+ from manager_for_ynab._main import main
2
+
3
+ if __name__ == "__main__":
4
+ raise SystemExit(main())
@@ -0,0 +1,17 @@
1
+ import os
2
+
3
+
4
+ _ENV_TOKEN = "YNAB_PERSONAL_ACCESS_TOKEN"
5
+
6
+
7
+ def resolve_token(token_override: str | None = None) -> str:
8
+ token = token_override or os.environ.get(_ENV_TOKEN)
9
+ if token:
10
+ return token
11
+
12
+ raise ValueError(
13
+ f"Must set YNAB access token as {_ENV_TOKEN!r} environment variable or pass token_override directly. See https://api.ynab.com/#personal-access-tokens"
14
+ )
15
+
16
+
17
+ __all__ = [resolve_token.__name__, _ENV_TOKEN]
@@ -0,0 +1,71 @@
1
+ import argparse
2
+ import sys
3
+ from typing import TYPE_CHECKING
4
+
5
+ import manager_for_ynab.pending_income as pending_income
6
+ import manager_for_ynab.reconciler as reconciler
7
+ import manager_for_ynab.zero_out as zero_out
8
+ from manager_for_ynab._version import get_version
9
+
10
+ if TYPE_CHECKING:
11
+ from collections.abc import Sequence
12
+
13
+
14
+ _RECONCILER_HELP = "Find and automatically reconciles unreconciled transactions."
15
+
16
+
17
+ def build_parser() -> argparse.ArgumentParser:
18
+ parser = argparse.ArgumentParser(prog="manager-for-ynab")
19
+ parser.add_argument(
20
+ "--version", action="version", version=f"%(prog)s {get_version()}"
21
+ )
22
+ subparsers = parser.add_subparsers(dest="command", required=True)
23
+
24
+ reconciler_parser = subparsers.add_parser(
25
+ "reconciler",
26
+ help=_RECONCILER_HELP,
27
+ description=_RECONCILER_HELP,
28
+ )
29
+ reconciler_parser.set_defaults(func=_run_reconciler)
30
+
31
+ pending_income_parser = subparsers.add_parser(
32
+ "pending-income", help="Move pending income transactions to today."
33
+ )
34
+ pending_income_parser.set_defaults(func=_run_pending_income)
35
+
36
+ zero_out_parser = subparsers.add_parser(
37
+ "zero-out",
38
+ help="Set a category's budgeted amount to zero across a month range.",
39
+ )
40
+ zero_out_parser.set_defaults(func=_run_zero_out)
41
+ return parser
42
+
43
+
44
+ def _run_reconciler(argv: Sequence[str]) -> int:
45
+ return reconciler.run(argv)
46
+
47
+
48
+ def _run_pending_income(argv: Sequence[str]) -> int:
49
+ return pending_income.run(argv)
50
+
51
+
52
+ def _run_zero_out(argv: Sequence[str]) -> int:
53
+ return zero_out.run(argv)
54
+
55
+
56
+ def main(argv: Sequence[str] = ()) -> int:
57
+ argv = argv or sys.argv[1:]
58
+ if not argv:
59
+ build_parser().print_help()
60
+ return 0
61
+ match argv[0]:
62
+ case "reconciler":
63
+ return _run_reconciler(argv[1:])
64
+ case "pending-income":
65
+ return _run_pending_income(argv[1:])
66
+ case "zero-out":
67
+ return _run_zero_out(argv[1:])
68
+
69
+ parser = build_parser()
70
+ parser.parse_args(argv)
71
+ raise AssertionError("subcommand parser should have exited")
@@ -0,0 +1,13 @@
1
+ from configparser import ConfigParser
2
+ from importlib.metadata import PackageNotFoundError
3
+ from importlib.metadata import version
4
+ from pathlib import Path
5
+
6
+
7
+ def get_version(distribution: str = "manager_for_ynab") -> str:
8
+ try:
9
+ return version(distribution)
10
+ except PackageNotFoundError:
11
+ config = ConfigParser()
12
+ config.read(Path(__file__).resolve().parent.parent / "setup.cfg")
13
+ return config["metadata"]["version"]
@@ -0,0 +1,141 @@
1
+ import argparse
2
+ import asyncio
3
+ import sqlite3
4
+ from collections import defaultdict
5
+ from dataclasses import dataclass
6
+ from datetime import date
7
+ from importlib.resources import files
8
+ from pathlib import Path
9
+ from typing import Never
10
+ from typing import TYPE_CHECKING
11
+
12
+ import rich
13
+ import ynab
14
+ from rich.table import Table
15
+ from sqlite_export_for_ynab import default_db_path
16
+ from sqlite_export_for_ynab import sync
17
+ from tldm import tldm
18
+
19
+ from manager_for_ynab._auth import resolve_token
20
+
21
+ if TYPE_CHECKING:
22
+ from collections.abc import Sequence
23
+
24
+
25
+ _PACKAGE = "manager-for-ynab pending-income"
26
+ _PENDING_INCOME_SQL = (
27
+ files("manager_for_ynab.pending_income").joinpath("pending_income.sql").read_text()
28
+ )
29
+
30
+
31
+ @dataclass(frozen=True)
32
+ class Transaction:
33
+ id: str
34
+ plan_id: str
35
+ account_name: str
36
+ payee_name: str
37
+ amount_formatted: str
38
+ date: str
39
+
40
+
41
+ def build_parser() -> argparse.ArgumentParser:
42
+ parser = argparse.ArgumentParser(prog=_PACKAGE)
43
+ parser.add_argument(
44
+ "--sqlite-export-for-ynab-db", type=Path, default=default_db_path()
45
+ )
46
+ parser.add_argument("--sqlite-export-for-ynab-full-refresh", action="store_true")
47
+ parser.add_argument("--for-real", action="store_true")
48
+ return parser
49
+
50
+
51
+ def run(argv: Sequence[str] | None = None, *, token_override: str | None = None) -> int:
52
+ args = build_parser().parse_args(argv)
53
+ db: Path = args.sqlite_export_for_ynab_db
54
+ full_refresh: bool = args.sqlite_export_for_ynab_full_refresh
55
+ for_real: bool = args.for_real
56
+
57
+ token = resolve_token(token_override)
58
+
59
+ print("** Refreshing SQLite DB **")
60
+ asyncio.run(sync(token, db, full_refresh))
61
+ print("** Done **")
62
+
63
+ with sqlite3.connect(db) as con:
64
+ con.row_factory = sqlite3.Row
65
+ txns_by_plan = fetch_pending_income(con.cursor())
66
+
67
+ total_txns = sum(len(txns) for txns in txns_by_plan.values())
68
+ print(f"Found {total_txns} income transaction(s) to update.")
69
+ if total_txns == 0:
70
+ return 0
71
+
72
+ print_found_txns([txn for txns in txns_by_plan.values() for txn in txns])
73
+
74
+ grouped = build_updates(txns_by_plan, date.today())
75
+
76
+ if not for_real:
77
+ print("Use --for-real to actually update transactions.")
78
+ return 0
79
+
80
+ api_client = ynab.TransactionsApi(
81
+ ynab.ApiClient(ynab.Configuration(access_token=token))
82
+ )
83
+
84
+ with tldm[Never](
85
+ total=total_txns, desc=f"Updating {total_txns} transaction(s)"
86
+ ) as progress:
87
+ for plan_id, txns in grouped.items():
88
+ api_client.update_transactions(
89
+ plan_id, ynab.PatchTransactionsWrapper(transactions=txns)
90
+ )
91
+ progress.update(len(txns))
92
+
93
+ return 0
94
+
95
+
96
+ def build_updates(
97
+ txns_by_plan: dict[str, list[Transaction]], today: date
98
+ ) -> dict[str, list[ynab.SaveTransactionWithIdOrImportId]]:
99
+ grouped: dict[str, list[ynab.SaveTransactionWithIdOrImportId]] = defaultdict(list)
100
+ for plan_id, txns in txns_by_plan.items():
101
+ grouped[plan_id].extend(
102
+ ynab.SaveTransactionWithIdOrImportId(id=txn.id, date=today) for txn in txns
103
+ )
104
+ return grouped
105
+
106
+
107
+ def fetch_pending_income(cur: sqlite3.Cursor) -> dict[str, list[Transaction]]:
108
+ txns = cur.execute(_PENDING_INCOME_SQL).fetchall()
109
+
110
+ txns_by_plan: dict[str, list[Transaction]] = defaultdict(list)
111
+ for txn in txns:
112
+ txns_by_plan[txn["plan_id"]].append(
113
+ Transaction(
114
+ id=txn["id"],
115
+ plan_id=txn["plan_id"],
116
+ account_name=txn["account_name"],
117
+ payee_name=txn["payee_name"],
118
+ amount_formatted=txn["amount_formatted"],
119
+ date=txn["date"],
120
+ )
121
+ )
122
+
123
+ return txns_by_plan
124
+
125
+
126
+ def print_found_txns(found_txns: list[Transaction]) -> None:
127
+ table = Table(title="Pending Income Transactions")
128
+ table.add_column("Date")
129
+ table.add_column("Account")
130
+ table.add_column("Payee")
131
+ table.add_column("Amount", justify="right")
132
+
133
+ for txn in found_txns:
134
+ table.add_row(
135
+ txn.date, txn.account_name, txn.payee_name or "", txn.amount_formatted
136
+ )
137
+
138
+ rich.print(table)
139
+
140
+
141
+ __all__ = [run.__name__]
@@ -0,0 +1,24 @@
1
+ SELECT
2
+ transactions.id
3
+ , transactions.plan_id
4
+ , transactions.account_name
5
+ , transactions.payee_name
6
+ , transactions.amount_formatted
7
+ , transactions."date"
8
+ FROM transactions
9
+ WHERE
10
+ TRUE
11
+ AND transactions.cleared = 'uncleared'
12
+ AND transactions."date" < DATE('now', 'localtime')
13
+ AND transactions.amount > 0
14
+ AND NOT transactions.deleted
15
+ AND SUBSTR(transactions."date", 6, 2) = SUBSTR(DATE(), 6, 2)
16
+ AND transactions.id NOT IN (
17
+ SELECT subtransactions.transfer_transaction_id
18
+ FROM subtransactions
19
+ WHERE
20
+ subtransactions.transfer_transaction_id IS NOT NULL
21
+ AND NOT subtransactions.deleted
22
+ )
23
+ ORDER BY transactions."date", transactions.account_name, transactions.payee_name
24
+ ;
File without changes