Feat(back): retreive_months

This commit is contained in:
Bertrand Benjamin 2023-06-06 16:05:01 +02:00
parent eb97abee7f
commit 7ab8df0f24
3 changed files with 30 additions and 3 deletions

0
backend/src/__init__.py Normal file
View File

View File

@ -1,10 +1,9 @@
from fastapi import FastAPI
from ..config import config
from ..months import retreive_months
app = FastAPI()
@app.get("/months")
async def get_months():
print(config)
return {"message": "plop"}
return retreive_months()

28
backend/src/months.py Normal file
View File

@ -0,0 +1,28 @@
from .config import config
from pathlib import Path
import csv
CSV_STYLE = {
"delimiter": ",",
"quotechar": '"',
}
def list_month_files(file_schema:str="*_months.csv") -> list[Path]:
return list(Path(config["datapath"]).glob(file_schema))
def extract_month(filename:Path) -> list[dict[str,str]]:
months = []
with open(filename, 'r', newline='') as csvfile:
month_reader = csv.DictReader(csvfile, **CSV_STYLE)
for month in month_reader:
months.append(month)
return months
def retreive_months():
month_files = list_month_files()
months = []
for file in month_files:
months += extract_month(file)
return months