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