1. Michał Górny
  2. gentoopm

Commits

Michał Górny  committed 120c2ec

Provide a str compat for string-like properties.

  • Participants
  • Parent commits 05136dd
  • Branches master

Comments (0)

Files changed (3)

File gentoopm/basepm/atom.py

View file
  • Ignore whitespace
 
 from abc import abstractmethod, abstractproperty
 
-from gentoopm.util import ABCObject, StringifiedComparisons
+from gentoopm.util import ABCObject, StringCompat, StringifiedComparisons
 
-class PMPackageKey(ABCObject, StringifiedComparisons):
+class PMPackageKey(ABCObject, StringCompat):
 	"""
 	A base class for a package key (CP/qualified package name).
 	"""
 	def __str__(self):
 		return self.package
 
-class PMPackageVersion(ABCObject, StringifiedComparisons):
+class PMPackageVersion(ABCObject, StringCompat):
 	"""
 	A base class for a package version.
 	"""
 
 	def __repr__(self):
 		if self.complete:
-			s = repr(self.__str__())
+			s = repr(str(self))
 		else:
 			s = '<incomplete>'
 		return '%s(%s)' % (self.__class__.__name__, s)

File gentoopm/basepm/pkg.py

View file
  • Ignore whitespace
 
 from gentoopm.basepm.atom import PMAtom
 from gentoopm.basepm.environ import PMPackageEnvironment
-from gentoopm.util import ABCObject, FillMissingComparisons
+from gentoopm.util import ABCObject, FillMissingComparisons, StringCompat
 
-class PMPackageDescription(ABCObject):
+class PMPackageDescription(ABCObject, StringCompat):
 	"""
 	Description of a package.
 	"""

File gentoopm/util.py

View file
  • Ignore whitespace
 
 	def __nonzero__(self):
 		return self.__bool__()
+
+class StringCompat(StringifiedComparisons):
+	"""
+	A helper class providing objects with compatibility string functions,
+	working on stringified form of objects. In other words, it lets you use
+	objects like strings.
+	"""
+
+	def __hasattr__(self, k):
+		return hasattr(str, k)
+
+	def __getattr__(self, k):
+		return getattr(str(self), k)
+
+	# other useful special methods
+	def __add__(self, other):
+		return str(self) + other
+
+	def __contains__(self, h):
+		# XXX: might be useful to override this
+		return h in str(self)
+
+	def __format__(self, spec):
+		return format(str(self), spec)
+
+	def __getitem__(self, k):
+		return str(self)[k]
+
+	def __getslice__(self, a, b):
+		return str(self)[a:b]
+
+	def __len__(self):
+		return len(str(self))
+
+	def __mul__(self, other):
+		return str(self) * other
+
+	def __rmod__(self, other):
+		return other % str(self)
+
+	def __rmul__(self, other):
+		return other * str(self)