-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathget_sorted_lots.py
56 lines (51 loc) · 1.85 KB
/
get_sorted_lots.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
import subprocess, os, sys
from decimal import *
import re
if len(sys.argv) < 2:
print "Usage: %s <file> [amount to spend]" % sys.argv[0]
sys.exit(-1)
# Get the output
output = subprocess.check_output(['ledger', '-f', sys.argv[1], 'bal', '--lots'])
# Find the break
output = output.split("--------------------")[1]
# Remove the excess
lots = []
for line in output.split('\n'):
match = re.search("(\w+)\s+\{(.+)\} \[(.+)\]", line)
if match:
# This line counts
# Figure out the commodity
lots.append([match.group(3), match.group(1), line])
for i in sorted(lots):
print i[2]
# If we were given an amount, find the amounts from the lots that comprise the transaction
if len(sys.argv) > 2:
parts = sys.argv[2].split(" ")
if len(parts) != 2:
print "Invalid format for amount"
sys.exit(-2)
print ":: Lot use for %s:" % sys.argv[2]
total = Decimal(0)
target = Decimal(parts[0])
for i in sorted(lots):
fields = i[2].split(" ")
# ensure it's the right commodity
if i[1] != parts[1]:
continue
n = Decimal(fields[0])
if total + n <= target:
# add it
total += n
print "\t" + i[2]
elif total + n > target:
diff = target - total
fields[0] = diff
i[2] = " ".join([str(k) for k in fields])
print "\t" + i[2]
total += diff
break
# Done
if target == total:
print "Success!"
else:
print "Failed to fund transaction (" + target + " != " + total + ")"