Skip to content

muxtools.muxing.token_handling

replace_with_temp_tokens

replace_with_temp_tokens(string: str) -> str

Replace regular $ tokens with # tokens to be able to be used in filenames temporarily.

Source code in muxtools/muxing/token_handling.py
def replace_with_temp_tokens(string: str) -> str:
    """
    Replace regular $ tokens with # tokens to be able to be used in filenames temporarily.
    """
    new = string
    for match in re.finditer(TOKEN_REGEX, string):
        new = new.replace(match.group(0), f"#{match.group(0)[1:-1]}#")
    return new

apply_dynamic_tokens

apply_dynamic_tokens(string: str, token_source: PathLike | ParsedFile, is_global: bool, user_lang: str | None = None, caller: Any | None = None) -> str

Apply dynamic tokens to naming strings based on a file and/or user selected language (for raw tracks).

Parameters:

Name Type Description Default
string str

The string to manipulate

required
token_source PathLike | ParsedFile

The file to read metadata from

required
is_global bool

Should be true if this string is meant to be used for a filename and False for tracks

required
user_lang str | None

The language to be used if there is none or if it should be overridden

None
caller Any | None

The object to be used for the logging, mostly for internal usage

None

Returns:

Type Description
str

The modified string if anything was there to modify, otherwise the untouched input.

Source code in muxtools/muxing/token_handling.py
def apply_dynamic_tokens(
    string: str, token_source: PathLike | ParsedFile, is_global: bool, user_lang: str | None = None, caller: Any | None = None
) -> str:
    """
    Apply dynamic tokens to naming strings based on a file and/or user selected language (for raw tracks).

    :param string:          The string to manipulate
    :param token_source:    The file to read metadata from
    :param is_global:       Should be true if this string is meant to be used for a filename and False for tracks
    :param user_lang:       The language to be used if there is none or if it should be overridden
    :param caller:          The object to be used for the logging, mostly for internal usage

    :return:                The modified string if anything was there to modify, otherwise the untouched input.
    """
    caller = caller or apply_dynamic_tokens

    matches = list(re.finditer(TOKEN_REGEX, string))
    if not matches:
        return string

    source = token_source if isinstance(token_source, ParsedFile) else ParsedFile.from_file(token_source, caller)
    new = string

    for match in matches:
        groups = match.groupdict()

        if is_global and not groups["type"]:
            if groups["prop"].lower() in ["res", "resolution"]:
                groups["type"] = "v"
            elif groups["prop"].lower() in ["ch", "channels"]:
                groups["type"] = "a"
            else:
                raise error(f"Ambiguous token '{match.group(0)}' used. Please specify a track type!", caller)

        tracktype = _matching_track_type(groups["type"])
        if groups["lang"]:
            relative_id = None
        else:
            relative_id = 0 if groups["num"] is None else int(groups["num"])

        post_filter_index = 0 if groups["num2"] is None else int(groups["num2"])

        track = source.find_tracks(
            type=tracktype,
            lang=groups["lang"],
            relative_id=relative_id,
            error_if_empty=True,
            caller=caller,
        )[post_filter_index]

        replacement = _get_prop(str(groups["prop"]), track, user_lang, match.group(0), caller)
        new = new.replace(match.group(0), replacement)

    return new