Package generic_fns :: Module relax_re
[hide private]
[frames] | no frames]

Source Code for Module generic_fns.relax_re

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2003-2012 Edward d'Auvergne                                   # 
 4  #                                                                             # 
 5  # This file is part of the program relax.                                     # 
 6  #                                                                             # 
 7  # relax is free software; you can redistribute it and/or modify               # 
 8  # it under the terms of the GNU General Public License as published by        # 
 9  # the Free Software Foundation; either version 2 of the License, or           # 
10  # (at your option) any later version.                                         # 
11  #                                                                             # 
12  # relax is distributed in the hope that it will be useful,                    # 
13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
15  # GNU General Public License for more details.                                # 
16  #                                                                             # 
17  # You should have received a copy of the GNU General Public License           # 
18  # along with relax; if not, write to the Free Software                        # 
19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Module implementing relax regular expression.""" 
25   
26  # Python module imports. 
27  import re 
28  from string import replace 
29   
30   
31 -def search(pattern, id):
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 # Catch None. 53 if id == None: 54 return False 55 56 # Convert to a string. 57 id = str(id) 58 59 # If pattern is not a list, convert it to one. 60 if not isinstance(pattern, list): 61 patterns = [pattern] 62 else: 63 patterns = pattern 64 65 # Loop over the patterns. 66 for pattern in patterns: 67 # Force a conversion to str. 68 pattern = str(pattern) 69 70 # Quick string check. 71 if id == pattern: 72 return True 73 74 # First replace any '*' with '.*' (relax to re conversion). 75 pattern_re = replace(pattern, '*', '.*') 76 id_re = replace(id, '*', '.*') 77 78 # Bracket the pattern. 79 pattern_re = '^%s$' % pattern_re 80 id_re = '^%s$' % id_re 81 82 # String matches (both ways). 83 if re.search(pattern_re, id): 84 return True 85 if re.search(id_re, pattern): 86 return True 87 88 # No matches. 89 return False
90