-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_sheet.py
executable file
·63 lines (50 loc) · 1.96 KB
/
read_sheet.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
#!/usr/bin/env python3
import sys
import csv
import datetime
import argparse
from decimal import Decimal
# Minimal implementation of translation layer for our CSV to Ledger
from transaction import *
def main(args):
# Open the outfile if it's a path, or use it directly.
out = args.output
with open(out, 'w') if type(out) == str else out as of:
# Print the contents of the header file.
if args.header:
try:
with open(args.header, 'r') as f:
for line in f:
print(line, end='', file=of)
print(file=of)
except FileNotFoundError:
print("Could not open header file, skipping: {}"
.format(args.header),
file=os.stderr)
ts = []
# Read the purchases file
with open(args.purchases, 'r') as f:
# Discard the first two lines, which are garbage.
next(f)
next(f)
# Read the purchases
ts.extend((Purchase(row) for row in csv.DictReader(f)))
# Read the payments file
with open(args.payments, 'r') as f:
# Read the transactions
ts.extend((Payment(row) for row in csv.DictReader(f)))
# Remove invalid transactions.
ts = filter(lambda t: t.valid, ts)
for t in sorted(ts, key=lambda t: t.date):
# Print the transaction and its ledger translation
print(t.ledger(), file=of)
print(file=of)
def parse(args):
parser = argparse.ArgumentParser()
parser.add_argument("--purchases", "--pu", "-p", default="purchases.csv")
parser.add_argument("--payments", "--pay", "--pa", "-P", default="payments.csv")
parser.add_argument("--output", "-o", default=sys.stdout)
parser.add_argument("--header", "-H", default="header.ledger")
return parser.parse_args(args)
if __name__ == "__main__":
sys.exit(main(parse(sys.argv[1:])))