Robot Framework
match.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import re
17 import fnmatch
18 from functools import partial
19 
20 from .normalizing import normalize
21 from .robottypes import is_string
22 
23 
24 def eq(str1, str2, ignore=(), caseless=True, spaceless=True):
25  str1 = normalize(str1, ignore, caseless, spaceless)
26  str2 = normalize(str2, ignore, caseless, spaceless)
27  return str1 == str2
28 
29 
30 class Matcher:
31 
32  def __init__(self, pattern, ignore=(), caseless=True, spaceless=True, regexp=False):
33  self.patternpattern = pattern
34  self._normalize_normalize = partial(normalize, ignore=ignore, caseless=caseless,
35  spaceless=spaceless)
36  self._regexp_regexp = self._compile_compile(self._normalize_normalize(pattern), regexp=regexp)
37 
38  def _compile(self, pattern, regexp=False):
39  if not regexp:
40  pattern = fnmatch.translate(pattern)
41  return re.compile(pattern, re.DOTALL)
42 
43  def match(self, string):
44  return self._regexp_regexp.match(self._normalize_normalize(string)) is not None
45 
46  def match_any(self, strings):
47  return any(self.matchmatch(s) for s in strings)
48 
49  def __bool__(self):
50  return bool(self._normalize_normalize(self.patternpattern))
51 
52 
54 
55  def __init__(self, patterns=None, ignore=(), caseless=True, spaceless=True,
56  match_if_no_patterns=False, regexp=False):
57  self._matchers_matchers = [Matcher(pattern, ignore, caseless, spaceless, regexp)
58  for pattern in self._ensure_list_ensure_list(patterns)]
59  self._match_if_no_patterns_match_if_no_patterns = match_if_no_patterns
60 
61  def _ensure_list(self, patterns):
62  if patterns is None:
63  return []
64  if is_string(patterns):
65  return [patterns]
66  return patterns
67 
68  def match(self, string):
69  if self._matchers_matchers:
70  return any(m.match(string) for m in self._matchers_matchers)
71  return self._match_if_no_patterns_match_if_no_patterns
72 
73  def match_any(self, strings):
74  return any(self.matchmatch(s) for s in strings)
75 
76  def __len__(self):
77  return len(self._matchers_matchers)
78 
79  def __iter__(self):
80  for matcher in self._matchers_matchers:
81  yield matcher.pattern
def match(self, string)
Definition: match.py:43
def _compile(self, pattern, regexp=False)
Definition: match.py:38
def match_any(self, strings)
Definition: match.py:46
def __bool__(self)
Definition: match.py:49
def __init__(self, pattern, ignore=(), caseless=True, spaceless=True, regexp=False)
Definition: match.py:32
def match(self, string)
Definition: match.py:68
def match_any(self, strings)
Definition: match.py:73
def __init__(self, patterns=None, ignore=(), caseless=True, spaceless=True, match_if_no_patterns=False, regexp=False)
Definition: match.py:56
def _ensure_list(self, patterns)
Definition: match.py:61
def eq(str1, str2, ignore=(), caseless=True, spaceless=True)
Definition: match.py:24
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:27