Handle glob -> regex errors

This commit is contained in:
Erik Johnston 2016-01-19 14:43:24 +00:00
parent 7a079adc8f
commit d056a0a3d8

View File

@ -258,40 +258,44 @@ def _glob_matches(glob, value, word_boundary=False):
Returns: Returns:
bool bool
""" """
if IS_GLOB.search(glob): try:
r = re.escape(glob) if IS_GLOB.search(glob):
r = re.escape(glob)
r = r.replace(r'\*', '.*?') r = r.replace(r'\*', '.*?')
r = r.replace(r'\?', '.') r = r.replace(r'\?', '.')
# handle [abc], [a-z] and [!a-z] style ranges. # handle [abc], [a-z] and [!a-z] style ranges.
r = GLOB_REGEX.sub( r = GLOB_REGEX.sub(
lambda x: ( lambda x: (
'[%s%s]' % ( '[%s%s]' % (
x.group(1) and '^' or '', x.group(1) and '^' or '',
x.group(2).replace(r'\\\-', '-') x.group(2).replace(r'\\\-', '-')
) )
), ),
r, r,
) )
if word_boundary: if word_boundary:
r = r"\b%s\b" % (r,)
r = re.compile(r, flags=re.IGNORECASE)
return r.search(value)
else:
r = r + "$"
r = re.compile(r, flags=re.IGNORECASE)
return r.match(value)
elif word_boundary:
r = re.escape(glob)
r = r"\b%s\b" % (r,) r = r"\b%s\b" % (r,)
r = re.compile(r, flags=re.IGNORECASE) r = re.compile(r, flags=re.IGNORECASE)
return r.search(value) return r.search(value)
else: else:
r = r + "$" return value.lower() == glob.lower()
r = re.compile(r, flags=re.IGNORECASE) except re.error:
logger.warn("Failed to parse glob to regex: %r", glob)
return r.match(value) return False
elif word_boundary:
r = re.escape(glob)
r = r"\b%s\b" % (r,)
r = re.compile(r, flags=re.IGNORECASE)
return r.search(value)
else:
return value.lower() == glob.lower()
def _flatten_dict(d, prefix=[], result={}): def _flatten_dict(d, prefix=[], result={}):