import re
import fnmatch


def exclusion_filter(exclude: str):
    """Converts a filter string "*.abc;*.def" to a function that can be passed to pack().

    If 'exclude' is None or an empty string, returns None (which means "no filtering").
    """

    if not exclude:
        return None


    # convert string into regex callback that operates on bytes
    # "*.txt;*.png;*.rst" --> rb".*\.txt$|.*\.png$|.*\.rst$"
    pattern = b'|'.join(fnmatch.translate(f).encode('utf-8')
                        for f in exclude.split(';')
                        if f)
    compiled_pattern = re.compile(pattern, re.IGNORECASE)

    def filename_filter(fname: bytes):
        return not compiled_pattern.match(fname)

    return filename_filter