Skip to content

main

main

LogLevel

Bases: str, Enum

Logical log levels for the CLI.

Includes a custom TRACE (more verbose than DEBUG) and QUIET (suppresses all output beyond CRITICAL).

Source code in source/src/defines.py
class LogLevel(str, Enum):
    """
    Logical log levels for the CLI.

    Includes a custom TRACE (more verbose than DEBUG) and QUIET
    (suppresses all output beyond CRITICAL).
    """

    TRACE = "trace"
    DEBUG = "debug"
    INFO = "info"
    WARNING = "warning"
    ERROR = "error"
    QUIET = "quiet"

    @classmethod
    def parse(cls, value: Optional[str]) -> Optional["LogLevel"]:
        """Parse case-insensitively; returns None if value is falsy."""
        print("Executing function parse from LogLevel")
        if not value:
            return None
        norm = value.strip().lower()
        try:
            return cls(norm)
        except ValueError as exc:
            valid = ", ".join(v.value for v in cls)
            raise ValueError(f"Unknown log level '{value}'. Valid: {valid}") from exc

    @classmethod
    def get_default_log_level(cls) -> "LogLevel":
        return LogLevel.TRACE

    def to_logging_level(self) -> int:
        if self is LogLevel.TRACE:
            return 0
        if self is LogLevel.DEBUG:
            return logging.DEBUG
        if self is LogLevel.INFO:
            return logging.INFO
        if self is LogLevel.WARNING:
            return logging.WARNING
        if self is LogLevel.ERROR:
            return logging.ERROR
        if self is LogLevel.QUIET:
            return logging.CRITICAL + 10
        # Fallback
        return LogLevel.get_default_log_level().to_logging_level()

parse classmethod

parse(value: Optional[str]) -> Optional[LogLevel]

Parse case-insensitively; returns None if value is falsy.

Source code in source/src/defines.py
@classmethod
def parse(cls, value: Optional[str]) -> Optional["LogLevel"]:
    """Parse case-insensitively; returns None if value is falsy."""
    print("Executing function parse from LogLevel")
    if not value:
        return None
    norm = value.strip().lower()
    try:
        return cls(norm)
    except ValueError as exc:
        valid = ", ".join(v.value for v in cls)
        raise ValueError(f"Unknown log level '{value}'. Valid: {valid}") from exc

read_env_var

read_env_var(var_name)

Reads an environment variable.

Parameters:

Name Type Description Default
var_name str

Name of the environment variable.

required

Returns:

Name Type Description
str

The value of the environment variable if valid.

Raises:

Type Description
KeyError

If the environment variable does not exist.

ValueError

If the environment variable is empty or contains only whitespace.

Source code in source/src/filesystem.py
def read_env_var(var_name):
    """
    Reads an environment variable.

    Args:
        var_name (str): Name of the environment variable.

    Returns:
        str: The value of the environment variable if valid.

    Raises:
        KeyError: If the environment variable does not exist.
        ValueError: If the environment variable is empty or contains only whitespace.
    """
    # Check if the environment variable exists
    if var_name not in os.environ:
        raise KeyError(f"The environment variable '{var_name}' does not exist.")

    # Read the value
    value = os.environ[var_name]

    # Check if the value is empty
    if not value:
        raise ValueError(f"The environment variable '{var_name}' is empty.")

    return value

read_file

read_file(file_path)

Reads a file and returns its content. Handles edge cases such as the file not existing or being unreadable.

Parameters:

Name Type Description Default
file_path str

Path to the token file.

required

Returns:

Name Type Description
str

The content of the file.

Raises:

Type Description
FileNotFoundError

If the file does not exist.

PermissionError

If the file cannot be read due to permission issues.

Source code in source/src/filesystem.py
def read_file(file_path):
    """
    Reads a file and returns its content.
    Handles edge cases such as the file not existing or being unreadable.

    Args:
        file_path (str): Path to the token file.

    Returns:
        str: The content of the file.

    Raises:
        FileNotFoundError: If the file does not exist.
        PermissionError: If the file cannot be read due to permission issues.
    """
    # Check if the file exists
    if not os.path.exists(file_path):
        raise FileNotFoundError(f"The file '{file_path}' does not exist.")

    # Check if the file is readable
    if not os.access(file_path, os.R_OK):
        raise PermissionError(f"The file '{file_path}' cannot be read. Check permissions.")

    # Read the file
    with open(file_path, "r") as file:
        content = file.read()

    return content

list_dir

list_dir(input_folder)

Returns a list of all file names in the ./input/salaries directory.

Source code in source/src/filesystem.py
def list_dir(input_folder):
    """Returns a list of all file names in the ./input/salaries directory."""
    # Ensure the input_folder is a valid directory
    if not os.path.isdir(input_folder):
        raise ValueError("input folder " + input_folder + " in list_files function is not a directory or can't be accessed")

    # List all files in the directory
    file_names = [os.path.basename(file) for file in os.listdir(input_folder)]

    return file_names

remove_folder

remove_folder(folder_path)

Remove the folder at the given path if it exists. Do nothing if it doesn't.

Source code in source/src/filesystem.py
def remove_folder(folder_path):
    """Remove the folder at the given path if it exists. Do nothing if it doesn't."""
    try:
        shutil.rmtree(folder_path)
    except FileNotFoundError:
        pass  # Do nothing if the folder does not exist
    except Exception as e:
        print(f"Error removing folder {folder_path}: {e}")