Add a Package.iuse.is_valid_flag() method to replace the regex attribute,
authorZac Medico <zmedico@gentoo.org>
Wed, 28 Apr 2010 03:56:36 +0000 (20:56 -0700)
committerZac Medico <zmedico@gentoo.org>
Wed, 28 Apr 2010 03:56:36 +0000 (20:56 -0700)
in order to avoid compiling regex for each Package instance.

pym/_emerge/Package.py
pym/portage/dep/__init__.py

index bce4e6cbec50d5ef6f5abce900a707e3da444030..8e0c37890c514a6fdb20a0325352e648102a669e 100644 (file)
@@ -153,12 +153,11 @@ class Package(Task):
        class _iuse(object):
 
                __slots__ = ("__weakref__", "all", "enabled", "disabled",
-                       "iuse_implicit", "tokens") + \
-                       ('_regex',)
+                       "tokens") + ("_iuse_implicit_regex",)
 
-               def __init__(self, tokens, iuse_implicit):
+               def __init__(self, tokens, iuse_implicit_regex):
                        self.tokens = tuple(tokens)
-                       self.iuse_implicit = iuse_implicit
+                       self._iuse_implicit_regex = iuse_implicit_regex
                        enabled = []
                        disabled = []
                        other = []
@@ -174,23 +173,13 @@ class Package(Task):
                        self.disabled = frozenset(disabled)
                        self.all = frozenset(chain(enabled, disabled, other))
 
-               @property
-               def regex(self):
+               def is_valid_flag(self, flag):
                        """
-                       @returns: A regular expression that matches valid USE values which
-                               may be specified in USE dependencies.
+                       @returns: True if flag is a valid USE value which may
+                               be specified in USE dependencies, False otherwise.
                        """
-                       try:
-                               return self._regex
-                       except AttributeError:
-                               # Escape anything except ".*" which is supposed
-                               # to pass through from _get_implicit_iuse()
-                               regex = (re.escape(x) for x in \
-                                       chain(self.all, self.iuse_implicit))
-                               regex = "^(%s)$" % "|".join(regex)
-                               regex = re.compile(regex.replace("\\.\\*", ".*"))
-                               self._regex = regex
-                               return regex
+                       return flag in self.all or \
+                               self._iuse_implicit_regex.match(flag) is not None
 
        def _get_hash_key(self):
                hash_key = getattr(self, "_hash_key", None)
@@ -306,7 +295,7 @@ class _PackageMetadataWrapper(_PackageMetadataWrapperBase):
 
        def _set_iuse(self, k, v):
                self._pkg.iuse = self._pkg._iuse(
-                       v.split(), self._pkg.root_config.iuse_implicit)
+                       v.split(), self._pkg.root_config.settings._iuse_implicit_re)
 
        def _set_slot(self, k, v):
                self._pkg.slot = v
index abe5a066627be6eac6186fae4c05c992a66ac068..341953930d8b364ad1dda12bcf5e1745f093ac8b 100644 (file)
@@ -1185,10 +1185,10 @@ def match_from_list(mydep, candidate_list):
                for x in candidate_list:
                        use = getattr(x, "use", None)
                        if use is not None:
-                               regex = x.iuse.regex
+                               is_valid_flag = x.iuse.is_valid_flag
                                missing_iuse = False
                                for y in mydep.use.required:
-                                       if regex.match(y) is None:
+                                       if not is_valid_flag(y):
                                                missing_iuse = True
                                                break
                                if missing_iuse: