

def translate(pat):
    'Translate a shell-style pattern to a regular expression.\n\n    The pattern may include "**<sep>" (<sep> stands for the platform-specific path separator; "/" on POSIX systems) for\n    matching zero or more directory levels and "*" for matching zero or more arbitrary characters with the exception of\n    any path separator. Wrap meta-characters in brackets for a literal match (i.e. "[?]" to match the literal character\n    "?").\n\n    This function is derived from the "fnmatch" module distributed with the Python standard library.\n\n    Copyright (C) 2001-2016 Python Software Foundation. All rights reserved.\n\n    TODO: support {alt1,alt2} shell-style alternatives\n\n    '
    sep = os.path.sep
    n = len(pat)
    i = 0
    res = ''
    while (i < n):
        c = pat[i]
        i += 1
        if (c == '*'):
            if (((i + 1) < n) and (pat[i] == '*') and (pat[(i + 1)] == sep)):
                res += ('(?:[^\\%s]*\\%s)*' % (sep, sep))
                i += 2
            else:
                res += ('[^\\%s]*' % sep)
        elif (c == '?'):
            res += ('[^\\%s]' % sep)
        elif (c == '['):
            j = i
            if ((j < n) and (pat[j] == '!')):
                j += 1
            if ((j < n) and (pat[j] == ']')):
                j += 1
            while ((j < n) and (pat[j] != ']')):
                j += 1
            if (j >= n):
                res += '\\['
            else:
                stuff = pat[i:j].replace('\\', '\\\\')
                i = (j + 1)
                if (stuff[0] == '!'):
                    stuff = ('^' + stuff[1:])
                elif (stuff[0] == '^'):
                    stuff = ('\\' + stuff)
                res += ('[%s]' % stuff)
        else:
            res += re.escape(c)
    return (res + '\\Z(?ms)')

dataset/ETHPy150Open borgbackup/borg/borg/shellpattern.py translate/original