From 2fd3ea367e0b7f7978a3fc85e5cb57e7191ee431 Mon Sep 17 00:00:00 2001 From: Sebastian Luther Date: Mon, 16 Aug 2010 15:52:38 +0200 Subject: portage.dep.use_reduce: Add token_class parameter All non operator token will be converted to this class --- pym/portage/dep/__init__.py | 19 ++++++++++++------- pym/portage/tests/dep/test_use_reduce.py | 29 +++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/pym/portage/dep/__init__.py b/pym/portage/dep/__init__.py index eed7a5eca..a16697835 100644 --- a/pym/portage/dep/__init__.py +++ b/pym/portage/dep/__init__.py @@ -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) diff --git a/pym/portage/tests/dep/test_use_reduce.py b/pym/portage/tests/dep/test_use_reduce.py index 29ed942bd..f715f4dcf 100644 --- a/pym/portage/tests/dep/test_use_reduce.py +++ b/pym/portage/tests/dep/test_use_reduce.py @@ -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: -- cgit v1.2.3-1-g7c22