genhtml: Add Space class
authorMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 9 Dec 2010 11:51:17 +0000 (12:51 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Thu, 9 Dec 2010 11:51:17 +0000 (12:51 +0100)
gw-tests/genhtml/genhtml-test.py
gw-tests/genhtml/genhtml.py

index 2e7aaa3..68d3131 100644 (file)
@@ -17,16 +17,16 @@ def Dimension_and_DimValue():
     
 @wvtest                
 def Two_Dimensions_in_Tests():
-    t = Tests(None, DimensionTest(), DimensionKern())
-    WVPASSEQ([(a.__class__.__name__, b.__class__.__name__) for a, b in t.iterDimensionPairs()],
+    t = Tests(None, Space(DimensionTest(), DimensionKern()))
+    WVPASSEQ([(a.__class__.__name__, b.__class__.__name__) for a, b in t.space.iterDimensionPairs()],
              [('DimensionTest', 'DimensionKern'), ('DimensionKern', 'DimensionTest')])
 @wvtest                
 def Three_Dimensions_in_Tests():
     dt = DimensionTest()
     dk = DimensionKern()
     dd = Dimension('tmp', "Tmp")
-    t = Tests(None, dt, dk, dd)
-    pairs = [(a.__class__.__name__, b.__class__.__name__) for a, b in t.iterDimensionPairs()]
+    t = Tests(None, Space(dt, dk, dd))
+    pairs = [(a.__class__.__name__, b.__class__.__name__) for a, b in t.space.iterDimensionPairs()]
     WVPASSEQ(pairs[0], ('DimensionTest', 'DimensionKern'))
     WVPASSEQ(pairs[1], ('DimensionKern', 'DimensionTest'))
     WVPASSEQ(pairs[2], ('DimensionTest', 'Dimension'))
@@ -55,12 +55,12 @@ def Three_Dimensions_in_Tests():
     WVPASSEQ(t.getTest((v3, v2, v1)), MyTest)
     WVEXCEPT(KeyError, t.getTest, (v1, v1, v1))
     WVEXCEPT(KeyError, t.getTest, (v1, v2))
-    WVEXCEPT(TypeError, t.getTest, v1)
+    WVEXCEPT(Exception, t.getTest, v1)
 
-    WVPASSEQ([d.__class__.__name__ for d in t.iterRemainingDimensions([dt])], ['DimensionKern', 'Dimension']) 
-    WVPASSEQ([d.__class__.__name__ for d in t.iterRemainingDimensions([dt, dd])], ['DimensionKern'])
+    WVPASSEQ([d.__class__.__name__ for d in t.space.iterRemainingDimensions([dt])], ['DimensionKern', 'Dimension']) 
+    WVPASSEQ([d.__class__.__name__ for d in t.space.iterRemainingDimensions([dt, dd])], ['DimensionKern'])
 
-    WVPASSEQ(str([v for v in iterDimValues([dt, dk])]), "[[DimValue(DimensionTest, 1), DimValue(DimensionKern, 'a')]]")
+    WVPASSEQ(str([v for v in Space(dt, dk).iterValues()]), "[[DimValue(DimensionTest, 1), DimValue(DimensionKern, 'a')]]")
     dt.addValue(2)
     dk.addValue('b')
-    WVPASSEQ(str([v for v in iterDimValues([dt, dk])]), "[[DimValue(DimensionTest, 1), DimValue(DimensionKern, 'a')], [DimValue(DimensionTest, 2), DimValue(DimensionKern, 'a')], [DimValue(DimensionTest, 1), DimValue(DimensionKern, 'b')], [DimValue(DimensionTest, 2), DimValue(DimensionKern, 'b')]]")
+    WVPASSEQ(str([v for v in Space(dt, dk).iterValues()]), "[[DimValue(DimensionTest, 1), DimValue(DimensionKern, 'a')], [DimValue(DimensionTest, 2), DimValue(DimensionKern, 'a')], [DimValue(DimensionTest, 1), DimValue(DimensionKern, 'b')], [DimValue(DimensionTest, 2), DimValue(DimensionKern, 'b')]]")
index 5dacc9e..1ba9f7f 100755 (executable)
@@ -25,11 +25,12 @@ class DimValues(list):
         return DimValues(self).append(val)
     def __sub__(self, dim):
         result = DimValues(self)
-        print repr(result)
         for v in self:
             if v.dim == dim:
                 result.remove(v)
         return result
+    def key(self):
+        return tuple([v.value for v in self])
 
 class Dimension(dict):
     def __init__(self, atype, name=None):
@@ -44,7 +45,6 @@ class Dimension(dict):
        for i in xrange(len(self)):
            yield self.getValue(i)
     def getValue(self, index):
-        print self.sortedKeys
         return self[self.sortedKeys[index]]
 
     def addValue(self, *values):
@@ -60,7 +60,7 @@ class Dimension(dict):
     def __str__(self):
         return self.name
     def __repr__(self):
-        return self.type
+        return "Dimension(%s)"%self.type
 
 class DimensionKern(Dimension):
     def __init__(self):
@@ -133,45 +133,62 @@ class Test:
             print >>file, "<a href='%s/%s'><img src='%s/thumb/%s'></a>" % \
                   (urllib.quote(self.path), img, urllib.quote(self.path), img)
 
-def iterDimValues(dimensions):
-    idx = [0 for i in xrange(len(dimensions))]
-    done=False
-    while not done:
-       values=DimValues()
-       for i in xrange(len(dimensions)):
-           values.append(dimensions[i].values()[idx[i]])
-       yield values
-       done=True
-       for i in xrange(len(dimensions)):
-           idx[i] += 1
-           if idx[i] < len(dimensions[i]):
-               done=False
-               break
-           idx[i] = 0
+class Space(list):
+    """List of Dimensions()s (order matters)"""
+    def __init__(self, *dimensions):
+        self.extend(list(dimensions))
+    def iterValues(self):
+        idx = [0 for i in xrange(len(self))]
+        done=False
+        while not done:
+            values=DimValues()
+            for i in xrange(len(self)):
+                values.append(self[i].values()[idx[i]])
+            yield values
+            done=True
+            for i in xrange(len(self)):
+                idx[i] += 1
+                if idx[i] < len(self[i]):
+                    done=False
+                    break
+                idx[i] = 0
+    def reorder(self, dimValues):
+        reordered = DimValues()
+        for d in self:
+           for v in dimValues:
+               if v.dim == d:
+                   reordered.append(v)
+        return reordered
+    def iterDimensionPairs(self):
+       for i in xrange(len(self)):
+           for j in xrange(i+1, len(self)):
+               yield (self[i], self[j])
+               yield (self[j], self[i])
+    def iterRemainingDimensions(self, dimensionPair):
+       for d in self:
+           if d not in dimensionPair:
+               yield d
+
 
 class Tests(dict):
     """Represents all tests organized along several dimensions"""
-    def __init__(self, rootpath, *dimensions):
+    def __init__(self, rootpath, space):
        dict.__init__(self)
-       self.dimensions = dimensions
+       self.space = space
        if (rootpath):
            self.populate(rootpath)
     def getTest(self, key):
-       realkey=[]
-       for d in self.dimensions:
-           for i in key:
-               if i.dim == d:
-                   realkey.append(i.value)
-       if len(realkey) != len(self.dimensions):
-           raise KeyError("The coordinates in key do not match dimensions")
-       return self[tuple(realkey)]
+       if len(key) != len(self.space):
+           raise KeyError("The coordinates in key do not match the dimension of the space")
+        realkey = self.space.reorder(key)
+       return self[realkey.key()]
 
     def addTest(self, test, coordinates):
-       if len(coordinates) != len(self.dimensions):
+       if len(coordinates) != len(self.space):
            raise KeyError("The number coordinates do not match the number of dimensions: " + str(coordinates))
        self[tuple(coordinates)] = test
        for i in xrange(len(coordinates)):
-           self.dimensions[i].addValue(coordinates[i])
+           self.space[i].addValue(coordinates[i])
 
     def populate(self, rootpath):
        for root, dirs, files in os.walk(rootpath):
@@ -181,19 +198,10 @@ class Tests(dict):
                coordinates = rootpath
            if Test.isOnPath(root):
                self.addTest(Test(root), coordinates.split("/"))
-    def iterDimensionPairs(self):
-       for i in xrange(len(self.dimensions)):
-           for j in xrange(i+1, len(self.dimensions)):
-               yield (self.dimensions[i], self.dimensions[j])
-               yield (self.dimensions[j], self.dimensions[i])
-    def iterRemainingDimensions(self, dimensionPair):
-       for d in self.dimensions:
-           if d not in dimensionPair:
-               yield d
     def generateHtml(self):
-       for pair in self.iterDimensionPairs():
-           remDims = [d for d in self.iterRemainingDimensions(pair)]
-           for vals in iterDimValues(remDims):
+       for pair in self.space.iterDimensionPairs():
+           remDims = Space(*tuple([d for d in self.space.iterRemainingDimensions(pair)]))
+           for vals in remDims.iterValues():
                page = Page(pair, remDims, vals, self)
                page.generate()
        try:
@@ -280,6 +288,6 @@ th, td { border: 1px solid lightgray; padding: 4px;}
 if __name__ == "__main__":
     os.chdir(sys.argv[1])
     os.system("rm *.html")
-    tests = Tests("./", DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest())
+    tests = Tests("./", Space(DimensionHostKern(), DimensionKern(), DimensionTraffic(), DimensionTest()))
     tests.generateHtml()
     sys.exit(0)