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