Skip to content

muxtools.utils.files

ensure_path

ensure_path(pathIn: PathLike | Sequence[PathLike] | GlobSearch | FileMixin, caller: Any) -> Path

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

Parameters:

Name Type Description Default
pathIn PathLike | Sequence[PathLike] | GlobSearch | FileMixin

Supposed passed Path

required
caller Any

Caller name used for the exception and error message

required
Source code in muxtools/utils/files.py
def ensure_path(pathIn: PathLike | Sequence[PathLike] | GlobSearch | FileMixin, 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:
        if isinstance(pathIn, FileMixin):
            pathIn = pathIn.file
        if isinstance(pathIn, GlobSearch):
            pathIn = pathIn.paths
        if isinstance(pathIn, Sequence) and not isinstance(pathIn, str):
            pathIn = pathIn[0]

        if not isinstance(pathIn, Path) and not isinstance(pathIn, str):
            raise crit("Path is not a Path object or a string!", caller)

        return Path(pathIn).resolve()

ensure_path_exists

ensure_path_exists(pathIn: PathLike | Sequence[PathLike] | GlobSearch | FileMixin, 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.

Parameters:

Name Type Description Default
pathIn PathLike | Sequence[PathLike] | GlobSearch | FileMixin

Supposed passed Path

required
caller Any

Caller name used for the exception and error message

required
Source code in muxtools/utils/files.py
def ensure_path_exists(pathIn: PathLike | Sequence[PathLike] | GlobSearch | FileMixin, 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
    """
    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

uniquify_path

uniquify_path(path: PathLike, *, reserved: Sequence[PathLike] = (), companion_extensions: Sequence[str] = ()) -> str

Extends path to avoid existing or reserved paths.

Parameters:

Name Type Description Default
path PathLike

Input path.

required
reserved Sequence[PathLike]

Additional paths that must not be reused.

()
companion_extensions Sequence[str]

Extensions whose companion paths must also be free.

()

Returns:

Type Description
str

Unique path.

Source code in muxtools/utils/files.py
def uniquify_path(path: PathLike, *, reserved: Sequence[PathLike] = (), companion_extensions: Sequence[str] = ()) -> str:
    """
    Extends path to avoid existing or reserved paths.

    :param path:                 Input path.
    :param reserved:             Additional paths that must not be reused.
    :param companion_extensions: Extensions whose companion paths must also be free.

    :return:                     Unique path.
    """

    path = str(ensure_path(path, None))
    reserved_paths = {ensure_path(reserved_path, None) for reserved_path in reserved}

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

    while True:
        candidate = Path(path)
        conflicts = candidate.exists() or candidate.resolve() in reserved_paths
        conflicts = conflicts or any(
            companion.exists() or companion.resolve() in reserved_paths for companion in (candidate.with_suffix(ext) for ext in companion_extensions)
        )
        if not conflicts:
            return str(candidate)

        path = filename + " (" + str(counter) + ")" + extension
        counter += 1

get_crc32

get_crc32(file: PathLike) -> str

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 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 ImportError:
        from binascii import crc32

    buffer_size = 1024 * 1024 * 32
    val = 0
    with open(ensure_path(file, get_crc32), "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