1
0
PiggyBank/piggybank/cli/take.py
Alexander "Arav" Andreev ca5d2157c8 A little edit of strings and docstrings. Sorted imports ascendingly.
Added replacement of letter T that separates data and time in a timestamp.
So timestamps now are looking prettier.
2019-12-29 18:26:37 +04:00

57 lines
2.0 KiB
Python

"""CLI: Take a set of coins from a coin box."""
from argparse import ArgumentParser
from sys import exit, stderr
from piggybank import print_program_version
from piggybank.cli import EPILOGUE
from piggybank.currencies import CURRENCIES, DEFAULT_CURRENCY, \
BaseCurrencyError, print_supported_currencies
from piggybank.piggybank import PiggyBank
from piggybank.transaction import TYPE_OUTCOME
from piggybank.util import add_common_arguments_to_parser, \
complement_array_of_coins
__all__ = ["main"]
def main():
"""An entry point for a take command."""
parser = ArgumentParser(prog="piggybank-take",
description="Take a set of coins from a coin box.",
epilog=EPILOGUE)
parser.add_argument("file", type=str,
help="a coin box file name. Missing .cb extension will"
"be added")
parser.add_argument("coins", type=int, nargs="+", metavar="COIN",
help="add a set of coins. A new file will be created"
"if it doesn't exist")
add_common_arguments_to_parser(parser)
args = parser.parse_args()
if args.version:
print_program_version()
exit()
if args.list_currencies:
print_supported_currencies()
exit()
try:
piggybank = PiggyBank.from_file(args.file)
coins = complement_array_of_coins(args.coins, piggybank.currency,
args.reverse)
piggybank.transact(coins, TYPE_OUTCOME)
piggybank.save(args.file)
except BaseCurrencyError as err:
print(f"{type(err).__name__}:", err, file=stderr)
except ValueError as err:
print(f"{type(err).__name__}:", err, file=stderr)
except FileNotFoundError as err:
print(f"{type(err).__name__}:", f"{err} doesn't exist.", file=stderr)
except Exception as err:
print(f"Something went exceptionally wrong. Error:",
f"{type(err).__name__}:", err, file=stderr)