Skip to content

Config

This module contains functions for creating, saving, loading, and setting the configuration for the AniList API.

Get_Config

Get_Config(app: object) -> None

Gets the configuration dictionary.

This function loads the configuration dictionary and sets the environment variables with the values from the configuration dictionary.

PARAMETER DESCRIPTION
app

The application object.

TYPE: App

Source code in AnilistMangaUpdater/Utils/Config.py
def Get_Config(app: object) -> None:
    """
    Gets the configuration dictionary.

    This function loads the configuration dictionary and sets the environment variables
    with the values from the configuration dictionary.

    Parameters:
        app (App): The application object.
    """
    # Load the configuration dictionary from the file
    Logger.INFO("Function Get_Config called.")
    config: Optional[dict] = load_config("config.json")

    if config:
        # If the configuration dictionary is loaded successfully, print a message
        app.update_terminal("Configuration file found.")
        Logger.INFO("Configuration file found.")

        # Set the environment variables with the values from the configuration dictionary
        Set_Environment_Variables(config)
        app.update_terminal("Environment variables set.")
        Logger.INFO("Environment variables set.")
    else:
        # If the configuration dictionary is not loaded successfully, print a message
        Logger.WARNING("Configuration file not found.")  # pylint: disable=E1101
        message = (
            "\nConfiguration file not found. Please use buttons on the left side "
            "to set the Client, Secret ID's, as well as the Private value and number of Months"
        )
        app.update_terminal(message)

Set_Environment_Variables

Set_Environment_Variables(config: dict) -> None

Sets the environment variables with the values from the configuration dictionary.

PARAMETER DESCRIPTION
config

The configuration dictionary.

TYPE: dict

Source code in AnilistMangaUpdater/Utils/Config.py
def Set_Environment_Variables(config: dict) -> None:
    """
    Sets the environment variables with the values from the configuration dictionary.

    Parameters:
        config (dict): The configuration dictionary.
    """
    Logger.INFO("Function Set_Environment_Variables called.")
    # Set the environment variables with the values from the configuration dictionary
    for key, value in config.items():
        if value is not None:
            os.environ[key] = value
            Logger.DEBUG(f"Environment variable {key} set to {value}.")
    Logger.INFO("Finished setting environment variables.")

create_config

create_config(client: str, secret: str, token: Optional[str] = None, months: Optional[int] = None, private: Optional[bool] = None) -> dict

Creates a configuration dictionary.

PARAMETER DESCRIPTION
client

The client ID for the AniList API.

TYPE: str

secret

The client secret for the AniList API.

TYPE: str

token

The access token for the AniList API. Default is None.

TYPE: str DEFAULT: None

months

The number of months to consider for the manga updates. Default is None.

TYPE: int DEFAULT: None

private

The privacy setting for the manga updates. Default is None.

TYPE: bool DEFAULT: None

RETURNS DESCRIPTION
dict

The configuration dictionary.

TYPE: dict

Source code in AnilistMangaUpdater/Utils/Config.py
def create_config(
    client: str,
    secret: str,
    token: Optional[str] = None,
    months: Optional[int] = None,
    private: Optional[bool] = None,
) -> dict:
    """
    Creates a configuration dictionary.

    Parameters:
        client (str): The client ID for the AniList API.
        secret (str): The client secret for the AniList API.
        token (str): The access token for the AniList API. Default is None.
        months (int): The number of months to consider for the manga updates. Default is None.
        private (bool): The privacy setting for the manga updates. Default is None.

    Returns:
        dict: The configuration dictionary.
    """
    # Create and return the configuration dictionary directly
    Logger.INFO("Function create_config called.")
    Logger.DEBUG(f"Parameters - client: {client}, " f"months: {months}, private: {private}")
    return {
        "ANILIST_CLIENT_ID": client,
        "ANILIST_CLIENT_SECRET": secret,
        "ACCESS_TOKEN": token,
        "MONTHS": months,
        "PRIVATE": private,
    }

load_config

load_config(file_path: str) -> Optional[dict[Union[str, Union[str, None]], None]]

Loads the configuration dictionary from a file.

PARAMETER DESCRIPTION
file_path

The path to the configuration file.

TYPE: str

RETURNS DESCRIPTION
dict

The configuration dictionary if the file is found, otherwise None.

TYPE: Optional[dict[Union[str, Union[str, None]], None]]

Source code in AnilistMangaUpdater/Utils/Config.py
def load_config(file_path: str) -> Optional[dict[Union[str, Union[str, None]], None]]:
    """
    Loads the configuration dictionary from a file.

    Parameters:
        file_path (str): The path to the configuration file.

    Returns:
        dict: The configuration dictionary if the file is found, otherwise None.
    """
    Logger.INFO("Function load_config called.")
    Logger.DEBUG(f"Attempting to load config from file: {file_path}")
    try:
        # Open the configuration file in read mode with explicit encoding
        with open(file_path, "r", encoding="utf-8") as file:
            # Load the configuration dictionary from the file
            config = json.load(file)
        Logger.INFO("Configuration loaded successfully.")
        # Return the configuration dictionary
        return config
    except FileNotFoundError:
        # If the configuration file is not found, return None
        Logger.WARNING(  # pylint: disable=E1101
            "Configuration file not found. Returning None."
        )
        return None

save_config

save_config(config: Union[dict[Union[str, None], None], None], file_path: str) -> None

Saves the configuration dictionary to a file.

PARAMETER DESCRIPTION
config

The configuration dictionary.

TYPE: dict

file_path

The path to the configuration file.

TYPE: str

Source code in AnilistMangaUpdater/Utils/Config.py
def save_config(config: Union[dict[Union[str, None], None], None], file_path: str) -> None:
    """
    Saves the configuration dictionary to a file.

    Parameters:
        config (dict): The configuration dictionary.
        file_path (str): The path to the configuration file.
    """
    # Open the configuration file in write mode with explicit encoding
    Logger.INFO("Function save_config called.")
    Logger.DEBUG(f"Saving config to file: {file_path}")
    with open(file_path, "w", encoding="utf-8") as file:
        # Write the configuration dictionary to the file in JSON format
        json.dump(config, file, indent=4)
    Logger.INFO("Config saved successfully.")