comparison upreckon/files.py @ 209:c03a8113685d

Rewrote files.regexp as files.File.regexp (a class method) I still dislike the iterative code though.
author Oleg Oshmyan <chortos@inbox.lv>
date Thu, 18 Aug 2011 02:41:46 +0300
parents ede78fbd509a
children 1cbe2c428942
comparison
equal deleted inserted replaced
208:ede78fbd509a 209:c03a8113685d
320 else: 320 else:
321 filename = filename.replace('.', os.path.extsep) 321 filename = filename.replace('.', os.path.extsep)
322 return File(virtual_path, 322 return File(virtual_path,
323 os.path.join(self._external_path, filename), 323 os.path.join(self._external_path, filename),
324 _has_tests=self._has_tests) 324 _has_tests=self._has_tests)
325 325
326 class RegexpMatchFile(object): 326 @classmethod
327 __slots__ = 'virtual_path', 'real_path', 'hastests', 'archive' 327 def regexp(cls, pattern):
328 328 if not pattern:
329 def __init__(self, virtual_path, real_path, hastests=False, archive=None): 329 yield cls('', os.curdir)
330 self.virtual_path = virtual_path 330 return
331 self.real_path = real_path 331 dirname, basename = posixpath.split(pattern)
332 self.hastests = hastests 332 dirs = cls.regexp(dirname)
333 self.archive = archive 333 reobj = re.compile(pattern + '$', re.UNICODE)
334 334 while dirs:
335 def regexp(pattern): 335 newdirs = []
336 if not pattern: 336 for directory in dirs:
337 yield RegexpMatchFile('', os.curdir)
338 return
339 dirname, basename = posixpath.split(pattern)
340 dirs = regexp(dirname)
341 reobj = re.compile(pattern + '$', re.UNICODE)
342 while dirs:
343 newdirs = []
344 for directory in dirs:
345 if directory.archive:
346 try: 337 try:
347 names = directory.archive.listdir(directory.real_path) 338 names = directory.listdir()
348 except KeyError: 339 except Exception:
349 continue 340 continue
350 join = posixpath.join 341 for name in names:
351 else: 342 dir_entry = directory + name
352 try: 343 if re.match(reobj, dir_entry.virtual_path):
353 names = os.listdir(directory.real_path) 344 yield dir_entry
354 except OSError: 345 if not directory._has_tests:
355 continue 346 if name == 'tests':
356 join = posixpath.join 347 dir_entry = directory.__add__(name, False)
357 for name in names: 348 dir_entry._has_tests = True
358 path = join(directory.real_path, name) 349 newdirs.append(dir_entry)
359 vpath = posixpath.join(directory.virtual_path, name) 350 elif not directory.archive and name in archives:
360 if re.match(reobj, vpath): 351 newdirs.append(directory.__add__(name, False))
361 yield RegexpMatchFile(vpath, path, directory.hastests, directory.archive) 352 dirs = newdirs
362 if not directory.hastests:
363 if name == 'tests':
364 newdirs.append(RegexpMatchFile(directory.virtual_path, path, True, directory.archive))
365 if not directory.archive and name in archives:
366 newdirs.append(RegexpMatchFile(directory.virtual_path, '', False, open_archive(path)))
367 dirs = newdirs