Change look and feel of table, add better handling of table columns

Add many improvements to table look including support for tags such as
missing, partial, done, and not applicable.  The tabularcolumns
directive does not seem to be affecting the tables width and columns
however.
This commit is contained in:
Kyle Mandli
2011-07-16 16:54:23 -05:00
parent d73416294d
commit 92ce731890
2 changed files with 132 additions and 45 deletions
+13 -2
View File
@@ -275,12 +275,23 @@ th {
/* Coverage States */
span.missing{
color: #FFFFFF;
color: #000;
background-color: #ff5840;
border-color: #A77272;
font-weight: bold;
}
span.covered{
span.partial{
color: #806600;
background-color: #ffc343;
font-weight: bold;
}
span.done{
color: #106600;
background-color: #60f030;
border-color: #4F8530;
font-weight: bold;
}
span.na{
color: #A8A8A8;
border-color: #4F8530;
}
+119 -43
View File
@@ -4,68 +4,144 @@ import sys
import os
import csv
# Parameters
missing_string = ":missing:`Not Implemented`"
page_title = "Coverage Tables"
table_names = ["Image Display and Exploration","Image File I/O"]
# Import StringIO module
try:
import cStringIO as StringIO
except ImportError:
import StringIO
def table_seperator(cols,lengths,character="-"):
output = "+"
output += '+'.join([character*(length+2) for length in lengths])
output += "+"
return output
# Missing item value
MISSING_STRING=":missing:`Not Implemented`"
def read_table_titles(reader):
r"""Create a dictionary with keys as section names and values as a list of
table names
def table_row(data,lengths,num_columns=None):
return (dict)
"""
section_titles = []
table_names = {}
try:
for row in reader:
names = []
# End of names table
if len(row[0]) == 0:
break
# Extract names of the tables
for name in row[1:]:
if len(name) > 0:
names.append(name)
else:
break
section_titles.append(row[0])
table_names[row[0]] = names
except csv.Error, e:
sys.exit('line %d: %s' % (reader.line_num, e))
return section_titles,table_names
def table_seperator(stream,cols,lengths,character="-"):
stream.write("+")
stream.write('+'.join([character*(length+2) for length in lengths]))
stream.write("+")
def table_row(stream,data,lengths,num_columns=None):
if num_columns is None:
num_columns = len(data)
output = "|"
stream.write("|")
for i in xrange(num_columns):
if len(data)-1 >= i:
entry = data[i]
if len(data[i]) == 0:
entry = MISSING_STRING
else:
entry = data[i]
else:
entry = missing_string
output += " " + entry + " "*(lengths[i] - len(entry)) + " |"
return output
entry = MISSING_STRING
stream.write(" " + entry + " "*(lengths[i] - len(entry)) + " |")
def generate_table(reader,column_titles=["Functionality","Matlab","Scipy"]):
def generate_table(reader,stream,table_name,
column_titles=["Functionality","Matlab","Scipy","Scipy"]):
# Find number of columns and column widths, base number of columns is
# determined by the headers
num_columns = 3
num_columns = len(column_titles)
data = [column_titles]
for row in reader:
if len(row) == 0:
break
data.append([entry.expandtabs() for entry in row])
num_columns = max(num_columns,len(row))
try:
for row in reader:
# print row
if len(row[0]) == 0:
break
data.append([entry.expandtabs() for entry in row])
num_columns = max(num_columns,len(row))
except csv.Error, e:
sys.exit('line %d: %s' % (reader.line_num, e))
column_lengths = [len(missing_string)]*num_columns
column_lengths = [len(MISSING_STRING)]*num_columns
for row in data:
for i in xrange(len(row)):
column_lengths[i] = max(column_lengths[i],len(row[i]))
output = table + "\n"
output += "-"*len(table)+"\n\n"
output += table_seperator(num_columns,column_lengths,character="-") + "\n"
output += table_row(data[0],column_lengths,num_columns) + "\n"
output += table_seperator(num_columns,column_lengths,character="=") + "\n"
# Output table header
stream.write(table_name + "\n")
stream.write("~"*len(table_name)+"\n\n")
stream.write(".. tabularcolumns:: |p{40%}|p{20%}|p{20%}|p{20%}|\n\n")
stream.write(".. table::%s\n\n" % table_name)
table_seperator(stream,num_columns,column_lengths,character="-")
stream.write("\n")
table_row(stream,data[0],column_lengths,num_columns)
stream.write("\n")
table_seperator(stream,num_columns,column_lengths,character="=")
stream.write("\n")
# Output table data
for row in data[1:]:
output += table_row(row,column_lengths,num_columns) + "\n"
output += table_seperator(num_columns,column_lengths,character='-') + "\n"
output += "\n\n"
return output
table_row(stream,row,column_lengths,num_columns)
stream.write("\n")
table_seperator(stream,num_columns,column_lengths,character='-')
stream.write("\n")
stream.write("\n\n")
def generate_page(reader,stream,page_title="Coverage Tables"):
stream.write("%s\n" % page_title)
stream.write("="*len(page_title) + "\n\n")
stream.write(".. role:: missing\n")
stream.write(".. role:: partial\n")
stream.write(".. role:: done\n")
stream.write(".. role:: na\n\n")
stream.write("Color Key\n")
stream.write("---------\n")
stream.write(":done:`Complete` ")
stream.write(":partial:`Partial` ")
stream.write(":missing:`Missing` ")
stream.write(":na:`Not applicable`\n\n")
sections,table_names = read_table_titles(reader)
for section_name in sections:
stream.write(section_name + "\n")
stream.write("-"*len(section_name) + "\n\n")
for table_name in table_names[section_name]:
generate_table(reader,stream,table_name)
if __name__ == "__main__":
if len(sys.argv) > 1:
csv_path = './coverage.csv'
output_path = './coverage_table.txt'
if len(sys.argv) == 2:
csv_path = os.path.abspath(sys.argv[1])
else:
csv_path = './coverage.csv'
if len(sys.argv) == 3:
output_path = os.path.abspath(sys.argv[2])
# Figure out dialect and create csv reader
csv_file = open(csv_path,'U')
# dialect = csv.Sniffer().sniff(csv_file.read(1024))
# csv_file.seek(0)
# reader = csv.reader(csv_file, dialect)
reader = csv.reader(csv_file,)
output = open(output_path,'w')
generate_page(reader,output)
csv_file.close()
output.close()
print "Generated %s from %s." % (output_path,csv_path)
reader = csv.reader(open(csv_path,'r'),delimiter=',',quotechar='"')
print page_title
print "="*len(page_title)
print
print ".. role:: missing"
print
for table in table_names:
print generate_table(reader)