Skip to content

muxtools.helpers.helper_util

replace_crc

replace_crc(file: Path, caller: Any | None = None) -> Path

Replace existing CRC32 in filename if any.

Parameters:

Name Type Description Default
file Path

File to replace the CRC in

required

Returns:

Type Description
Path

The new Path to the file

Source code in muxtools/helpers/helper_util.py
def replace_crc(file: Path, caller: Any | None = None) -> Path:
    """
    Replace existing CRC32 in filename if any.

    :param file:        File to replace the CRC in

    :return:            The new Path to the file
    """
    match = re.search(CRC_REGEX, file.name)
    if not match:
        return file

    debug(f"Generating new CRC-32 for file '{file.name}'...", caller)
    new = get_crc32(file).upper()

    if new == match.group(1):
        debug("New crc matches input!", caller)
        return file

    return file.rename(file.with_name(file.name.replace(match.group(1), new)))