changeset 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 8c4e92fb32d8
files upreckon/files.py upreckon/problem.py
diffstat 2 files changed, 27 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/upreckon/files.py	Thu Aug 18 02:20:24 2011 +0300
+++ b/upreckon/files.py	Thu Aug 18 02:41:46 2011 +0300
@@ -322,46 +322,31 @@
 			return File(virtual_path,
 			            os.path.join(self._external_path, filename),
 			            _has_tests=self._has_tests)
-
-class RegexpMatchFile(object):
-	__slots__ = 'virtual_path', 'real_path', 'hastests', 'archive'
 	
-	def __init__(self, virtual_path, real_path, hastests=False, archive=None):
-		self.virtual_path = virtual_path
-		self.real_path = real_path
-		self.hastests = hastests
-		self.archive = archive
-
-def regexp(pattern):
-	if not pattern:
-		yield RegexpMatchFile('', os.curdir)
-		return
-	dirname, basename = posixpath.split(pattern)
-	dirs = regexp(dirname)
-	reobj = re.compile(pattern + '$', re.UNICODE)
-	while dirs:
-		newdirs = []
-		for directory in dirs:
-			if directory.archive:
+	@classmethod
+	def regexp(cls, pattern):
+		if not pattern:
+			yield cls('', os.curdir)
+			return
+		dirname, basename = posixpath.split(pattern)
+		dirs = cls.regexp(dirname)
+		reobj = re.compile(pattern + '$', re.UNICODE)
+		while dirs:
+			newdirs = []
+			for directory in dirs:
 				try:
-					names = directory.archive.listdir(directory.real_path)
-				except KeyError:
-					continue
-				join = posixpath.join
-			else:
-				try:
-					names = os.listdir(directory.real_path)
-				except OSError:
+					names = directory.listdir()
+				except Exception:
 					continue
-				join = posixpath.join
-			for name in names:
-				path = join(directory.real_path, name)
-				vpath = posixpath.join(directory.virtual_path, name)
-				if re.match(reobj, vpath):
-					yield RegexpMatchFile(vpath, path, directory.hastests, directory.archive)
-				if not directory.hastests:
-					if name == 'tests':
-						newdirs.append(RegexpMatchFile(directory.virtual_path, path, True, directory.archive))
-					if not directory.archive and name in archives:
-						newdirs.append(RegexpMatchFile(directory.virtual_path, '', False, open_archive(path)))
-		dirs = newdirs
+				for name in names:
+					dir_entry = directory + name
+					if re.match(reobj, dir_entry.virtual_path):
+						yield dir_entry
+					if not directory._has_tests:
+						if name == 'tests':
+							dir_entry = directory.__add__(name, False)
+							dir_entry._has_tests = True
+							newdirs.append(dir_entry)
+						elif not directory.archive and name in archives:
+							newdirs.append(directory.__add__(name, False))
+			dirs = newdirs
--- a/upreckon/problem.py	Thu Aug 18 02:20:24 2011 +0300
+++ b/upreckon/problem.py	Thu Aug 18 02:41:46 2011 +0300
@@ -354,12 +354,12 @@
 	reobj = re.compile(pattern, re.UNICODE)
 	if not group:
 		ids = []
-		for f in files.regexp(pattern):
+		for f in files.File.regexp(pattern):
 			ids.append(re.match(reobj, f.virtual_path).group(1))
 		return natsorted(ids)
 	else:
 		ids = {}
-		for f in files.regexp(pattern):
+		for f in files.File.regexp(pattern):
 			m = re.match(reobj, f.virtual_path)
 			g = m.group(group)
 			ids.setdefault(g, [])