12
12
13
13
from collections import defaultdict
14
14
15
+
16
+ html_template = """<html><head><style media="screen" type="text/css">
17
+ img{{
18
+ width:100%;
19
+ max-width:800px;
20
+ }}
21
+ </style>
22
+ </head><body>
23
+ {failed}
24
+ {body}
25
+ </body></html>
26
+ """
27
+
28
+ subdir_template = """<h2>{subdir}</h2><table>
29
+ <thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>
30
+ {rows}
31
+ </table>
32
+ """
33
+
34
+ failed_template = """<h2>Only Failed</h2><table>
35
+ <thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>
36
+ {rows}
37
+ </table>
38
+ """
39
+
40
+ row_template = ('<tr>'
41
+ '<td>{0} {1}</td>'
42
+ '<td>{2}</td>'
43
+ '<td><a href="{3}"><img src="{3}"></a></td>'
44
+ '<td>{4}</td>'
45
+ '</tr>' )
46
+
47
+ linked_image_template = '<a href="{0}"><img src="{0}"></a>'
48
+
49
+
15
50
def run ():
16
51
# Build a website for visual comparison
17
52
image_dir = "result_images"
18
53
# build the website
19
- _html = ""
20
- _html += """<html><head><style media="screen" type="text/css">
21
- img{
22
- width:100%;
23
- max-width:800px;
24
- }
25
- </style>
26
- </head><body>\n """
27
54
_subdirs = [name for name in os .listdir (image_dir ) if os .path .isdir (os .path .join (image_dir , name ))]
28
55
# loop over all pictures
29
- _row = '<tr><td>{0} {1}</td><td>{2}</td><td><a href="{3}"><img src="{3}"></a></td><td>{4}</td>\n '
30
- _failed = ""
31
- _failed += "<h2>Only Failed</h2>"
32
- _failed += "<table>\n <thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n "
33
56
_has_failure = False
34
- _body = ""
57
+ failed_rows = []
58
+ body_sections = []
35
59
for subdir in _subdirs :
36
60
if subdir == "test_compare_images" :
37
61
# these are the image which test the image comparison functions...
@@ -51,37 +75,46 @@ def run():
51
75
else :
52
76
pictures [fn ]["c" ] = "/" .join ((subdir , file ))
53
77
54
- _body += "<h2>{0}</h2>" .format (subdir )
55
- _body += "<table>\n <thead><td>name</td><td>actual</td><td>expected</td><td>diff</td></thead>\n "
78
+ subdir_rows = []
56
79
for name , test in six .iteritems (pictures ):
57
80
if test .get ("f" , None ):
58
81
# a real failure in the image generation, resulting in different images
59
82
_has_failure = True
60
83
s = "(failed)"
61
84
failed = '<a href="{0}">diff</a>' .format (test .get ("f" , "" ))
62
- current = '<a href="{0}"><img src="{0}"></a>' .format (test .get ("c" , "" ))
63
- _failed += _row .format (name , "" , current , test .get ("e" , "" ), failed )
85
+ current = linked_image_template .format (test .get ("c" , "" ))
86
+ failed_rows .append (row_template .format (name , "" , current ,
87
+ test .get ("e" , "" ),
88
+ failed ))
64
89
elif test .get ("c" , None ) is None :
65
90
# A failure in the test, resulting in no current image
66
91
_has_failure = True
67
92
s = "(failed)"
68
93
failed = '--'
69
94
current = '(Failure in test, no image produced)'
70
- _failed += _row .format (name , "" , current , test .get ("e" , "" ), failed )
95
+ failed_rows .append (row_template .format (name , "" , current ,
96
+ test .get ("e" , "" ),
97
+ failed ))
71
98
else :
72
99
s = "(passed)"
73
100
failed = '--'
74
- current = '<a href="{0}"><img src="{0}"></a>' .format (test .get ("c" , "" ))
75
- _body += _row .format (name , "" , current , test .get ("e" , "" ), failed )
76
- _body += "</table>\n "
77
- _failed += "</table>\n "
101
+ current = linked_image_template .format (test .get ("c" , "" ))
102
+ subdir_rows .append (row_template .format (name , "" , current ,
103
+ test .get ("e" , "" ), failed ))
104
+
105
+ body_sections .append (
106
+ subdir_template .format (subdir = subdir , rows = '\n ' .join (subdir_rows )))
107
+
78
108
if _has_failure :
79
- _html += _failed
80
- _html += _body
81
- _html += "\n </body></html>"
109
+ failed = failed_template .format (rows = '\n ' .join (failed_rows ))
110
+ else :
111
+ failed = ''
112
+ body = '' .join (body_sections )
113
+ html = html_template .format (failed = failed , body = body )
82
114
index = os .path .join (image_dir , "index.html" )
83
115
with open (index , "w" ) as f :
84
- f .write (_html )
116
+ f .write (html )
117
+
85
118
try :
86
119
import webbrowser
87
120
webbrowser .open (index )
0 commit comments