[Piglit] [PATCH 3/4] summary.py: further simplify the html_generator function

Dylan Baker baker.dylan.c at gmail.com
Mon Nov 3 17:05:05 PST 2014


This replaces the returnList function with a simpler version making use
of posixpath. This eliminates about 30 lines of code and makes the
resulting code easier to understand.

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
 framework/summary.py | 116 ++++++++++++++++++---------------------------------
 1 file changed, 40 insertions(+), 76 deletions(-)

diff --git a/framework/summary.py b/framework/summary.py
index 0dbeb82..64b0f7b 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -131,49 +131,6 @@ def html_generator(summary, page, exclude):
     new_row = '<tr>'
     end_row = '</tr>'
 
-    def returnList(open, close):
-        """
-        As HTMLIndex iterates through the groups and tests it uses this
-        function to determine which groups to close (and thus reduce the
-        depth of the next write) and which ones to open (thus increasing
-        the depth)
-
-        To that end one of two things happens, the path to the previous
-        group (close) and the next group (open) are equal, in that event we
-        don't want to open and close, becasue that will result in a
-        sawtooth pattern of a group with one test followed by the same
-        group with one test, over and over.  Instead we simply return two
-        empty lists, which will result in writing a long list of test
-        results.  The second option is that the paths are different, and
-        the function determines any commonality between the paths, and
-        returns the differences as close (the groups which are completly
-        written) and open (the new groups to write).
-        """
-        common = []
-
-        # Open and close are lists, representing the group hierarchy, open
-        # being the groups that need are soon to be written, and close
-        # representing the groups that have finished writing.
-        if open == close:
-            return [], []
-        else:
-            for i, j in itertools.izip_longest(open, close):
-                if i != j:
-                    for k in common:
-                        open.remove(k)
-                        close.remove(k)
-                    return open, close
-                else:
-                    common.append(i)
-
-    # set a starting depth of 1, 0 is used for 'all' so 1 is the
-    # next available group
-    depth = 1
-
-    # Current dir is a list representing the groups currently being
-    # written.
-    currentDir = []
-
     # Add a new 'tab' for each result
     yield new_row
     yield '<td />'
@@ -193,41 +150,48 @@ def html_generator(summary, page, exclude):
                             summary.fractions[each.name]['all'])
     yield end_row
 
+    # depth is the amount to indent the groups. since 0 is used for the root
+    # 'all' group, other groups start at 1
+    depth = 1
+
+    current = ''
+
     # Add the groups and tests to the out list
     for key in sorted(page):
-        # Split the group names and test names, then determine
-        # which groups to close and which to open
-        openList = key.replace('\\', '/').split('/')
-        test = openList.pop()
-        openList, closeList = returnList(openList, list(currentDir))
-
-        # Close any groups in the close list
-        # for each group closed, reduce the depth by one
-        for i in reversed(closeList):
-            currentDir.remove(i)
-            depth -= 1
-
-        # Open new groups
-        for localGroup in openList:
-            yield new_row
-
-            # Add the left-most column: the name of the group
-            yield _group_row("head", depth, localGroup)
-
-            # Add the group that we just opened to the currentDir, which
-            # will then be used to add that group to the HTML list. If
-            # there is a KeyError (the group doesn't exist), use (0, 0)
-            # which will get skip. This sets the group coloring correctly
-            currentDir.append(localGroup)
-            for each in summary.results:
-                # Decide which fields need to be updated
-                yield _group_result(
-                    summary.status[each.name][path.join(*currentDir)],
-                    summary.fractions[each.name][path.join(*currentDir)])
-
-            # After each group increase the depth by one
-            depth += 1
-            yield end_row
+        assert '\\' not in key, '\\ is not allowed in test names'
+
+        previous = current
+        current, test = posixpath.split(key)
+        if current != previous:
+            common = posixpath.commonprefix([current, previous])
+
+            # close any elements of the previous group that are not shared with
+            # the previous group.
+            # On the first run posixpath.relpath will have a value error
+            # because previous is ''.
+            try:
+                for _ in posixpath.relpath(previous, common).split('/'):
+                    depth -= 1
+            except ValueError as e:
+                if e.message != 'no path specified':
+                    raise e
+
+            # Open new groups
+            work = common.split('/')
+            for group in posixpath.relpath(current, common).split('/'):
+                work.append(group)
+                yield new_row
+
+                # Add the left-most column: the name of the group
+                yield _group_row("head", depth, group)
+                for each in summary.results:
+                    yield _group_result(
+                        summary.status[each.name][path.join(*work)],
+                        summary.fractions[each.name][path.join(*work)])
+
+                # After each group increase the depth by one
+                depth += 1
+                yield end_row
 
         # Add the tests for the current group
         yield new_row
-- 
2.1.3



More information about the Piglit mailing list