Commits

Michał Górny committed f8df069

Migrate package keys to new StringCompat (mostly).

There are still a few test failures which I need to take a closer look
at.

  • Participants
  • Parent commits 36f21ba

Comments (0)

Files changed (7)

File gentoopm/basepm/atom.py

 
 from abc import abstractmethod, abstractproperty
 
-from ..util import ABCObject, StringCompat2, StringifiedComparisons, \
-		FillMissingComparisons
+from ..util import ABCObject, StringCompat, StringifiedComparisons, \
+		FillMissingComparisons, StringCompat2
 
-class PMPackageKey(ABCObject, StringCompat2, FillMissingComparisons):
+class PMPackageKey(ABCObject, StringCompat):
 	"""
 	A base class for a package key (CP/qualified package name).
 	"""
 
+	def __new__(self, key):
+		"""
+		Instantiate.
+
+		@param key: complete package key
+		@type key: string
+		"""
+		return StringCompat.__new__(self, key)
+
 	@abstractproperty
 	def category(self):
 		"""
 		"""
 		pass
 
-	@abstractmethod
-	def __str__(self):
-		"""
-		Return the stringified package key.
-
-		@return: Stringified package key.
-		@rtype: string
-		"""
-		pass
-
-	def __lt__(self, other):
-		return str(self) < str(other)
-
 class PMIncompletePackageKey(PMPackageKey):
 	"""
 	An incomplete package key (without a category).
 	def category(self):
 		return None
 
-	def __str__(self):
-		return self.package
+	@property
+	def package(self):
+		return str(self)
 
 class PMPackageVersion(ABCObject, StringCompat2):
 	"""

File gentoopm/paludispm/atom.py

 _category_wildcard_re = re.compile(r'\w')
 
 class PaludisPackageKey(PMPackageKey):
-	def __init__(self, key):
-		self._k = key
+	def __new__(self, key):
+		k = PMPackageKey.__new__(self, str(key))
+		k._k = key
+		return k
 
 	@property
 	def category(self):
 	def package(self):
 		return str(self._k.package)
 
-	def __str__(self):
-		return str(self._k)
-
 class PaludisIncompletePackageKey(PMIncompletePackageKey):
-	def __init__(self, key):
-		self._k = key
-
-	@property
-	def package(self):
-		return str(self._k)
+	def __new__(self, key):
+		return PMIncompletePackageKey.__new__(self, str(key))
 
 class PaludisPackageVersion(PMPackageVersion):
 	def __init__(self, ver):

File gentoopm/paludispm/pkg.py

 from .depend import PaludisPackageDepSet
 
 class PaludisBoundPackageKey(PaludisPackageKey, PMBoundPackageKey):
-	def __init__(self, key, pkg):
-		PaludisPackageKey.__init__(self, key)
-		self._state = PMPackageState(
+	def __new__(self, key, pkg):
+		k = PaludisPackageKey.__new__(self, key)
+		k._state = PMPackageState(
 				installable = isinstance(pkg, PaludisInstallableID),
 				installed = isinstance(pkg, PaludisInstalledID))
+		return k
 
 	@property
 	def state(self):

File gentoopm/pkgcorepm/atom.py

 		return None
 
 class PkgCorePackageKey(PMPackageKey):
-	def __init__(self, atom):
-		self._atom = atom
+	def __new__(self, atom):
+		k = PMPackageKey.__new__(self, atom.key)
+		k._atom = atom
+		return k
 
 	@property
 	def category(self):
 	def package(self):
 		return self._atom.package
 
-	def __str__(self):
-		return self._atom.key
-
 class PkgCoreIncompletePackageKey(PMIncompletePackageKey):
-	def __init__(self, r):
-		self._r = _find_res(r, PackageDep)
-		if self._r is None:
+	def __new__(self, r):
+		pd = _find_res(r, PackageDep)
+		if pd is None:
 			raise AssertionError('No PackageDep in restrictions.')
-
-	@property
-	def package(self):
-		return self._r.restriction.exact
+		return PMIncompletePackageKey.__new__(self, pd.restriction.exact)
 
 class PkgCorePackageVersion(PMPackageVersion):
 	def __init__(self, atom):

File gentoopm/portagepm/atom.py

 from ..exceptions import InvalidAtomStringError
 
 class PortagePackageKey(PMPackageKey):
-	def __init__(self, cp):
-		self._cp = cp
-
 	@property
 	def category(self):
-		return catsplit(self._cp)[0]
+		return catsplit(self)[0]
 
 	@property
 	def package(self):
-		return catsplit(self._cp)[1]
-
-	def __str__(self):
-		return self._cp
+		return catsplit(self)[1]
 
 class PortageIncompletePackageKey(PMIncompletePackageKey, PortagePackageKey):
 	pass

File gentoopm/portagepm/pkg.py

 	pass
 
 class PortageBoundPackageKey(PortagePackageKey, PMBoundPackageKey):
-	def __init__(self, cp, pkg):
-		PortagePackageKey.__init__(self, cp)
-		self._state = PMPackageState(
+	def __new__(self, cp, pkg):
+		k = PortagePackageKey.__new__(self, cp)
+		k._state = PMPackageState(
 				installable = isinstance(pkg, PortageCPV),
 				installed = isinstance(pkg, PortageVDBCPV))
+		return k
 
 	@property
 	def state(self):

File gentoopm/util.py

 	def __nonzero__(self):
 		return self.__bool__()
 
+class StringCompat(str):
+	"""
+	A helper class to create objects inheriting from string. It is basically
+	like subclassing str directly but with a nice C{repr()}.
+	"""
+
+	def __repr__(self):
+		return '%s(%s)' % (self.__class__.__name__, repr(str(self)))
+
 class StringCompat2(StringifiedComparisons):
 	"""
 	A helper class providing objects with compatibility string functions,