from re import search from typing import List, Callable from sys import argv from piggybank import print_program_version from piggybank.configuration import Configuration from piggybank.currencies import CURRENCIES __all__ = ["handle_default_arguments", "complement_list_of_coins", "print_supported_currencies", "decimal_to_float"] 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-h|--help)|(?P-v|--version)" \ r"|(?P-L|--list-currencies)" \ r"|(?=--set-default-currency (?P\w+))" argd = search(r, args) if not argd is None: argd = argd.groupdict() return { "help": not argd["help"] is None, "version": not argd["version"] is None, "list-currencies": not argd["list_currencies"] is None, "default-currency": argd["default_currency"] } return None def handle_default_arguments(args: dict, help_func: Callable) -> None: cargs = parse_common_arguments(' '.join(argv)) if not cargs is None: 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: conf = Configuration() conf["default-currency"] = cargs["default-currency"] conf.save() 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"]) def complement_list_of_coins(coins: List[int], currency: str, _reversed: bool = False) -> List[int]: """Complements list of coins up to the count of currency's coins.""" offset_array = [0] * (CURRENCIES[currency]["count"] - len(coins)) return offset_array + coins if not _reversed else coins + offset_array def decimal_to_float(lst: List[int]) -> List[float]: """Converts decimal style of storing money amounts to float.""" return list(map(lambda x: x / 100, lst))