aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'pym/gentoolkit/keyword.py')
-rw-r--r--pym/gentoolkit/keyword.py180
1 files changed, 89 insertions, 91 deletions
diff --git a/pym/gentoolkit/keyword.py b/pym/gentoolkit/keyword.py
index e997efe..4160781 100644
--- a/pym/gentoolkit/keyword.py
+++ b/pym/gentoolkit/keyword.py
@@ -7,12 +7,7 @@
http://www.gentoo.org/proj/en/glep/glep-0053.html
"""
-__all__ = (
- 'Keyword',
- 'compare_strs',
- 'reduce_keywords',
- 'determine_keyword'
-)
+__all__ = ("Keyword", "compare_strs", "reduce_keywords", "determine_keyword")
# =======
# Imports
@@ -23,87 +18,90 @@ __all__ = (
# Classes
# =======
+
class Keyword:
- """Provides common methods on a GLEP 53 keyword."""
+ """Provides common methods on a GLEP 53 keyword."""
+
+ def __init__(self, keyword):
+ self.keyword = keyword
+ arch, sep, os = keyword.partition("-")
+ self.arch = arch
+ self.os = os
- def __init__(self, keyword):
- self.keyword = keyword
- arch, sep, os = keyword.partition('-')
- self.arch = arch
- self.os = os
+ def __eq__(self, other):
+ if not isinstance(other, self.__class__):
+ return False
+ return self.keyword == other.keyword
- def __eq__(self, other):
- if not isinstance(other, self.__class__):
- return False
- return self.keyword == other.keyword
+ def __ne__(self, other):
+ return not self == other
- def __ne__(self, other):
- return not self == other
+ def __lt__(self, other):
+ if not isinstance(other, self.__class__):
+ raise TypeError(
+ "other isn't of %s type, is %s" % (self.__class__, other.__class__)
+ )
+ if self.os < other.os:
+ return True
+ return self.arch < other.arch
- def __lt__(self, other):
- if not isinstance(other, self.__class__):
- raise TypeError("other isn't of %s type, is %s" % (
- self.__class__, other.__class__)
- )
- if self.os < other.os:
- return True
- return self.arch < other.arch
+ def __le__(self, other):
+ return self == other or self < other
- def __le__(self, other):
- return self == other or self < other
+ def __gt__(self, other):
+ return not self <= other
- def __gt__(self, other):
- return not self <= other
+ def __ge__(self, other):
+ return self == other or self > other
- def __ge__(self, other):
- return self == other or self > other
+ def __str__(self):
+ return self.keyword
- def __str__(self):
- return self.keyword
+ def __repr__(self):
+ return "<{0.__class__.__name__} {0.keyword!r}>".format(self)
- def __repr__(self):
- return "<{0.__class__.__name__} {0.keyword!r}>".format(self)
# =========
# Functions
# =========
+
def compare_strs(kw1, kw2):
- """Similar to the builtin cmp, but for keyword strings. Usually called
- as: keyword_list.sort(keyword.compare_strs)
+ """Similar to the builtin cmp, but for keyword strings. Usually called
+ as: keyword_list.sort(keyword.compare_strs)
- An alternative is to use the Keyword descriptor directly:
- >>> keyword_list = ['~x86', '~amd64', 'x86']
- >>> kwds = sorted(Keyword(x) for x in keyword_list)
+ An alternative is to use the Keyword descriptor directly:
+ >>> keyword_list = ['~x86', '~amd64', 'x86']
+ >>> kwds = sorted(Keyword(x) for x in keyword_list)
- @see: >>> help(cmp)
- """
+ @see: >>> help(cmp)
+ """
- kw1_arch, sep, kw1_os = kw1.partition('-')
- kw2_arch, sep, kw2_os = kw2.partition('-')
- if kw1_arch != kw2_arch:
- if kw1_os != kw2_os:
- return -1 if kw1_os < kw2_os else 1
- return -1 if kw1_arch < kw2_arch else 1
- if kw1_os == kw2_os:
- return 0
- return -1 if kw1_os < kw2_os else 1
+ kw1_arch, sep, kw1_os = kw1.partition("-")
+ kw2_arch, sep, kw2_os = kw2.partition("-")
+ if kw1_arch != kw2_arch:
+ if kw1_os != kw2_os:
+ return -1 if kw1_os < kw2_os else 1
+ return -1 if kw1_arch < kw2_arch else 1
+ if kw1_os == kw2_os:
+ return 0
+ return -1 if kw1_os < kw2_os else 1
def reduce_keywords(keywords):
- """Reduce a list of keywords to a unique set of stable keywords.
+ """Reduce a list of keywords to a unique set of stable keywords.
- Example usage:
- >>> kw = reduce_keywords(['~amd64', 'x86', '~x86'])
- >>> isinstance(kw, set)
- True
- >>> sorted(kw)
- ['amd64', 'x86']
+ Example usage:
+ >>> kw = reduce_keywords(['~amd64', 'x86', '~x86'])
+ >>> isinstance(kw, set)
+ True
+ >>> sorted(kw)
+ ['amd64', 'x86']
- @type keywords: array
- @rtype: set
- """
- return set(x.lstrip('~') for x in keywords)
+ @type keywords: array
+ @rtype: set
+ """
+ return set(x.lstrip("~") for x in keywords)
abs_keywords = reduce_keywords
@@ -114,31 +112,31 @@ abs_keywords = reduce_keywords
# I was trying to avoid a 2nd use of determine_keyword name (in analyse.lib)
# but that one is a little different and not suitable for this task.
def determine_keyword(arch, accepted, keywords):
- """Determine a keyword from matching a dep's KEYWORDS
- list against the ARCH & ACCEPT_KEYWORDS provided.
-
- @type arch: string
- @param arch: portage.settings["ARCH"]
- @type accepted: string
- @param accepted: portage.settings["ACCEPT_KEYWORDS"]
- @type keywords: string
- @param keywords: the pkg ebuilds keywords
- """
- if not keywords:
- return ''
- keys = keywords.split()
- if arch in keys:
- return arch
- keyworded = "~" + arch
- if keyworded in keys:
- return keyworded
- match = list(set(accepted.split(" ")).intersection(keys))
- if len(match) > 1:
- if arch in match:
- return arch
- if keyworded in match:
- return keyworded
- return 'unknown'
- if match:
- return match[0]
- return 'unknown'
+ """Determine a keyword from matching a dep's KEYWORDS
+ list against the ARCH & ACCEPT_KEYWORDS provided.
+
+ @type arch: string
+ @param arch: portage.settings["ARCH"]
+ @type accepted: string
+ @param accepted: portage.settings["ACCEPT_KEYWORDS"]
+ @type keywords: string
+ @param keywords: the pkg ebuilds keywords
+ """
+ if not keywords:
+ return ""
+ keys = keywords.split()
+ if arch in keys:
+ return arch
+ keyworded = "~" + arch
+ if keyworded in keys:
+ return keyworded
+ match = list(set(accepted.split(" ")).intersection(keys))
+ if len(match) > 1:
+ if arch in match:
+ return arch
+ if keyworded in match:
+ return keyworded
+ return "unknown"
+ if match:
+ return match[0]
+ return "unknown"