2020-06-04 05:11:29 +04:00
|
|
|
from re import search
|
|
|
|
from typing import List, Callable
|
|
|
|
from sys import argv, exit
|
2020-03-27 20:14:22 +04:00
|
|
|
|
2020-06-04 05:11:29 +04:00
|
|
|
from piggybank import print_program_version
|
|
|
|
from piggybank.configuration import Configuration
|
2020-03-27 20:14:22 +04:00
|
|
|
from piggybank.currencies import CURRENCIES
|
|
|
|
|
2020-06-04 05:11:29 +04:00
|
|
|
__all__ = ["handle_default_arguments", "complement_array_of_coins"]
|
|
|
|
|
|
|
|
|
|
|
|
USAGE_COMMON: str = "Usage: piggybank-* " \
|
|
|
|
"[(-h|--help)|(-v|--version)|(-L|--list-currencies)]\n" \
|
|
|
|
"A set of common flags. Only one could be specified.\n" \
|
|
|
|
"-h, --help -- print this help;\n" \
|
|
|
|
"-v, --version -- print program version;\n" \
|
|
|
|
"-L, --list-currencies -- print supported currencies;\n" \
|
|
|
|
"--set-default-currency CURRENCY -- set default currency.\n"
|
|
|
|
|
|
|
|
|
|
|
|
def parse_common_arguments(args: str):
|
|
|
|
r = r"(?P<help>-h|--help)|(?P<version>-v|--version)" \
|
|
|
|
r"|(?P<list_currencies>-L|--list-currencies)" \
|
|
|
|
r"|(?=--set-default-currency (?P<default_currency>\w+))"
|
|
|
|
argd = search(r, args)
|
|
|
|
if not argd is None:
|
|
|
|
argd = argd.groupdict()
|
|
|
|
return {
|
|
|
|
"help": argd["help"] is None,
|
|
|
|
"version": argd["transactions"] is None,
|
|
|
|
"list-currencies": argd["list_currencies"] is None,
|
|
|
|
"default-currency": argd["default_currency"] }
|
|
|
|
|
|
|
|
|
|
|
|
def handle_default_arguments(args: dict, help_func: Callable) -> None:
|
|
|
|
cargs = parse_common_arguments(' '.join(argv))
|
|
|
|
|
|
|
|
if cargs["help"]:
|
|
|
|
help_func()
|
|
|
|
print(USAGE_COMMON)
|
|
|
|
exit()
|
|
|
|
elif cargs["version"]:
|
|
|
|
print_program_version()
|
|
|
|
exit()
|
|
|
|
elif cargs["list-currencies"]:
|
|
|
|
print_supported_currencies()
|
|
|
|
exit()
|
|
|
|
elif not cargs["default-currency"] is None:
|
|
|
|
Configuration()["default-currency"] = cargs["default-currency"]
|
|
|
|
exit()
|
|
|
|
|
|
|
|
|
|
|
|
def print_supported_currencies() -> None:
|
|
|
|
"""Print a list of supported currencies."""
|
|
|
|
print("Supported currencies are:")
|
|
|
|
for cur in CURRENCIES:
|
|
|
|
print(f" {cur:^4} ┃ {CURRENCIES[cur]['name']:^31}"
|
|
|
|
f" ┃ {CURRENCIES[cur]['description']}")
|
|
|
|
print("Default currency is", Configuration()["default-currency"])
|
2020-03-27 20:14:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
def complement_array_of_coins(coins: List[int], currency: str,
|
|
|
|
_reversed: bool = False) -> List[int]:
|
|
|
|
"""Complements array of coins up to the count of currency's coins."""
|
|
|
|
offset_array = [0] * (CURRENCIES[currency]["count"] - len(coins))
|
2020-06-04 05:11:29 +04:00
|
|
|
return offset_array + coins if not _reversed else coins + offset_array
|