add CLI to analyze.py
This commit is contained in:
+41
-4
@@ -3,7 +3,6 @@
|
||||
'''extract and process donation data from Whiteleaf logs.
|
||||
|
||||
This only accounts for donation done through the WL system.
|
||||
|
||||
'''
|
||||
|
||||
import sys
|
||||
@@ -12,6 +11,8 @@ if sys.version_info < (3, 7):
|
||||
|
||||
from typing import *
|
||||
|
||||
import argparse
|
||||
from collections import defaultdict
|
||||
from decimal import Decimal
|
||||
from datetime import datetime as DateTime
|
||||
from dataclasses import dataclass
|
||||
@@ -88,6 +89,42 @@ def parse_donations(lines: Iterable[str]) -> Iterable[Donation]:
|
||||
amount=Decimal(x['amount'].replace(',', '')),
|
||||
)
|
||||
|
||||
# Display total donation per log file.
|
||||
for f in sorted(Path().glob('*.txt')):
|
||||
print(f, sum(d.amount for d in parse_donations(f.open())))
|
||||
def all_files():
|
||||
return Path().glob('*.txt')
|
||||
|
||||
def grand(args):
|
||||
'''print grand total'''
|
||||
print(sum(d.amount for f in all_files() for d in parse_donations(f.open())))
|
||||
|
||||
def files(args):
|
||||
'''print total per file'''
|
||||
for f in sorted(all_files()):
|
||||
print(f, sum(d.amount for d in parse_donations(f.open())))
|
||||
|
||||
def paypigs(args):
|
||||
'''print total per paypig'''
|
||||
sums = defaultdict(Amount)
|
||||
for f in all_files():
|
||||
for d in parse_donations(f.open()):
|
||||
sums[d.paypig] += d.amount
|
||||
for paypig, amount in sorted(sums.items(), key=lambda i: i[1]):
|
||||
print(paypig, amount)
|
||||
|
||||
|
||||
top = argparse.ArgumentParser()
|
||||
commands = top.add_subparsers()
|
||||
argparser_grand = commands.add_parser("grand", help="compute grand total of donations")
|
||||
argparser_grand.set_defaults(func=grand)
|
||||
argparser_files = commands.add_parser("files", help="tabulate sum for each logfile")
|
||||
argparser_files.set_defaults(func=files)
|
||||
argparser_paypigs = commands.add_parser("paypigs", help="tabulate sum for each paypig")
|
||||
argparser_paypigs.set_defaults(func=paypigs)
|
||||
|
||||
args = top.parse_args()
|
||||
try:
|
||||
func = args.func
|
||||
except AttributeError:
|
||||
top.print_help()
|
||||
sys.exit(2)
|
||||
else:
|
||||
func(args)
|
||||
|
||||
Reference in New Issue
Block a user