Repository navigation
Expand file tree
/
Copy pathbeanpost-export.py
More file actions
executable file
·287 lines (241 loc) · 7.11 KB
/
Copy pathbeanpost-export.py
File metadata and controls
executable file
·287 lines (241 loc) · 7.11 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
#!/usr/bin/env python
import logging
from argparse import ArgumentParser
from pathlib import Path
import psycopg2 as dbapi
import psycopg2.extras
from beancount.utils import misc_utils
from psycopg2.extensions import parse_dsn
def export_accounts(cursor, file):
# Execute the SQL query
cursor.execute(
"""
SELECT
name,
open_date,
close_date,
currencies,
meta
FROM
account
ORDER BY
account.id
"""
)
# Fetch all rows from the cursor
rows = cursor.fetchall()
# Iterate over each row
for row in rows:
open_date = row["open_date"]
name = row["name"]
# Handle None case by defaulting to an empty list if row["currencies"] is None
currencies = ",".join(row["currencies"] or [])
# Write the open_date to the file followed by a newline
file.write(f"{open_date} open {name} {currencies}\n")
export_meta(file, row["meta"])
if close_date := row["close_date"]:
file.write(f"{close_date} close {name}\n\n")
def export_transactions(cursor, file):
cursor.execute(
"""
SELECT
transaction.id,
transaction.flag,
transaction.payee,
transaction.narration,
transaction.tags,
transaction.links,
posting.date,
posting.amount,
posting.cost,
posting.cost_date,
posting.cost_label,
posting.price,
account.name
FROM
TRANSACTION
JOIN posting ON posting.transaction_id = transaction.id
JOIN account ON account.id = posting.account_id
ORDER BY
transaction.id
"""
)
rows = cursor.fetchall()
transaction_id = None
# Iterate over each row
for row in rows:
if transaction_id != row["id"]:
transaction_id = row["id"]
date = row["date"]
flag = row["flag"] or ""
payee = row["payee"] or ""
narration = row["narration"] or ""
file.write(f'\n{date} {flag} "{payee}" "{narration}"\n')
account = row["name"]
number, currency = parse_amount(row["amount"])
amount = f"{number} {currency}"
if cost := row["cost"]:
number, currency = parse_amount(cost)
cost_date = row["cost_date"]
cost_label = row["cost_label"]
if cost_label:
amount += f' {{{cost_date}, {number} {currency}, "{cost_label}"}}'
else:
amount += f" {{{cost_date}, {number} {currency}}}"
if price := row["price"]:
number, currency = parse_amount(price)
amount += f" @ {number} {currency}"
file.write(f" {account} {amount}\n")
def export_balances(cursor, file):
# Execute the SQL query
cursor.execute(
"""
SELECT
assertion.date,
assertion.amount,
account.name
FROM
assertion
JOIN account ON account.id = assertion.account_id
ORDER BY
assertion.id
"""
)
# Fetch all rows from the cursor
rows = cursor.fetchall()
# Iterate over each row
for row in rows:
date = row["date"]
account = row["name"]
number, currency = parse_amount(row["amount"])
file.write(f"{date} balance {account} {number} {currency}\n\n")
def export_prices(cursor, file):
# Execute the SQL query
cursor.execute(
"""
SELECT
date, currency, amount
FROM
price
ORDER BY
price.id
"""
)
# Fetch all rows from the cursor
rows = cursor.fetchall()
# Iterate over each row
for row in rows:
date = row["date"]
base_currency = row["currency"]
number, currency = parse_amount(row["amount"])
file.write(f"{date} price {base_currency} {number} {currency}\n\n")
def export_commodities(cursor, file):
# Execute the SQL query
cursor.execute(
"""
SELECT
date, currency, decimal_places, meta
FROM
commodity
ORDER BY
commodity.id
"""
)
# Fetch all rows from the cursor
rows = cursor.fetchall()
# Iterate over each row
for row in rows:
date = row["date"]
currency = row["currency"]
decimal_places = row["decimal_places"]
meta = row["meta"]
meta["decimal_places"] = decimal_places
file.write(f"{date} commodity {currency}\n")
export_meta(file, meta)
def export_documents(cursor, file):
documents_path = Path(file.name).parent / "documents"
# Selecting without fetching document.data initially
cursor.execute(
"""
SELECT
document.date,
document.filename,
document.id,
account.name
FROM
document
JOIN account ON account.id = document.account_id
ORDER BY
document.id
"""
)
# Fetch all rows from the cursor
rows = cursor.fetchall()
# Iterate over each row
for row in rows:
relative_path = Path(row["filename"])
final_path = documents_path / relative_path
# Check if the file already exists
if not final_path.exists():
# Fetch the document data only if the file does not exist
cursor.execute(
"""
SELECT document.data
FROM document
WHERE document.id = %s;
""",
(row["id"],),
)
data = cursor.fetchone()["data"]
# Ensure the directory for this file exists
final_path.parent.mkdir(parents=True, exist_ok=True)
# Write bytea data in data column to final_path
with final_path.open("wb") as doc_file:
doc_file.write(data)
def export_insert_entry(cursor, file):
file.write('2020-01-01 custom "fava-option" "insert-entry" ".*"')
def parse_amount(amount):
stripped_string = amount.strip("()")
number, currency = stripped_string.split(",")
return number, currency
def export_meta(file, meta):
for k, v in meta.items():
# Check if the value is of boolean type
if isinstance(v, bool):
# Convert boolean to uppercase string
value_str = "TRUE" if v else "FALSE"
# Check if the value is a string
elif isinstance(v, str):
# Surround the string with quotes
value_str = f'"{v}"'
else:
value_str = v
file.write(f" {k}: {value_str}\n")
def main():
parser = ArgumentParser(description=__doc__)
parser.add_argument("database", help="PostgreSQL connection string")
parser.add_argument("filename", help="Beancount filename")
args = parser.parse_args()
logging.basicConfig(level=logging.INFO, format="%(levelname)-8s: %(message)s")
dsn = parse_dsn(args.database)
connection = dbapi.connect(**dsn)
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
absolute_file_path = Path(args.filename).resolve()
with absolute_file_path.open("w") as file:
for function in [
export_accounts,
export_transactions,
export_balances,
export_prices,
export_commodities,
export_documents,
export_insert_entry,
]:
step_name = getattr(function, "__name__", function.__class__.__name__)
with misc_utils.log_time(step_name, logging.info):
function(cursor, file)
connection.commit()
cursor.close()
connection.close()
if __name__ == "__main__":
main()