quite a bit of development around get rates input handling

This commit is contained in:
Pablo Martin 2024-06-07 16:10:35 +02:00
parent 46988352ca
commit 4f81ac2e62
8 changed files with 199 additions and 20 deletions

View file

@ -1,12 +1,51 @@
import datetime
import logging
import pathlib
from typing import Union
from currencies import Currency
from xexe.constants import DEFAULT_CURRENCIES
from xexe.utils import DateRange
logger = logging.getLogger()
def handle_get_rates_inputs(start_date, end_date, currencies, dry_run, output):
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.")
logger.debug(f"Received start_date: {start_date}")
logger.debug(f"Received end_date: {end_date}")
logger.debug(f"Received currencies: {currencies}")
logger.debug(f"dry_run state: {dry_run}")
logger.debug(f"Received output: {output}")
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 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