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

@ -74,7 +74,7 @@ def get_rates(
dry_run: bool,
output: pathlib.Path,
):
handle_get_rates_inputs(
inputs = handle_get_rates_inputs(
start_date=start_date,
end_date=end_date,
currencies=currencies,

View file

@ -1,6 +1,10 @@
import pathlib
from dataclasses import dataclass
from currencies import Currency
DEFAULT_CURRENCIES = {Currency("EUR"), Currency("GBP"), Currency("USD")}
@dataclass
class PATHS:

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

42
xexe/utils.py Normal file
View file

@ -0,0 +1,42 @@
import datetime
class DateRange:
def __init__(self, start_date: datetime.date, end_date: datetime.date):
if type(start_date) != datetime.date or type(end_date) != datetime.date:
raise TypeError("start_date and end_date must be date objects.")
if start_date > end_date:
raise ValueError("start_date can't be after end_date.")
self._start_date = start_date
self._end_date = end_date
@property
def start_date(self):
return self._start_date
@start_date.setter
def start_date(self, value: datetime.date):
if type(value) != datetime.date:
raise TypeError("start_date must be a date object.")
if value > self._end_date:
raise ValueError("start_date can't be after end_date.")
self._start_date = value
@property
def end_date(self):
return self._end_date
@end_date.setter
def end_date(self, value: datetime.date):
if type(value) != datetime.date:
raise TypeError("end_date must be a date object.")
if value < self._start_date:
raise ValueError("end_date can't be before start_date.")
self._end_date = value