import json import logging.config import os from pathlib import Path import pendulum import yaml from dotenv import load_dotenv from models import Action, BookingFilter, Club, User load_dotenv() ROOT_DIR = Path(__file__).parent def get_booking_filter() -> BookingFilter: """ Read the environment variables related to the current booking filter and build the BookingFilter object :return: the club """ sport_name = os.environ.get("SPORT_NAME") date_time_tmp = os.environ.get("DATE_TIME") date_time = pendulum.parse(date_time_tmp) if date_time_tmp else None return BookingFilter(sport_name=sport_name.lower(), date=date_time) def get_user() -> User: """ Read the environment variables related to the current user and build the User object :return: the club """ login = os.environ.get("LOGIN") password = os.environ.get("PASSWORD") return User(login=login, password=password) def get_post_headers(platform_id: str) -> dict: """ Get the headers for the POST endpoint related to a specific booking platform :param platform_id: the platform to which the headers apply :return: the headers as a dictionary """ root_path = Path(__file__).parent headers_file = Path(root_path, "resources", platform_id, "post-headers.json") with headers_file.open(mode="r", encoding="utf-8") as f: headers = json.load(f) return headers def init_log_config(): """ Read the logging.yaml file to initialize the logging configuration """ logging_file = ROOT_DIR / "logging.yaml" with logging_file.open(mode="r", encoding="utf-8") as f: logging_config = yaml.safe_load(f.read()) logging.config.dictConfig(logging_config) def _build_urls(platform_urls: dict) -> dict: return {url["name"]: url for url in platform_urls} def _read_clubs(platforms_data: dict, clubs_data: dict) -> dict[str, Club]: platforms = {platform["id"]: platform for platform in platforms_data} for club in clubs_data["clubs"]: club_platform = club["bookingPlatform"] platform_id = club_platform["id"] club_platform["urls"] = _build_urls(platforms[platform_id]["urls"]) return {club["id"]: Club(**club) for club in clubs_data["clubs"]} def get_clubs(): platforms_file = ROOT_DIR / "resources" / "platforms.yaml" with platforms_file.open(mode="r", encoding="utf-8") as fp: platforms_data = yaml.safe_load(fp) clubs_file = ROOT_DIR / "resources" / "clubs.yaml" with clubs_file.open(mode="r", encoding="utf-8") as fp: clubs_data = yaml.safe_load(fp) return _read_clubs(platforms_data["platforms"], clubs_data) def get_club() -> Club: """ Get the club from an environment variable :return: the club """ club_id = os.environ.get("CLUB_ID") clubs = get_clubs() return clubs[club_id] def read_users(data: dict, club_id: str) -> list[User]: """ Deserialize users :param data: the dictionnary of users :param club_id: the club id :return: a list if users from the club """ for club in data.get("clubs"): if club.get("id") == club_id: return [User(**user) for user in club.get("users")] def get_users(club_id: str) -> list[User]: """ Get a list of users from a club :param club_id: the club to which the users should have an account :return: the list of all users for that club """ users_file = ROOT_DIR / "resources" / "users.yaml" with users_file.open(mode="r", encoding="utf-8") as fp: data = yaml.safe_load(fp) return read_users(data, club_id) def get_resources_folder() -> Path: """ Compute the path to the resources used by the program :return: the path to the resources folder """ default_resources_folder = Path(__file__).parent / "resources" return Path(os.environ.get("RESOURCES_FOLDER", default_resources_folder)) def get_action() -> Action: """ Get the action to perform from an environment variable :return: the action to perform """ return Action(os.environ.get("ACTION").lower())