-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger_ops.py
165 lines (141 loc) · 4.78 KB
/
ledger_ops.py
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
import re
import subprocess
from dataclasses import dataclass
from datetime import datetime
from typing import Optional
from dateutil import parser
import common
@dataclass
class LedgerEntry:
date: str
payee: str
body: list[str]
def header(self) -> str:
return f"{self.date} {self.payee}"
def parse_date(self) -> datetime:
return parser.parse(self.date)
def full_list(self) -> list[str]:
lines = [self.header()]
lines.extend(self.body)
return lines
def full_str(self) -> str:
return "\n".join(self.full_list())
def validate(self) -> tuple[bool, str]:
ledger_cmd = f"{common.LEDGER_BIN} -f -"
try:
output = subprocess.check_output(
ledger_cmd,
input=self.full_str(),
shell=True,
text=True,
stderr=subprocess.PIPE,
)
return True, output
except subprocess.CalledProcessError as e:
return False, e.stderr
def write(self) -> tuple[bool, list[str]]:
logs = []
validated, output = self.validate()
if not validated:
logs.append(output)
return False, logs
new_entry_date = self.parse_date()
logs.append(f"New entry date: {new_entry_date}")
with open(f"{common.LEDGER_DAT}", "r") as f:
contents = f.readlines()
needs_insert = False
for i, text in enumerate(contents):
if text and text[0].isdigit():
entry_date = parser.parse(text.split()[0])
if entry_date > new_entry_date:
needs_insert = True
break
if needs_insert:
logs.append(f"Insert at line {i}")
contents.insert(i, self.full_str() + "\n")
with open(f"{common.LEDGER_DAT}", "w") as f:
f.write("".join(contents))
else:
logs.append("Appending to end of ledger")
with open(f"{common.LEDGER_DAT}", "a") as f:
f.write(self.full_str() + "\n")
return True, logs
def modify_ledger(
entry: LedgerEntry, date_str: str, price: str
) -> Optional[LedgerEntry]:
output_lines = entry.full_list()
price = price.upper()
if len(price):
try:
float(price)
except ValueError:
pass
else:
price = f"${price}"
new_output = []
found = False
for line in output_lines:
if not found and any(x in line for x in ["Expenses:", "Liabilities:"]):
old_price = re.split(r"\s{2,}", line)[-1]
new_output.append(line.replace(old_price, price))
found = True
else:
new_output.append(line)
output_lines = new_output
output_lines = [x for x in output_lines if x]
if (new_entry := parse_ledger_entry(output_lines)) is not None:
if date_str:
new_entry.date = date_str
return new_entry
return None
def parse_ledger_entry(lines: list[str]) -> Optional[LedgerEntry]:
try:
date = lines[0].split()[0]
payee = lines[0].split(maxsplit=1)[1]
return LedgerEntry(date=date, payee=payee, body=lines[1:])
except IndexError:
pass
return None
def parse_ledger_output(output: str) -> list[LedgerEntry]:
entries = []
for entry in output.split("\n\n"):
lines = [x for x in entry.split("\n") if x]
if ledger := parse_ledger_entry(lines):
entries.append(ledger)
return entries
def make_ledger_command(
payee: Optional[str] = None,
commodity: Optional[str] = None,
search: Optional[str] = None,
) -> str:
args = []
if payee:
args.append(f"payee '{payee}'")
if search:
if not payee:
args.append(f"'{search}'")
else:
args.append(f"and '{search}'")
if commodity:
args.append(f"--limit 'commodity=~/{commodity}/'")
ledger_prefix = common.LEDGER_PREFIX.replace("-c ", "")
return f"{ledger_prefix} --tail 10 print {' '.join(args)}"
def get_ledger_entries(
payee: Optional[str] = None,
commodity: Optional[str] = None,
search: Optional[str] = None,
) -> list[LedgerEntry]:
ledger_cmd = make_ledger_command(payee, commodity, search)
return get_ledger_entries_from_command(ledger_cmd)
def get_ledger_entries_from_command(command: str) -> list[LedgerEntry]:
return parse_ledger_output(subprocess.check_output(command, shell=True, text=True))
def get_ledger_balance(command):
"""Get account balance from ledger."""
try:
return float(
subprocess.check_output(
f"{command} | tail -1", shell=True, text=True
).split()[1]
)
except IndexError:
return 0