import pathlib
import typing


def _normalize_path(pth: pathlib.Path, suffix: typing.Optional[str] = None) -> str:
    """
    Produce a string version of pth replacing / with __ to produce a legal filename

    Replace suffix at the end if specified
    """
    if suffix is not None:
        pth = pth.with_suffix(suffix)

    s = str(pth)
    return s.strip("/").replace("/", "__")