def _calculate_current_cost(holding, today):
    """Calculates current cost of a holding (in a local currency)."""

    rate_archive = RateArchive()
    cur_rates = rate_archive.get_approx(holding["currency"], today)
    if cur_rates is not None:
        # TODO: bank interest
        holding["current_cost"] = holding["current_amount"] * cur_rates[1]
def _calculate_holding_info(holding, today):
    """Calculates various info about a holding."""

    _calculate_past_cost(holding, today)
    _calculate_rate_profit(holding, today)
    _calculate_current_amount(holding, today)
    _calculate_current_cost(holding, today)
    _calculate_pure_profit(holding, today)


    rate_archive = RateArchive()

    for completion in holding.get("completions", []):
        if completion["date"] <= today:
            holding["amount"] += completion["amount"]

    cur_rates = rate_archive.get_approx(holding["currency"], today)
    if cur_rates is not None:
        holding["cost"] = holding["amount"] * cur_rates[1]
Beispiel #3
0
def main():
    """The application's main function."""

    show_all = False
    debug_mode = False
    offline_mode = False
    show_expiring = None
    today = datetime.date.today()

    try:
        # Parsing command line options -->
        try:
            cmd_options, cmd_args = getopt.gnu_getopt(sys.argv[1:],
                "ade:hot:", [ "all", "debug-mode", "expiring=", "help", "offline-mode", "today=" ] )

            for option, value in cmd_options:
                if option in ("-a", "--all"):
                    show_all = True
                elif option in ("-d", "--debug-mode"):
                    debug_mode = True
                elif option in ("-e", "--expiring"):
                    try:
                        show_expiring = int(value)
                        if show_expiring < 0:
                            raise Exception("negative number")
                    except Exception:
                        raise Error("Invalid number of days ({}).", value)
                elif option in ("-h", "--help"):
                    print (
                        """pydeposits [OPTIONS]\n\n"""
                         """Options:\n"""
                         """ -a, --all            show all deposits (not only that are not closed)\n"""
                         """ -t, --today DAY      behave like today is the day, specified by the argument in {0} format\n"""
                         """ -e, --expiring DAYS  print only deposits which will be expired in DAYS days (useful for running by cron)\n"""
                         """ -o, --offline-mode   offline mode (do not connect to the Internet for getting currency rates)\n"""
                         """ -d, --debug-mode     enable debug mode\n"""
                         """ -h, --help           show this help"""
                         .format(constants.DATE_FORMAT)
                    )
                    sys.exit(0)
                elif option in ("-o", "--offline-mode"):
                    offline_mode = True
                elif option in ("-t", "--today"):
                    try:
                        today = datetime.datetime.strptime(value, constants.DATE_FORMAT).date()
                    except Exception as e:
                        raise Error("Invalid today date ({}).", value)
                else:
                    raise Error("Logical error.")
            if len(cmd_args):
                raise Error("'{}' is not recognized", cmd_args[0])
        except Exception as e:
            raise Error("Invalid arguments:").append(e)
        # Parsing command line options <--

        pcli.log.setup(name="pydeposits", debug_mode=debug_mode)

        if debug_mode:
            RateArchive.set_db_dir(os.path.abspath("."))
        RateArchive.enable_offline_mode(offline_mode)

        try:
            deposits = pydeposits.deposits.get()
        except Error as e:
            # To print exact error string without any modifications by EE().
            sys.exit(str(e))

        if show_expiring is not None:
            pydeposits.statements.print_expiring(deposits, today, show_expiring)
        else:
            pydeposits.statements.print_account_statement(deposits, today, show_all)
    except Exception as e:
        if debug_mode:
            traceback.print_exc()
            sys.exit(1)
        else:
            sys.exit("Error: " + EE(e))
    else:
        sys.exit(0)