-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
executable file
·79 lines (68 loc) · 2.52 KB
/
report.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
#!/usr/bin/env python3
import sys
import datetime
import subprocess
import argparse
import textwrap
def main(args):
ledger_args = {
'ledger': args.ledger,
'file': args.file,
'month': args.month,
}
budget_output = subprocess.check_output(
args.budget_cmd.format(**ledger_args).split()
).decode('utf-8')
debt_output = {}
for person in args.people:
ledger_args['person'] = person
debt_output[person] = subprocess.check_output(
args.debt_cmd.format(**ledger_args).split()
).decode('utf-8')
report = \
"""{intro_text}
{column_information}
- Actual purchases
- Maximum monthly budget
- Percent of budget expended
- Category
{budget_report}
{owe_text}
{owe_addendum}
{owe_report}
""".format(
intro_text = textwrap.fill("""This is the monthly Hackafé finance report. The
following table shows our expenses plotted against our budget, grouped by
category.""", width = args.width),
column_information = textwrap.fill("""The columns from left to right
are:""", width = args.width),
budget_report = budget_output,
owe_text = textwrap.fill("""The amount that you owe is shown below. It is the
sum of Hackafé's liabilities to you (amounts you have paid on our behalf that
have not been repaid by others), assets (amounts you owe as result of a purchase
on your behalf) and income (amounts you have paid to others).""",
width = args.width),
owe_addendum = textwrap.fill("""If the amount is negative, Hackafé owes you
money.""", width = args.width),
owe_report = "{owe_report}")
if args.action == 'print':
for person in args.people:
print('- {} {}'.format(person,
'-' * (args.width - len(person) - 3)))
print(report.format(owe_report = debt_output[person]))
def parse(args):
parser = argparse.ArgumentParser()
parser.add_argument('action', choices=['print'])
parser.add_argument('people', nargs='+')
parser.add_argument('--ledger', default='ledger')
parser.add_argument('--budget-cmd',
default='{ledger} -f {file} -p {month} budget ^Expenses')
parser.add_argument('--debt-cmd',
default='{ledger} -f {file} balance {person}')
parser.add_argument('--month',
default=datetime.datetime.now().strftime('%Y/%m'))
parser.add_argument('--width', type=int, default=78)
parser.add_argument('--file', default='sheet.ledger')
return parser.parse_args(args)
if __name__ == "__main__":
sys.exit(main(parse(sys.argv[1:])))