mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Performance improvement/error catching in expr_match
`fnmatch.fnmatch()` does exact matches, so checking for an exact match is an unnecessary extra step. Additionally, while regex errors are caught, TypeErrors are not, so the use of non-string expressions/values will result in a traceback. This prevents those tracebacks.
This commit is contained in:
parent
0faced1d54
commit
dac42a672b
1 changed files with 15 additions and 6 deletions
|
@ -294,15 +294,24 @@ def build_whitespace_split_regex(text):
|
|||
|
||||
def expr_match(line, expr):
|
||||
'''
|
||||
Evaluate a line of text against an expression. First try a full-string
|
||||
match, next try globbing, and then try to match assuming expr is a regular
|
||||
Evaluate a line of text against an expression. Tries to match expr first as
|
||||
a glob using fnmatch.fnmatch(), and then tries to match expr as a regular
|
||||
expression. Originally designed to match minion IDs for
|
||||
whitelists/blacklists.
|
||||
|
||||
Note that this also does exact matches, as fnmatch.fnmatch() will return
|
||||
``True`` when no glob characters are used and the string is an exact match:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
>>> fnmatch.fnmatch('foo', 'foo')
|
||||
True
|
||||
'''
|
||||
if line == expr:
|
||||
return True
|
||||
if fnmatch.fnmatch(line, expr):
|
||||
return True
|
||||
try:
|
||||
if fnmatch.fnmatch(line, expr):
|
||||
return True
|
||||
except TypeError:
|
||||
pass
|
||||
try:
|
||||
if re.match(r'\A{0}\Z'.format(expr), line):
|
||||
return True
|
||||
|
|
Loading…
Add table
Reference in a new issue