Skip to content

Instantly share code, notes, and snippets.

@bstaletic
Created January 30, 2019 12:00
Show Gist options
  • Save bstaletic/b627f4dac2e64899f5a3806d1e84132f to your computer and use it in GitHub Desktop.
Save bstaletic/b627f4dac2e64899f5a3806d1e84132f to your computer and use it in GitHub Desktop.
trasform a vim wildignore value into a list of python regexes
import os, sys
def OnWindows():
return sys.platform == 'win32'
def Parser( wildignore ):
regex_list = []
skip_next = False
saw_backslash = False
for pattern in wildignore.split( ',' ):
regex = ''
for i, c in enumerate( pattern ):
if skip_next:
skip_next = False
continue
if saw_backslash:
saw_backslash = False
# If literal character is special we need to escape it
if c == '*' or c == '?' or c == '[':
regex += '\\'
regex += c
continue
# wildignore doesn't treat \ as an escape character on Windows
if c == '\\' and not OnWindows():
saw_backslash = True
continue
if c == '*':
if i + 1 < len( pattern ) and pattern[ i + 1 ] == '*':
# ** found
regex += '.*'
skip_next = True
else:
# * found
regex += '[^' + os.path.sep
if os.path.altsep:
regex += os.path.altsep
regex += ']'
continue
if c == '?':
regex += '.'
continue
regex += c
regex_list.append( regex )
return regex_list
if __name__ == '__main__':
print( Parser( '\\asd\\*asd,a*s\\?*,\\[asd]*?**' ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment