<gh_stars>1-10
"""pysav.utility_functions - Contains functions used in edge cases.

Pysav repeatedly uses code which is specifically for ease of use.
The functions are available in this module should an edge case appear
where one of these functions is needed.e

Pysav is a Python module that allows you to easily save and load data
into a folder within an environment for your Python App. Pysav aims to take
the hassle out of saving and loading data.

https://github.com/ThomasLandstra/pysav

Shared under the MIT License.
https://github.com/ThomasLandstra/pysav/blob/main/LICENSE
"""

# Imports
from os.path import join, expanduser, isdir, isfile
from os import getenv, listdir
from typing import Optional, List
from cryptography.fernet import Fernet
from .exceptions import UnsupportedEnvironment

# Utility functions
def supported_formats() -> List[str]:
    """Returns a list of the supported file formats for the save class."""
    return ["json", "encrypted"]


def supported_environments() -> List[str]:
    """Returns a list of the supported save path environments for the save class."""
    return [
        "3D OBJECTS",
        "APPDATA",
        "CONTACTS",
        "DESKTOP",
        "DOCUMENTS",
        "DOWNLOADS",
        "FAVORITES",
        "LINKS",
        "LOCALAPPDATA",
        "MUSIC",
        "PICTURES",
        "SAVED GAMES",
        "SEARCHES",
        "VIDEOS",
    ]


def generate_environment_path(environment: str) -> str:
    """Returns the path of the given environment."""
    environment = environment.upper()

    if environment in ["APPDATA", "LOCALAPPDATA", "TEMP"]:
        environment = getenv(environment)
    elif environment in [
        "CONTACTS",
        "DESKTOP",
        "DOCUMENTS",
        "DOWNLOADS",
        "FAVORITES",
        "LINKS",
        "MUSIC",
        "PICTURES",
        "SEARCHES",
        "VIDEOS",
    ]:
        environment = expanduser("~\\" + environment[0] + environment.lower()[1:])
    elif environment == "3D OBJECTS":
        environment = expanduser("~\\3D Objects")
    elif environment == "SAVED GAMES":
        environment = expanduser("~\\Saved Games")
    else:
        raise UnsupportedEnvironment(environment)

    return environment


def does_app_dir_exist(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """
    Finds the app's directory (if any) and returns
    a bool representing if the directory exists.
    """

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return isdir(save_path)


def does_save_exist(
    app_name: str,
    save_name: str,
    extension: Optional[str] = "json",
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """Finds the file (if any) and returns a bool representing if the file exists."""

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return isfile(join(save_path, save_name) + "." + extension)


def does_any_save_exist(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """
    Finds any file in the apps directory (if any)
    and returns a bool representing if any file exists.
    """

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    try:
        return len(listdir(save_path)) > 0
    except FileNotFoundError:
        return False


def list_save_files(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> List[str]:
    """Finds all files in the apps directory (if any) and returns a list of them."""

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return listdir(save_path)


def generate_key() -> bytes:
    """Generates an encryption key for the encrypted file format."""
    return Fernet.generate_key()

"""pysav.utility_functions - Contains functions used in edge cases.

Pysav repeatedly uses code which is specifically for ease of use.
The functions are available in this module should an edge case appear
where one of these functions is needed.e

Pysav is a Python module that allows you to easily save and load data
into a folder within an environment for your Python App. Pysav aims to take
the hassle out of saving and loading data.

https://github.com/ThomasLandstra/pysav

Shared under the MIT License.
https://github.com/ThomasLandstra/pysav/blob/main/LICENSE
"""

# Imports
from os.path import join, expanduser, isdir, isfile
from os import getenv, listdir
from typing import Optional, List
from cryptography.fernet import Fernet
from .exceptions import UnsupportedEnvironment

# Utility functions
def supported_formats() -> List[str]:
    """Returns a list of the supported file formats for the save class."""
    return ["json", "encrypted"]


def supported_environments() -> List[str]:
    """Returns a list of the supported save path environments for the save class."""
    return [
        "3D OBJECTS",
        "APPDATA",
        "CONTACTS",
        "DESKTOP",
        "DOCUMENTS",
        "DOWNLOADS",
        "FAVORITES",
        "LINKS",
        "LOCALAPPDATA",
        "MUSIC",
        "PICTURES",
        "SAVED GAMES",
        "SEARCHES",
        "VIDEOS",
    ]


def generate_environment_path(environment: str) -> str:
    """Returns the path of the given environment."""
    environment = environment.upper()

    if environment in ["APPDATA", "LOCALAPPDATA", "TEMP"]:
        environment = getenv(environment)
    elif environment in [
        "CONTACTS",
        "DESKTOP",
        "DOCUMENTS",
        "DOWNLOADS",
        "FAVORITES",
        "LINKS",
        "MUSIC",
        "PICTURES",
        "SEARCHES",
        "VIDEOS",
    ]:
        environment = expanduser("~\\" + environment[0] + environment.lower()[1:])
    elif environment == "3D OBJECTS":
        environment = expanduser("~\\3D Objects")
    elif environment == "SAVED GAMES":
        environment = expanduser("~\\Saved Games")
    else:
        raise UnsupportedEnvironment(environment)

    return environment


def does_app_dir_exist(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """
    Finds the app's directory (if any) and returns
    a bool representing if the directory exists.
    """

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return isdir(save_path)


def does_save_exist(
    app_name: str,
    save_name: str,
    extension: Optional[str] = "json",
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """Finds the file (if any) and returns a bool representing if the file exists."""

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return isfile(join(save_path, save_name) + "." + extension)


def does_any_save_exist(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> bool:
    """
    Finds any file in the apps directory (if any)
    and returns a bool representing if any file exists.
    """

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    try:
        return len(listdir(save_path)) > 0
    except FileNotFoundError:
        return False


def list_save_files(
    app_name: str,
    environment: Optional[str] = "APPDATA",
    save_path: Optional[str] = None,
) -> List[str]:
    """Finds all files in the apps directory (if any) and returns a list of them."""

    if save_path is None:
        save_path = join(generate_environment_path(environment), app_name)

    return listdir(save_path)


def generate_key() -> bytes:
    """Generates an encryption key for the encrypted file format."""
    return Fernet.generate_key()
pysav.utility_functions - Contains functions used in edge cases.  Pysav repeatedly uses code which is specifically for ease of use. The functions are available in this module should an edge case appear where one of these functions is needed.e  Pysav is a Python module that allows you to easily save and load data into a folder within an environment for your Python App. Pysav aims to take the hassle out of saving and loading data.  https://github.com/ThomasLandstra/pysav  Shared under the MIT License. https://github.com/ThomasLandstra/pysav/blob/main/LICENSE # Imports # Utility functions Returns a list of the supported file formats for the save class. Returns a list of the supported save path environments for the save class. Returns the path of the given environment. Finds the app's directory (if any) and returns     a bool representing if the directory exists. Finds the file (if any) and returns a bool representing if the file exists. Finds any file in the apps directory (if any)     and returns a bool representing if any file exists. Finds all files in the apps directory (if any) and returns a list of them. Generates an encryption key for the encrypted file format.