Skip to content

files

ensure_path(pathIn, caller)

Utility function for other functions to make sure a path was passed to them.

Parameters:

Name Type Description Default
pathIn PathLike

Supposed passed Path

required
caller Any

Caller name used for the exception and error message

required
Source code in .venv/lib/python3.11/site-packages/muxtools/utils/files.py
def ensure_path(pathIn: PathLike, caller: Any) -> Path:
    """
    Utility function for other functions to make sure a path was passed to them.

    :param pathIn:      Supposed passed Path
    :param caller:      Caller name used for the exception and error message
    """
    if pathIn is None:
        raise crit("Path cannot be None.", caller)
    else:
        return Path(pathIn).resolve()

ensure_path_exists(pathIn, caller, allow_dir=False)

Utility function for other functions to make sure a path was passed to them and that it exists.

Parameters:

Name Type Description Default
pathIn PathLike | list[PathLike] | GlobSearch

Supposed passed Path

required
caller Any

Caller name used for the exception and error message

required
Source code in .venv/lib/python3.11/site-packages/muxtools/utils/files.py
def ensure_path_exists(pathIn: PathLike | list[PathLike] | GlobSearch, caller: Any, allow_dir: bool = False) -> Path:
    """
    Utility function for other functions to make sure a path was passed to them and that it exists.

    :param pathIn:      Supposed passed Path
    :param caller:      Caller name used for the exception and error message
    """
    from ..muxing.muxfiles import MuxingFile

    if isinstance(pathIn, MuxingFile):
        return ensure_path_exists(pathIn.file, caller)
    if isinstance(pathIn, GlobSearch):
        pathIn = pathIn.paths
    if isinstance(pathIn, list):
        pathIn = pathIn[0]
    path = ensure_path(pathIn, caller)
    if not path.exists():
        raise crit(f"Path target '{path}' does not exist.", caller)
    if not allow_dir and path.is_dir():
        raise crit("Path cannot be a directory.", caller)
    return path

get_crc32(file)

Generates crc32 checksum for file

Parameters:

Name Type Description Default
file PathLike

Input file

required

Returns:

Type Description
str

Checksum for file

Source code in .venv/lib/python3.11/site-packages/muxtools/utils/files.py
def get_crc32(file: PathLike) -> str:
    """
    Generates crc32 checksum for file

    :param file:        Input file

    :return:            Checksum for file
    """
    try:
        from zlib import crc32
    except:
        from binascii import crc32

    buffer_size = 1024 * 1024 * 32
    val = 0
    with open(file, "rb") as f:
        buffer = f.read(buffer_size)
        while len(buffer) > 0:
            val = crc32(buffer, val)
            buffer = f.read(buffer_size)

    val = val & 0xFFFFFFFF
    return "%08X" % val

uniquify_path(path)

Extends path to not conflict with existing files

Parameters:

Name Type Description Default
file

Input file

required

Returns:

Type Description
str

Unique path

Source code in .venv/lib/python3.11/site-packages/muxtools/utils/files.py
def uniquify_path(path: PathLike) -> str:
    """
    Extends path to not conflict with existing files

    :param file:        Input file

    :return:            Unique path
    """

    if isinstance(path, Path):
        path = str(path.resolve())

    filename, extension = os.path.splitext(path)
    counter = 1

    while os.path.exists(path):
        path = filename + " (" + str(counter) + ")" + extension
        counter += 1

    return path