From 7ab8df0f24db82415c37eafb85f31c30fe208a4c Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Tue, 6 Jun 2023 16:05:01 +0200 Subject: [PATCH] Feat(back): retreive_months --- backend/src/__init__.py | 0 backend/src/api/main.py | 5 ++--- backend/src/months.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 backend/src/__init__.py create mode 100644 backend/src/months.py diff --git a/backend/src/__init__.py b/backend/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/src/api/main.py b/backend/src/api/main.py index 87a1b02..355f09b 100644 --- a/backend/src/api/main.py +++ b/backend/src/api/main.py @@ -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() diff --git a/backend/src/months.py b/backend/src/months.py new file mode 100644 index 0000000..56f8d51 --- /dev/null +++ b/backend/src/months.py @@ -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