portage.dep.paren_reduce: Remove redundant brackets
authorSebastian Luther <SebastianLuther@gmx.de>
Tue, 10 Aug 2010 08:10:58 +0000 (10:10 +0200)
committerZac Medico <zmedico@gentoo.org>
Tue, 10 Aug 2010 08:15:11 +0000 (01:15 -0700)
pym/portage/dep/__init__.py
pym/portage/tests/dep/test_paren_reduce.py

index 19e347cfb581a10015466c2efc9a3884a04e4290..89b384a5b70107e2fae79ba6626a932503ec3c58 100644 (file)
@@ -84,11 +84,11 @@ def strip_empty(myarr):
 def paren_reduce(mystr):
        """
        Take a string and convert all paren enclosed entities into sublists and
-       split the list elements by spaces.
+       split the list elements by spaces. All redundant brackets are removed.
 
        Example usage:
-               >>> paren_reduce('foobar foo ( bar baz )')
-               ['foobar', 'foo', ['bar', 'baz']]
+               >>> paren_reduce('foobar foo? ( bar baz )')
+               ['foobar', 'foo?', ['bar', 'baz']]
 
        @param mystr: The string to reduce
        @type mystr: String
@@ -111,7 +111,26 @@ def paren_reduce(mystr):
                                        _("malformed syntax: '%s'") % mystr)
                        if level > 0:
                                level -= 1
-                               stack[level].append(stack.pop())
+                               l = stack.pop()
+                               if l:
+                                       if not stack[level] or (stack[level][-1] != "||" and not stack[level][-1][-1] == "?"):
+                                               #Optimize: ( ( ... ) ) -> ( ... )
+                                               stack[level].extend(l)
+                                       elif len(l) == 1 and stack[level][-1] == "||":
+                                               #Optimize: || ( A ) -> A
+                                               stack[level].pop()
+                                               stack[level].extend(l)
+                                       elif len(l) == 2 and (l[0] == "||" or l[0][-1] == "?") and stack[level][-1] in (l[0], "||"):
+                                               #Optimize:      || ( || ( ... ) ) -> || ( ... )
+                                               #                       foo? ( foo? ( ... ) ) -> foo? ( ... )
+                                               #                       || ( foo? ( ... ) ) -> foo? ( ... )
+                                               stack[level].pop()
+                                               stack[level].extend(l)
+                                       else:
+                                               stack[level].append(l)
+                               else:
+                                       if stack[level] and (stack[level][-1] == "||" or stack[level][-1][-1] == "?"):
+                                               stack[level].pop()
                        else:
                                raise portage.exception.InvalidDependString(
                                        _("malformed syntax: '%s'") % mystr)
index 3277ab58bf42a404a291aa67047ad35c5bd3fb15..bd5f42584d093db0b95e9469a17671e3d2bb9434 100644 (file)
@@ -11,12 +11,23 @@ class TestParenReduce(TestCase):
 
                test_cases = (
                        ( "A", ["A"]),
-                       ( "( A )", [["A"]]),
+                       ( "( A )", ["A"]),
                        ( "|| ( A B )", [ "||", ["A", "B"] ]),
                        ( "|| ( A || ( B C ) )", [ "||", ["A", "||", ["B", "C"]]]),
                        ( "|| ( A || ( B C D ) )", [ "||", ["A", "||", ["B", "C", "D"]] ]),
                        ( "|| ( A || ( B || ( C D ) E ) )", [ "||", ["A", "||", ["B", "||", ["C", "D"], "E"]] ]),
                        ( "a? ( A )", ["a?", ["A"]]),
+                       
+                       ( "( || ( ( ( A ) B ) ) )", [ "||", ["A", "B"] ]),
+                       ( "( || ( || ( ( A ) B ) ) )", [ "||", ["A", "B"] ]),
+                       ( "( || ( || ( ( A ) B ) ) )", [ "||", ["A", "B"] ]),
+                       ( "|| ( A )", ["A"]),
+                       ( "( || ( || ( || ( A ) foo? ( B ) ) ) )", [ "||", ["A", "foo?", ["B"] ]]),
+                       ( "( || ( || ( bar? ( A ) || ( foo? ( B ) ) ) ) )", [ "||", ["bar?", ["A"], "foo?", ["B"] ]]),
+                       ( "A || ( ) foo? ( ) B", ["A", "B"]),
+
+                       ( "|| ( A ) || ( B )", ["A", "B"]),
+                       ( "foo? ( A ) foo? ( B )", ["foo?", ["A"], "foo?", ["B"]]),
                )
                
                test_cases_xfail = (
@@ -36,6 +47,9 @@ class TestParenReduce(TestCase):
                        "|| ( A B || )",
                        
                        "a? A",
+                       
+                       ( "( || ( || || ( A ) foo? ( B ) ) )"),
+                       ( "( || ( || bar? ( A ) foo? ( B ) ) )"),
                )
 
                for dep_str, expected_result in test_cases: