Commits

Michał Górny committed 19312dc

Introduce FlattenedToken-based global flag access API.

Instead of using the whole MakeConfVariable like a PackageEntry, iterate
over the flattened tokens, casting them into FlattenedToken class
(derived from PackageEntry).

Comments (0)

Files changed (2)

lib/flaggie/makeconf.py

 
 wsregex = re.compile('(?u)(\s+)')
 
-class MakeConfVariable(PackageFileSet.PackageFile.PackageEntry):
+class MakeConfVariable(object):
 	class MakeConfFlag(PackageFileSet.PackageFile.PackageEntry.PackageFlag):
 		def __init__(self, s, lta = []):
 			PackageFileSet.PackageFile.PackageEntry.PackageFlag.__init__( \
 			else:
 				raise NotImplementedError('Disabling modified for PartialFlag is not supported.')
 
+	class FlattenedToken(PackageFileSet.PackageFile.PackageEntry):
+		def __init__(self, token):
+			self._token = token
+			token.flags = []
+
+		@property
+		def data(self):
+			return self._token.data
+
+		@property
+		def modified(self):
+			return self._token.modified
+
+		@modified.setter
+		def modified(self, val):
+			self._token.modified = val
+
+		@property
+		def flags(self):
+			return self._token.flags
+
+		def __iter__(self):
+			""" Iterate over all flags in the entry. """
+			for f in reversed(self.flags):
+				if isinstance(f, MakeConfVariable.MakeConfFlag):
+					yield f
+
 	def __init__(self, key, tokens):
 		def flattentokens(l):
 			out = []
 				if isinstance(t, MakeConfVariable):
 					out.extend(flattentokens(t._tokens))
 				else:
-					out.append(t)
+					out.append(self.FlattenedToken(t))
 			return out
 
 		self._key = key
 					sl = nsl
 					nt = None
 				else:
-					t.flags = []
 					sl = wsregex.split(t.data)
 					# 'flag1 flag2' -> flag1, ' ', flag2
 					# ' flag1 flag2' -> '', ' ', flag1, ' ', flag2
 							break
 						else:
 							nsl = wsregex.split(nt.data)
-							nt.flags = []
 							if len(nsl) == 1 and not nsl[0]:
 								pass
 							elif not nsl[0]: # the whitespace we were hoping for
 		self.parseflags()
 
 		for t in reversed(self._flattokens):
-			for i, f in enumerate(reversed(t.flags)):
-				if isinstance(f, self.MakeConfFlag):
-					yield f
+			yield t
 
 	def __delitem__(self, flag):
 		""" Remove all occurences of a flag. """
 		self.parseflags()
 
-		for t in self._flattokens:
+		for t in self:
 			flags = []
 			wasflag = False
 			for f in t.flags:
 
 			@property
 			def modified(self):
-				if self._modified:
-					return True
-				elif hasattr(self, 'flags'):
-					for f in self.flags:
-						if f.modified:
-							return True
-				return False
+				return self._modified
 
 			@modified.setter
 			def modified(self, val):

lib/flaggie/packagefile.py

 
 		if pkg is None:
 			if self.makeconfvar is not None:
-				yield self.makeconfvar
+				for e in self.makeconfvar:
+					yield e
 			else:
 				raise KeyError('PackageFileSet[None] requested but no MakeConf assigned.')
 			return