portage.dep.use_reduce: Add token_class parameter
authorSebastian Luther <SebastianLuther@gmx.de>
Mon, 16 Aug 2010 13:52:38 +0000 (15:52 +0200)
committerZac Medico <zmedico@gentoo.org>
Tue, 17 Aug 2010 00:40:27 +0000 (17:40 -0700)
All non operator token will be converted to this class

pym/portage/dep/__init__.py
pym/portage/tests/dep/test_use_reduce.py

index eed7a5eca2d24ffd33ff33815c20a64e7150e014..a1669783592d5e7e31838995288f57027e7dd27c 100644 (file)
@@ -221,7 +221,7 @@ def paren_enclose(mylist):
        return " ".join(mystrparts)
 
 def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], is_src_uri=False, \
-       allow_src_uri_file_renames=False, opconvert=False, flat=False, is_valid_flag=None):
+       allow_src_uri_file_renames=False, opconvert=False, flat=False, is_valid_flag=None, token_class=None):
        """
        Takes a dep string and reduces the use? conditionals out, leaving an array
        with subarrays. All redundant brackets are removed.
@@ -246,6 +246,8 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
        @type flat: Bool
        @param is_valid_flag: Function that decides if a given use flag might be used in use conditionals
        @type is_valid_flag: Function
+       @param token_class: Convert all non operator tokens into this class
+       @type token_class: Class
        @rtype: List
        @return: The use reduced depend array
        """
@@ -290,7 +292,7 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                return (flag in uselist and not is_negated) or \
                        (flag not in uselist and is_negated)
 
-       def invalid_token_check(token, pos):
+       def missing_white_space_check(token, pos):
                """
                Used to generate good error messages for invalid tokens.
                """
@@ -299,9 +301,6 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                                raise portage.exception.InvalidDependString(
                                        _("missing whitespace around '%s' at '%s' in '%s', token %s") % (x, token, depstr, pos+1))
 
-               raise portage.exception.InvalidDependString(
-                       _("invalid token '%s' in '%s', token %s") % (token, depstr, pos+1))
-
        mysplit = depstr.split()
        #Count the bracket level.
        level = 0
@@ -409,8 +408,7 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                        need_simple_token = True
                        stack[level].append(token)      
                else:
-                       if "(" in token or ")" in token or "|" in token:
-                               invalid_token_check(token, pos)
+                       missing_white_space_check(token, pos)
 
                        if need_bracket:
                                raise portage.exception.InvalidDependString(
@@ -425,6 +423,13 @@ def use_reduce(depstr, uselist=[], masklist=[], matchall=False, excludeall=[], i
                                need_bracket = True
                        else:
                                need_simple_token = False
+                               if token_class and not is_src_uri:
+                                       #Add a hack for SRC_URI here, to avoid conditional code at the consumer level
+                                       try:
+                                               token = token_class(token)
+                                       except Exception as e:
+                                               raise portage.exception.InvalidDependString(
+                                                       _("Invalid token '%s' in '%s', token %s") % (token, depstr, pos+1))
 
                        stack[level].append(token)
 
index 29ed942bdf180bc6187cf4903d7cbb517201543a..f715f4dcfb1eaae648b18763681f77a6d3a2d80a 100644 (file)
@@ -3,12 +3,13 @@
 
 from portage.tests import TestCase
 from portage.exception import InvalidDependString
-from portage.dep import use_reduce
+from portage.dep import Atom, use_reduce
 
 class UseReduceTestCase(object):
        def __init__(self, deparray, uselist=[], masklist=[], \
                matchall=0, excludeall=[], is_src_uri=False, \
-               allow_src_uri_file_renames=False, opconvert=False, flat=False, expected_result=None, is_valid_flag=None):
+               allow_src_uri_file_renames=False, opconvert=False, flat=False, expected_result=None, \
+                       is_valid_flag=None, token_class=None):
                self.deparray = deparray
                self.uselist = uselist
                self.masklist = masklist
@@ -19,12 +20,13 @@ class UseReduceTestCase(object):
                self.opconvert = opconvert
                self.flat = flat
                self.is_valid_flag = is_valid_flag
+               self.token_class = token_class
                self.expected_result = expected_result
 
        def run(self):
                return use_reduce(self.deparray, self.uselist, self.masklist, \
                        self.matchall, self.excludeall, self.is_src_uri, self.allow_src_uri_file_renames, \
-                               self.opconvert, self.flat, self.is_valid_flag)
+                               self.opconvert, self.flat, self.is_valid_flag, self.token_class)
                                
 class UseReduce(TestCase):
 
@@ -414,6 +416,17 @@ class UseReduce(TestCase):
                                "foo? ( A )",
                                is_valid_flag = self.always_true,
                                expected_result = []),
+
+                       #token_class
+                       UseReduceTestCase(
+                               "foo? ( dev-libs/A )",
+                               uselist = ["foo"],
+                               token_class=Atom,
+                               expected_result = ["dev-libs/A"]),
+                       UseReduceTestCase(
+                               "foo? ( dev-libs/A )",
+                               token_class=Atom,
+                               expected_result = []),
                )
                
                test_cases_xfail = (
@@ -433,7 +446,6 @@ class UseReduce(TestCase):
                        UseReduceTestCase("a? A"),
                        UseReduceTestCase("( || ( || || ( A ) foo? ( B ) ) )"),
                        UseReduceTestCase("( || ( || bar? ( A ) foo? ( B ) ) )"),
-                       UseReduceTestCase("A(B"),
                        UseReduceTestCase("foo?"),
                        
                        #SRC_URI stuff
@@ -469,6 +481,15 @@ class UseReduce(TestCase):
                                uselist = ["foo"],
                                is_valid_flag = self.always_false,
                                ),
+
+                       #token_class
+                       UseReduceTestCase(
+                               "foo? ( A )",
+                               uselist = ["foo"],
+                               token_class=Atom),
+                       UseReduceTestCase(
+                               "A(B",
+                               token_class=Atom),
                )
 
                for test_case in test_cases: