data-xexe/xexe/inputs_handling.py
2024-06-12 17:36:33 +02:00

59 lines
1.8 KiB
Python

import datetime
import logging
import pathlib
from typing import Union
from money.currency import Currency
from xexe.constants import DEFAULT_CURRENCIES, RATES_SOURCES
from xexe.utils import DateRange
logger = logging.getLogger()
def handle_get_rates_inputs(
start_date: Union[datetime.datetime, datetime.date],
end_date: Union[datetime.datetime, datetime.date],
currencies: Union[None, str],
dry_run: bool,
rates_source: str,
ignore_warnings: bool,
output: Union[str, pathlib.Path],
):
logger.info("Handling inputs.")
date_range = DateRange(start_date=start_date.date(), end_date=end_date.date())
if date_range.end_date > datetime.datetime.today().date():
date_range.end_date = datetime.datetime.today().date()
if currencies:
# CLI input comes as a string of comma-separated currency codes
currencies = {currency_code.strip() for currency_code in currencies.split(",")}
tmp = {Currency(currency_code) for currency_code in currencies}
currencies = tmp
if currencies is None or currencies == "":
logger.info("No currency list passed. Running for default currencies.")
currencies = DEFAULT_CURRENCIES
if rates_source not in RATES_SOURCES:
raise ValueError(f"--rates-source must be one of {RATES_SOURCES.keys()}.")
# The Path constructor is idempotent, so this works equally fine if output
# is a string or an actual Path object.
output = pathlib.Path(output)
if output.suffix != ".csv":
raise ValueError("Output must be a .csv file.")
prepared_inputs = {
"date_range": date_range,
"currencies": currencies,
"dry_run": dry_run,
"rates_source": rates_source,
"ignore_warnings": ignore_warnings,
"output": output,
}
logger.debug(prepared_inputs)
return prepared_inputs