|
|
|
@ -2,10 +2,11 @@
|
|
|
|
|
|
|
|
|
|
from re import search, split |
|
|
|
|
from sys import argv, stderr |
|
|
|
|
from os.path import exists |
|
|
|
|
from typing import List |
|
|
|
|
|
|
|
|
|
from piggybank import VERSION, CURRENCIES |
|
|
|
|
from piggybank.configuration import Configuration |
|
|
|
|
from piggybank.configuration import Configuration, DEFAULT_CONFIGURATION_FILE |
|
|
|
|
from piggybank.currency import Currency, BaseCurrencyError |
|
|
|
|
from piggybank.piggybank import PiggyBank |
|
|
|
|
from piggybank.transaction import sum_transactions, TYPE_INCOME, TYPE_OUTCOME |
|
|
|
@ -71,13 +72,16 @@ def parse_arguments(args: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def print_transactions(pb: PiggyBank) -> None: |
|
|
|
|
cur = CURRENCIES[pb.currency] |
|
|
|
|
currency = CURRENCIES[pb.currency] |
|
|
|
|
names = (currency.currency_symbol.format(n//100) if n >= 100 \ |
|
|
|
|
else currency.fraction_symbol.format(n) \ |
|
|
|
|
for n in currency.face_values) |
|
|
|
|
def print_separator(l: str, m: str, r: str): |
|
|
|
|
print(f"{l}{'━'*21}{m}{'━'*5}{m}{m.join(['━'*12]*cur.count)}{r}") |
|
|
|
|
print(f"{l}{'━'*21}{m}{'━'*5}{m}{m.join(['━'*12]*len(currency))}{r}") |
|
|
|
|
|
|
|
|
|
print_separator("┏", "┳", "┓") |
|
|
|
|
print(f"┃{'Timestamp':^21}┃ I/O ┃" \ |
|
|
|
|
f"{'┃'.join(f'{n:^12}' for n in cur.coin_names)}┃") |
|
|
|
|
f"{'┃'.join(f'{n:^12}' for n in names)}┃") |
|
|
|
|
print_separator("┣", "╋", "┫") |
|
|
|
|
for t in pb.transactions: |
|
|
|
|
print(f"┃ {t.timestamp.replace('T', ' ')} " |
|
|
|
@ -86,21 +90,24 @@ def print_transactions(pb: PiggyBank) -> None:
|
|
|
|
|
print_separator("┗", "┻", "┛") |
|
|
|
|
|
|
|
|
|
def print_summary(pb: PiggyBank) -> None: |
|
|
|
|
cur = CURRENCIES[pb.currency] |
|
|
|
|
currency = CURRENCIES[pb.currency] |
|
|
|
|
names = (currency.currency_symbol.format(n//100) if n >= 100 \ |
|
|
|
|
else currency.fraction_symbol.format(n) \ |
|
|
|
|
for n in currency.face_values) |
|
|
|
|
def print_separator(l: str, lm: str, rm: str, r:str): |
|
|
|
|
print(f"{l}{'━'*27}{lm}{rm.join(['━'*12]*cur.count)}{r}") |
|
|
|
|
print(f"{l}{'━'*27}{lm}{rm.join(['━'*12]*len(currency))}{r}") |
|
|
|
|
|
|
|
|
|
print_separator("┏", "┳", "┳", "┓") |
|
|
|
|
print(f"┃{cur.name:^27}┃{'┃'.join(f'{n:^12}' for n in cur.coin_names)}┃") |
|
|
|
|
print(f"┃{currency.name:^27}┃{'┃'.join(f'{n:^12}' for n in names)}┃") |
|
|
|
|
print_separator("┣", "╋", "╋", "┫") |
|
|
|
|
s = sum_transactions(pb.transactions) |
|
|
|
|
print(f"┃{'Counts':^27}┃{'┃'.join(f'{c:^12}' for c in s)}┃") |
|
|
|
|
print_separator("┣", "╋", "╋", "┫") |
|
|
|
|
s = cur * s |
|
|
|
|
s = currency * s |
|
|
|
|
print(f"┃{'Sums in currency':^27}┃{'┃'.join(f'{c/100:^12.2f}' for c in s)}┃") |
|
|
|
|
print_separator("┣", "╋", "┻", "┫") |
|
|
|
|
s = sum(s) |
|
|
|
|
print(f"┃{'Total in currency':^27}┃{s/100:^{12*cur.count+cur.count-1}.2f}┃") |
|
|
|
|
print(f"┃{'Total in currency':^27}┃{s/100:^{12*len(currency)+len(currency)-1}.2f}┃") |
|
|
|
|
print_separator("┗", "┻", "━", "┛") |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -146,19 +153,23 @@ def main():
|
|
|
|
|
exit() |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
if not exists(DEFAULT_CONFIGURATION_FILE): |
|
|
|
|
Configuration.write_default_to_file(DEFAULT_CONFIGURATION_FILE) |
|
|
|
|
load_currencies() |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
pb = PiggyBank.from_file(args["file"]) |
|
|
|
|
except FileNotFoundError: |
|
|
|
|
if args["action"] == "in": |
|
|
|
|
if args["action"] == 'in': |
|
|
|
|
currency = Configuration()["default-currency"] \ |
|
|
|
|
if args["currency"] is None else args["currency"] |
|
|
|
|
if not args["currency"] else args["currency"] |
|
|
|
|
pb = PiggyBank(currency) |
|
|
|
|
else: |
|
|
|
|
raise FileNotFoundError(f"{args['file']} is missing.") |
|
|
|
|
|
|
|
|
|
if args["action"] in ["in", "from"]: |
|
|
|
|
coins = complement_list_of_coins(args["coins"], pb.currency, |
|
|
|
|
args["reversed"]) |
|
|
|
|
args["reversed"]) |
|
|
|
|
pb.transact(coins, TYPE_INCOME if args["action"] == "in" \ |
|
|
|
|
else TYPE_OUTCOME) |
|
|
|
|
pb.save(args["file"]) |
|
|
|
@ -177,4 +188,4 @@ def main():
|
|
|
|
|
f"{type(err).__name__}:", err, file=stderr) |
|
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
main() |
|
|
|
|
main() |
|
|
|
|