~dosage-dev/dosage/test-mode

« back to all changes in this revision

Viewing changes to admin/compare-table.py

  • Committer: ns
  • Date: 2009-12-01 07:06:02 UTC
  • Revision ID: ns@ww1aviationlinks.cjb.net-20091201070602-f45kw323bb3i7erl
W plugin added WhiteNoise and WorldOfWarcraftEh

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
import sys, os
 
3
if os.sep + 'dosage' in os.path.abspath(sys.argv[0]):
 
4
    sys.path.insert(0, os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir)))
 
5
 
 
6
import dosage.modules
 
7
import csv
 
8
import re
 
9
 
 
10
'''
 
11
This tool builds a list of all comic modules dosage supports, gets some support
 
12
data from a CSV table and generates a nice HTML table to compare Dosage against
 
13
other webcomic aggregators.
 
14
'''
 
15
 
 
16
def constructRow(header, dataDict, count, url = None):
 
17
    row = []
 
18
    url = url or dataDict['URL']
 
19
    for x in header:
 
20
        celldata = dataDict[x]
 
21
        if celldata.split()[0] == '-':
 
22
            cellclass = 'bad'
 
23
        else:
 
24
            cellclass = 'good'
 
25
            if dataDict["broken"] != 'yes':
 
26
                count[x].add(celldata)
 
27
 
 
28
        if x == 'Name':
 
29
            if dataDict["broken"] == 'yes':
 
30
                cellclass = 'bad'
 
31
                count['broken'].add(celldata)
 
32
            if url != None:
 
33
                celldata = '<a href="%s">%s</a>' % (url, celldata)
 
34
        row.append((cellclass, celldata))
 
35
    return row
 
36
 
 
37
 
 
38
def simpleTable(datarows, headerrows = [], footerrows = []):
 
39
    html = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 
40
    "http://www.w3.org/TR/html4/strict.dtd">
 
41
    <html><head><title>Dosage Comparison</title>
 
42
    <script src="sortable.js"></script>
 
43
    <meta http-equiv="Content-Type" value="text/html; charset=utf-8">
 
44
    <link rel="stylesheet" type="text/css" href="compare-table.css">
 
45
    </head><body><p>For easier reading this table is color coded. For the first
 
46
    column green fields show that this comic was found on the net the last time
 
47
    someone checked, red means the comic seems to have disappered. For all
 
48
    other columns green shows a supported comic and red an unsupported
 
49
    comic.</p>
 
50
    <table class="sortable" id="comparison"><thead>
 
51
    '''
 
52
    for row in headerrows:
 
53
        html += '<tr><th>' + '</th><th>'.join(row) + "</th></tr>\n"
 
54
    html += "</thead><tfoot>\n"
 
55
    for row in footerrows:
 
56
        html += '<tr><th>' + '</th><th>'.join(row) + "</th></tr>\n"
 
57
    html += "</tfoot><tbody>\n"
 
58
    for row in datarows:
 
59
        html += '<tr>' + ''.join(['<td class="%s">%s</td>' % x for x in row]) + "</tr>\n"
 
60
    html += "</tbody></table></body>"
 
61
    return html
 
62
 
 
63
def main():
 
64
    base = os.path.splitext(sys.argv[0])[0]
 
65
    csvdata = csv.DictReader(open(base + ".csv", "rb"), dialect='excel-tab')
 
66
    supported = {}
 
67
    unsupported = []
 
68
    for row in csvdata:
 
69
        if row['Dosage'].split()[0] != '-':
 
70
            for strip in row['Dosage'].split(','):
 
71
                supported[strip] = row.copy()
 
72
                supported[strip]['Dosage'] = strip
 
73
        else:
 
74
            unsupported.append(row)
 
75
    header = [x for x in csvdata.fieldnames if x not in ('URL', 'broken')]
 
76
 
 
77
    counters = {}
 
78
    for x in csvdata.fieldnames:
 
79
        counters[x] = set()
 
80
 
 
81
    rows = []
 
82
    notfound = []
 
83
    for moduleName in dosage.modules.items(dosage.modules.__path__[0]):
 
84
        module = dosage.modules.get(moduleName)
 
85
        extradata = supported.pop(moduleName, None)
 
86
        if extradata:
 
87
            try:
 
88
                url = module.latestUrl
 
89
            except:
 
90
                print "WARNING: %s cannot get latest URL!" % (moduleName)
 
91
                url = getattr(module, 'baseUrl', None)
 
92
            rows.append(constructRow(header, extradata, counters, url))
 
93
        else:
 
94
            notfound.append(moduleName)
 
95
    print "The following Dosage modules are not in the CSV table: " + " ".join(notfound)
 
96
    print "The following Dosage modules are in the CSV table but not in Dosage: " + " ".join(supported)
 
97
 
 
98
    # At least add the rows with errors to the table
 
99
    for module in supported:
 
100
        supported[module]['Dosage'] = '- (was %s)' % (module)
 
101
        rows.append(constructRow(header, supported[module], counters))
 
102
 
 
103
    for module in unsupported:
 
104
        rows.append(constructRow(header, module, counters))
 
105
 
 
106
    footer = [str(len(counters[x])) for x in header]
 
107
    footer[0] += ' (+ %i broken)' % (len(counters['broken']))
 
108
 
 
109
    deltags = re.compile(r'<[^<>]+>')
 
110
    rows.sort(cmp = lambda x,y: cmp(deltags.sub("", x[0][1].lower()), deltags.sub("", y[0][1].lower())))
 
111
 
 
112
    f = open(base + '.html', 'w')
 
113
    try:
 
114
        f.write(simpleTable(rows, [header], [footer]))
 
115
    finally:
 
116
        f.close()
 
117
 
 
118
if __name__ == '__main__':
 
119
    main()