179
179
so are matched first, then the basename patterns, then the fullpath
185
182
# We want to _add_patterns in a specific order (as per type_list below)
186
183
# starting with the shortest and going to the longest.
187
184
# As some Python version don't support ordered dicts the list below is
188
185
# used to select inputs for _add_pattern in a specific order.
189
type_list = [ TYPE_EXTENSION, TYPE_BASENAME, TYPE_FULLPATH ]
193
TYPE_FULLPATH : _sub_fullpath,
194
TYPE_BASENAME : _sub_basename,
195
TYPE_EXTENSION : _sub_extension,
198
# Prefixes used to combine various patterns.
199
# See: Globster._add_patterns
202
TYPE_BASENAME : r'(?:.*/)?(?!.*/)',
203
TYPE_EXTENSION : r'(?:.*/)?(?!.*/)(?:.*\.)',
186
pattern_types = [ "extension", "basename", "fullpath" ]
190
"translator" : _sub_extension,
191
"prefix" : r'(?:.*/)?(?!.*/)(?:.*\.)'
194
"translator" : _sub_basename,
195
"prefix" : r'(?:.*/)?(?!.*/)'
198
"translator" : _sub_fullpath,
206
203
def __init__(self, patterns):
207
204
self._regex_patterns = []
208
205
pattern_lists = {
209
Globster.TYPE_FULLPATH : [],
210
Globster.TYPE_EXTENSION : [],
211
Globster.TYPE_BASENAME : [],
213
210
for pat in patterns:
214
211
pat = normalize_pattern(pat)
215
212
pattern_lists[Globster.identify(pat)].append(pat)
216
for t in Globster.type_list:
217
self._add_patterns(pattern_lists[t], Globster.translators[t],
218
Globster.prefixes[t])
213
pi = Globster.pattern_info
214
for t in Globster.pattern_types:
215
self._add_patterns(pattern_lists[t], pi[t]["translator"],
220
218
def _add_patterns(self, patterns, translator, prefix=''):
259
257
and returns the appropriate type.
261
259
if pattern.startswith(u'RE:') or u'/' in pattern:
262
return Globster.TYPE_FULLPATH
263
261
elif pattern.startswith(u'*.'):
264
return Globster.TYPE_EXTENSION
266
return Globster.TYPE_BASENAME
269
267
def is_pattern_valid(pattern):
274
272
see: globbing.normalize_pattern
277
translator = Globster.translators[Globster.identify(pattern)]
275
translator = Globster.pattern_info[Globster.identify(pattern)]["translator"]
278
276
tpattern = '(%s)' % translator(pattern)
280
278
re_obj = re.compile(tpattern, re.UNICODE)
331
329
self._regex_patterns = []
332
330
for pat in patterns:
333
331
pat = normalize_pattern(pat)
334
pat_type = Globster.identify(pat)
335
self._add_patterns([pat], Globster.translators[pat_type],
336
Globster.prefixes[pat_type])
332
t = Globster.identify(pat)
333
self._add_patterns([pat], Globster.pattern_info[t]["translator"],
334
Globster.pattern_info[t]["prefix"])
339
337
_slashes = re.compile(r'[\\/]+')