Skip to content

Commit 94770be

Browse files
committed
Decorator refactoring
1 parent 09b2ed9 commit 94770be

File tree

7 files changed

+71
-69
lines changed

7 files changed

+71
-69
lines changed

src/beancount_multitool/JABank.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,6 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
104104
else: # a credit
105105
accounts = self.credit_db.match(memo)
106106

107-
# Add UUID for manual transactions reconcilation between accounts
108-
if "#reconcile" in accounts[0]["tags"]:
109-
if amount > 0: # a credit
110-
metadata["uuid"] = ""
111-
else: # a debit
112-
if hasattr(sys, "_called_from_pytest"):
113-
# remove randomness during pytest
114-
metadata["uuid"] = "_called_from_pytest"
115-
else:
116-
metadata["uuid"] = str(uuid.uuid4())
117-
118107
account_metadata = {}
119108
for x in range(1, len(accounts)):
120109
account_metadata[f"match{x+1}"] = str(accounts[x])

src/beancount_multitool/RakutenBank.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,6 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
8989
else: # a credit
9090
accounts = self.credit_db.match(memo)
9191

92-
# Add UUID for manual transactions reconcilation between accounts
93-
if "#reconcile" in accounts[0]["tags"]:
94-
if amount > 0: # a credit
95-
metadata["uuid"] = ""
96-
else: # a debit
97-
if hasattr(sys, "_called_from_pytest"):
98-
# remove randomness during pytest
99-
metadata["uuid"] = "_called_from_pytest"
100-
else:
101-
metadata["uuid"] = str(uuid.uuid4())
102-
10392
account_metadata = {}
10493
for x in range(1, len(accounts)):
10594
account_metadata[f"match{x+1}"] = str(accounts[x])

src/beancount_multitool/ShinseiBank.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,6 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
108108
else: # a credit
109109
accounts = self.credit_db.match(memo)
110110

111-
# Add UUID for manual transactions reconcilation between accounts
112-
if "#reconcile" in accounts[0]["tags"]:
113-
if amount > 0: # a credit
114-
metadata["uuid"] = ""
115-
else: # a debit
116-
if hasattr(sys, "_called_from_pytest"):
117-
# remove randomness during pytest
118-
metadata["uuid"] = "_called_from_pytest"
119-
else:
120-
metadata["uuid"] = str(uuid.uuid4())
121-
122111
account_metadata = {}
123112
for x in range(1, len(accounts)):
124113
account_metadata[f"match{x+1}"] = str(accounts[x])

src/beancount_multitool/SumishinNetBank.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,6 @@ def write_bean(self, df: pd.DataFrame, file_name: str) -> None:
109109
else: # a credit
110110
accounts = self.credit_db.match(memo)
111111

112-
# Add UUID for manual transactions reconcilation between accounts
113-
if "#reconcile" in accounts[0]["tags"]:
114-
if amount > 0: # a credit
115-
metadata["uuid"] = ""
116-
else: # a debit
117-
if hasattr(sys, "_called_from_pytest"):
118-
# remove randomness during pytest
119-
metadata["uuid"] = "_called_from_pytest"
120-
else:
121-
metadata["uuid"] = str(uuid.uuid4())
122-
123112
account_metadata = {}
124113
for x in range(1, len(accounts)):
125114
account_metadata[f"match{x+1}"] = str(accounts[x])

src/beancount_multitool/as_transaction.py

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,63 @@
1-
import datetime
2-
import decimal
1+
from datetime import datetime
2+
from decimal import Decimal
3+
import sys
4+
import uuid
35

46

7+
def make_hashtags(func):
8+
"""Add # prefix if missing from any tags.
9+
"""
10+
def add_hash(*args, **kwargs):
11+
12+
hashtags = []
13+
for x in kwargs["tags"]:
14+
if len(x):
15+
if x.startswith("#"):
16+
hashtags.append(x)
17+
else:
18+
hashtags.append("#" + x)
19+
kwargs["tags"] = hashtags
20+
21+
result = func(*args, **kwargs)
22+
return result
23+
return add_hash
24+
25+
26+
def reconcile(func):
27+
"""Add an UUID field if #reconcile tag exists
28+
"""
29+
def add_uuid(*args, **kwargs):
30+
31+
# Add UUID for manual transactions reconcilation between accounts
32+
if "#reconcile" in kwargs["tags"]:
33+
if kwargs["amount"] < 0: # a credit
34+
kwargs["metadata"]["uuid"] = ""
35+
else: # a debit
36+
if hasattr(sys, "_called_from_pytest"):
37+
# remove randomness during pytest
38+
kwargs["metadata"]["uuid"] = "_called_from_pytest"
39+
else:
40+
kwargs["metadata"]["uuid"] = str(uuid.uuid4())
41+
42+
result = func(*args, **kwargs)
43+
return result
44+
return add_uuid
45+
46+
47+
@reconcile
48+
@make_hashtags
549
def as_transaction(
6-
date: datetime.datetime,
7-
payee: str,
8-
narration: str,
9-
tags: list[str],
10-
source_account: str,
11-
account: str,
12-
amount: decimal.Decimal,
13-
currency: str,
14-
flag: str,
15-
metadata: dict,
16-
account_metadata: dict,
50+
date: datetime | None = None,
51+
payee: str = "",
52+
narration: str = "",
53+
tags: list[str] | None = None,
54+
source_account: str = "",
55+
account: str = "",
56+
amount: Decimal = Decimal(0),
57+
currency: str = "",
58+
flag: str = "",
59+
metadata: dict | None = None,
60+
account_metadata: dict | None = None,
1761
**kwargs,
1862
) -> str:
1963
"""
@@ -32,16 +76,18 @@ def as_transaction(
3276
for key, value in {account_metadata}:
3377
{key}: "{value}"
3478
"""
35-
# Add "#" prefix if does not exist
36-
hashtags = []
37-
for x in tags:
38-
if len(x):
39-
if x.startswith("#"):
40-
hashtags.append(x)
41-
else:
42-
hashtags.append("#" + x)
43-
if len(hashtags):
44-
tags_str = " " + " ".join(hashtags)
79+
# Initialize None. Ref: https://docs.astral.sh/ruff/rules/mutable-argument-default/
80+
if date is None:
81+
date = datetime.today()
82+
if tags is None:
83+
tags = []
84+
if metadata is None:
85+
metadata = {}
86+
if account_metadata is None:
87+
account_metadata = {}
88+
89+
if len(tags):
90+
tags_str = " " + " ".join(tags)
4591
else:
4692
tags_str = ""
4793

tests/data2/ja_bank/debit_mapping.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ regex = '出金NSS'
2121
account = "Expenses:Education:Books"
2222
payee = "Bookstore"
2323
narration = ""
24-
tags = []
24+
tags = ["nonfiction"]
2525
flag = ""
2626

2727
[[mapping]]

tests/data2/ja_bank/test.bean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Assets:JP:JABank
66
! Assets:JP:Wallet -7000 JPY
77

8-
2024-04-30 * "Bookstore" ""
8+
2024-04-30 * "Bookstore" "" #nonfiction
99
memo: "出金NSS"
1010
Assets:JP:JABank
1111
Expenses:Education:Books 4600 JPY

0 commit comments

Comments
 (0)