-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathedit.py
executable file
·59 lines (54 loc) · 2.23 KB
/
edit.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
#!/usr/bin/env python
#
# Tool to change gnucash files. Currently this can change all transactions
# involving 1 account to another account.
import codecs
import gnucash
import sys
from gnucashutil import full_acc_name
from sys import stderr, exit
out = codecs.getwriter('UTF-8')(sys.stdout)
if len(sys.argv) < 3:
stderr.write("Invocation: %s gnucash_filename COMMAND\n" % sys.argv[0])
stderr.write(" Commands:\n")
stderr.write(" accountlist List account names+numbers\n")
stderr.write(" switchacc old new "
"Change all transactions from old to new account guid")
exit(1)
conn = gnucash.open_file(sys.argv[1])
data = gnucash.read_data(conn)
if sys.argv[2] == "accountlist":
for account in data.accounts.values():
if account.type is None or account.type == "ROOT":
continue
if str(account.commodity) == "template":
continue
out.write("%s - %s\n" % (account.guid, full_acc_name(account, 3)))
elif sys.argv[2] == "switchacc":
# TODO: Introduce some syntax to only select a subset of transactions
# (or splits)
# In a list of transactions switch account #1 to account #2
fromguid = sys.argv[3]
toguid = sys.argv[4]
fromaccount = data.accounts.get(fromguid)
toaccount = data.accounts.get(toguid)
if fromaccount is None:
stderr.write("There is no account '%s'" % fromguid)
exit(1)
if toaccount is None:
stderr.write("There is no account '%s'" % toguid)
exit(1)
if fromaccount.commodity != toaccount.commodity:
# TODO: maybe introduce an override switch, we could still do this if
# the user really really wants it...
stderr.write("Account commodities don't match up, this would go wrong")
exit(1)
for transaction in data.transactions.values():
for split in transaction.splits:
if split.account == fromaccount:
stderr.write("Found split %s in transaction '%s'\n" %
(split.guid, transaction.description, ))
gnucash.change_split_account(conn, split.guid,
fromaccount.guid, toaccount.guid)
else:
stderr.write("Unknown command %s\n" % sys.argv[2])