Revision bbfed756 lib/utils/text.py

b/lib/utils/text.py
74 74
  if not case_sensitive:
75 75
    re_flags |= re.IGNORECASE
76 76
    key = key.upper()
77
  mo = re.compile("^%s(\..*)?$" % re.escape(key), re_flags)
77

  
78
  name_re = re.compile(r"^%s(\..*)?$" % re.escape(key), re_flags)
79

  
78 80
  names_filtered = []
79 81
  string_matches = []
80 82
  for name in name_list:
81
    if mo.match(name) is not None:
83
    if name_re.match(name) is not None:
82 84
      names_filtered.append(name)
83 85
      if not case_sensitive and key == name.upper():
84 86
        string_matches.append(name)
......
87 89
    return string_matches[0]
88 90
  if len(names_filtered) == 1:
89 91
    return names_filtered[0]
92

  
90 93
  return None
91 94

  
92 95

  
96
def _DnsNameGlobHelper(match):
97
  """Helper function for L{DnsNameGlobPattern}.
98

  
99
  Returns regular expression pattern for parts of the pattern.
100

  
101
  """
102
  text = match.group(0)
103

  
104
  if text == "*":
105
    return "[^.]*"
106
  elif text == "?":
107
    return "[^.]"
108
  else:
109
    return re.escape(text)
110

  
111

  
112
def DnsNameGlobPattern(pattern):
113
  """Generates regular expression from DNS name globbing pattern.
114

  
115
  A DNS name globbing pattern (e.g. C{*.site}) is converted to a regular
116
  expression. Escape sequences or ranges (e.g. [a-z]) are not supported.
117

  
118
  Matching always starts at the leftmost part. An asterisk (*) matches all
119
  characters except the dot (.) separating DNS name parts. A question mark (?)
120
  matches a single character except the dot (.).
121

  
122
  @type pattern: string
123
  @param pattern: DNS name globbing pattern
124
  @rtype: string
125
  @return: Regular expression
126

  
127
  """
128
  return r"^%s(\..*)?$" % re.sub(r"\*|\?|[^*?]*", _DnsNameGlobHelper, pattern)
129

  
130

  
93 131
def FormatUnit(value, units):
94 132
  """Formats an incoming number of MiB with the appropriate unit.
95 133

  

Also available in: Unified diff