1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 """Module implementing relax regular expression."""
25
26
27 import re
28 from string import replace
29
30
32 """Determine if id matches the pattern, or vice versa, allowing for regular expressions.
33
34 This method converts from relax's RE syntax to that of the re python module.
35
36 The changes include:
37
38 1. All '*' to '.*'.
39 2. The identifier is bracketed, '^' is added to the start and '$' to the end.
40
41 After conversion of both the string and patterns, the comparison is then performed both ways from the converted string matching the original string (using re.search()).
42
43
44 @param pattern: The pattern to match the string to. This can be a list of patterns. All elements will be converted to strings, so the pattern or list can consist of anything.
45 @type pattern: anything
46 @param id: The identification object.
47 @type id: None, str, or number
48 @return: True if there is a match, False otherwise.
49 @rtype: bool
50 """
51
52
53 if id == None:
54 return False
55
56
57 id = str(id)
58
59
60 if not isinstance(pattern, list):
61 patterns = [pattern]
62 else:
63 patterns = pattern
64
65
66 for pattern in patterns:
67
68 pattern = str(pattern)
69
70
71 if id == pattern:
72 return True
73
74
75 pattern_re = replace(pattern, '*', '.*')
76 id_re = replace(id, '*', '.*')
77
78
79 pattern_re = '^%s$' % pattern_re
80 id_re = '^%s$' % id_re
81
82
83 if re.search(pattern_re, id):
84 return True
85 if re.search(id_re, pattern):
86 return True
87
88
89 return False
90