34 lines
1 KiB
Python
34 lines
1 KiB
Python
import csv
|
|
import pathlib
|
|
from abc import ABC, abstractmethod
|
|
|
|
from xexe.exchange_rates import ExchangeRates
|
|
|
|
|
|
class RateWriter(ABC):
|
|
@abstractmethod
|
|
def write_rates(self, rates: ExchangeRates) -> None:
|
|
pass
|
|
|
|
|
|
class CSVRateWriter(RateWriter):
|
|
|
|
def __init__(self, output_file_path: pathlib.Path) -> None:
|
|
super().__init__()
|
|
self.output_file_path = output_file_path
|
|
|
|
def write_rates(self, rates: ExchangeRates) -> None:
|
|
with open(self.output_file_path, mode="w") as csv_file:
|
|
csv_writer = csv.writer(csv_file)
|
|
csv_writer.writerow(["from_currency", "to_currency", "rate", "rate_date"])
|
|
|
|
# Write the exchange rate data
|
|
for rate in rates._rate_index.values():
|
|
csv_writer.writerow(
|
|
[
|
|
rate.from_currency.value,
|
|
rate.to_currency.value,
|
|
rate.rate.amount,
|
|
rate.rate_date.strftime("%Y-%m-%d"),
|
|
]
|
|
)
|