data-xexe/xexe/inputs_handling.py
2024-06-11 13:34:49 +02:00

51 lines
1.5 KiB
Python

import datetime
import logging
import pathlib
from typing import Union
from money.currency import Currency
from xexe.constants import DEFAULT_CURRENCIES
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,
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:
currencies = DEFAULT_CURRENCIES
# 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,
"output": output,
}
logger.debug(prepared_inputs)
return prepared_inputs