from typing import Tuple


def from_checksum_format(line_bytes: bytes) -> Tuple[str, str]:
    """
    Given one line of bytes from a checksums.md5 file,
    parses the line and returns the filepath and that file's recorded checksum

    We expect a line to look roughly like this:
    2eb067afb7ba4eefe89a0416ab16f688  provenance/metadata.yaml

    ...with checksum followed by relative filepath (excluding root UUID dir)

    Code adapted from qiime2/core/util.py
    """
    line = str(line_bytes, 'utf-8').rstrip('\n')
    parts = line.split('  ', 1)
    if len(parts) < 2:
        parts = line.split(' *', 1)

    checksum, filepath = parts

    if checksum[0] == '\\':
        chars = ''
        escape = False
        # Gross, but regular `.replace` will overlap with itself and
        # negative lookbehind in regex is *probably* harder than scanning
        for char in filepath:
            # 1) Escape next character
            if not escape and char == '\\':
                escape = True
                continue

            # 2) Handle escape sequence
            if escape:
                try:
                    chars += {'\\': '\\', 'n': '\n'}[char]
                except KeyError:
                    chars += '\\' + char  # Wasn't an escape after all
                escape = False
                continue

            # 3) Nothing interesting
            chars += char

        checksum = checksum[1:]
        filepath = chars

    return filepath, checksum