2012-11-28 17:15:12 +00:00
|
|
|
#!/usr/bin/env python
|
2013-01-09 21:21:19 +00:00
|
|
|
# Copyright (C) 2012-2013 Bastian Kleineidam
|
2012-11-28 17:15:12 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import time
|
2012-12-18 22:39:55 +00:00
|
|
|
import cgi
|
2013-01-28 05:53:27 +00:00
|
|
|
import codecs
|
2012-11-28 17:15:12 +00:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
|
|
|
|
from dosagelib.scraper import get_scrapers
|
2013-01-24 20:42:04 +00:00
|
|
|
from dosagelib.configuration import Version as DosageVersion
|
|
|
|
from scriptutil import load_result, save_result
|
2012-11-28 17:15:12 +00:00
|
|
|
|
2013-01-24 20:42:04 +00:00
|
|
|
json_file = __file__.replace(".py", ".json")
|
|
|
|
|
|
|
|
class Status:
|
2013-01-28 17:52:32 +00:00
|
|
|
"""Status of a comic strip."""
|
2013-01-24 20:42:04 +00:00
|
|
|
ok = "ok"
|
|
|
|
error = "error"
|
|
|
|
orphan = "orphan"
|
|
|
|
|
2013-01-28 05:53:27 +00:00
|
|
|
indextemplate = u"""
|
2012-12-17 20:28:20 +00:00
|
|
|
---
|
|
|
|
extends: base.j2
|
2013-01-24 20:42:04 +00:00
|
|
|
title: Dosage comic list
|
|
|
|
description: a list of comic strips supported by Dosage
|
2012-12-17 20:28:20 +00:00
|
|
|
---
|
2012-12-18 22:39:55 +00:00
|
|
|
{%% block js %%}
|
2013-01-24 20:42:04 +00:00
|
|
|
<script src="{{ media_url('js/masonry.min.js') }}"></script>
|
2012-12-18 22:39:55 +00:00
|
|
|
{%% endblock js %%}
|
2012-12-17 20:28:20 +00:00
|
|
|
|
2012-12-18 22:39:55 +00:00
|
|
|
{%% block content %%}
|
2012-12-17 20:28:20 +00:00
|
|
|
<section id="main-content">
|
|
|
|
|
2013-01-24 20:42:04 +00:00
|
|
|
<h2>Dosage comic list</h2>
|
|
|
|
<div id="comics">
|
2012-12-05 20:52:52 +00:00
|
|
|
%(content)s
|
|
|
|
</div>
|
2013-01-24 20:42:04 +00:00
|
|
|
<script type="text/javascript">
|
|
|
|
(function() {
|
|
|
|
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
|
|
|
|
po.src = 'https://apis.google.com/js/plusone.js';
|
|
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
|
|
|
|
})();
|
|
|
|
</script>
|
2012-12-05 20:52:52 +00:00
|
|
|
<script>
|
|
|
|
window.onload = function() {
|
2013-01-24 20:42:04 +00:00
|
|
|
var wall = new Masonry(document.getElementById('comics'), {
|
2012-12-05 20:52:52 +00:00
|
|
|
columnWidth: 240
|
|
|
|
});
|
|
|
|
};
|
|
|
|
</script>
|
2013-01-24 20:42:04 +00:00
|
|
|
</section>
|
|
|
|
{%% endblock content %%}
|
|
|
|
"""
|
|
|
|
|
2013-01-28 05:53:27 +00:00
|
|
|
comic_template = u"""
|
2013-01-24 20:42:04 +00:00
|
|
|
---
|
|
|
|
extends: base.j2
|
|
|
|
title: Dosage comic %(name)s
|
|
|
|
---
|
|
|
|
{%% block content %%}
|
|
|
|
<section id="main-content">
|
|
|
|
|
|
|
|
<h2>Dosage comic %(name)s</h2>
|
|
|
|
<table class="comicinfo">
|
|
|
|
<tr>
|
|
|
|
<th>Description</th><td>%(description)s</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>Website</th><td><a href="%(url)s">%(url)s</a></td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>Adult content</th><td>%(adult)s</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>Available since</th><td>Dosage v%(since)s</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<th>Status</th><td>%(status)s on %(date)s</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
|
|
|
<div class="g-plusone" data-size="standard" data-annotation="inline" data-width="300"></div>
|
|
|
|
<script type="text/javascript">
|
|
|
|
(function() {
|
|
|
|
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
|
|
|
|
po.src = 'https://apis.google.com/js/plusone.js';
|
|
|
|
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
</section>
|
2012-12-18 22:39:55 +00:00
|
|
|
{%% endblock content %%}
|
2012-11-28 17:15:12 +00:00
|
|
|
"""
|
|
|
|
|
2013-01-28 05:53:27 +00:00
|
|
|
entrytemplate_url = u"""
|
2013-01-24 20:42:04 +00:00
|
|
|
<a href="%(url)s" title="%(title)s" class="%(css)s">%(name)s</a>
|
|
|
|
<div class="g-plusone" data-size="medium" data-annotation="bubble" data-href="%(url)s"></div>
|
|
|
|
"""
|
|
|
|
|
2013-01-28 05:53:27 +00:00
|
|
|
entrytemplate_nourl = u"""
|
2013-01-24 20:42:04 +00:00
|
|
|
<span title="%(title)s" class="%(css)s">%(name)s</span>
|
|
|
|
"""
|
|
|
|
|
2012-11-28 17:15:12 +00:00
|
|
|
|
|
|
|
def get_mtime (filename):
|
|
|
|
"""Return modification time of filename."""
|
2012-12-17 20:28:20 +00:00
|
|
|
return os.path.getmtime(filename)
|
2012-11-28 17:15:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def strdate(t):
|
2013-01-09 21:26:00 +00:00
|
|
|
"""Get formatted date string."""
|
2012-11-28 17:15:12 +00:00
|
|
|
return time.strftime("%d.%m.%Y", time.localtime(t))
|
|
|
|
|
|
|
|
|
2013-01-24 20:42:04 +00:00
|
|
|
def get_testscraper(line):
|
|
|
|
"""Get scraper from test output line."""
|
2012-11-28 17:15:12 +00:00
|
|
|
classname = line.split('::')[1][4:]
|
|
|
|
for scraper in get_scrapers():
|
|
|
|
if scraper.__name__ == classname:
|
2013-01-24 20:42:04 +00:00
|
|
|
return scraper
|
2012-11-28 17:15:12 +00:00
|
|
|
raise ValueError("Scraper %r not found" % classname)
|
|
|
|
|
|
|
|
|
2013-01-24 20:42:04 +00:00
|
|
|
def get_testinfo(filename, modified):
|
|
|
|
"""Maintains a static list of comics which users can vote on.
|
|
|
|
The original set of comic strips is stored in a JSON file which gets
|
|
|
|
updated from the test results.
|
|
|
|
If a comic strip stored in JSON is not found in the test results, it is
|
|
|
|
orphaned.
|
|
|
|
@return: {name -> {
|
|
|
|
"status": Status.*,
|
2013-01-28 05:53:27 +00:00
|
|
|
"url": string,
|
|
|
|
"description": string,
|
2013-01-24 20:42:04 +00:00
|
|
|
"error": string or None,
|
2013-01-28 05:53:27 +00:00
|
|
|
"since": string,
|
|
|
|
"adult": bool,
|
2013-01-24 20:42:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
if os.path.isfile(json_file):
|
|
|
|
testinfo = load_result(json_file)
|
|
|
|
else:
|
|
|
|
testinfo = {}
|
2012-11-28 17:15:12 +00:00
|
|
|
with open(filename, "r") as f:
|
|
|
|
print("Tests parsed: 0", end=" ", file=sys.stderr)
|
|
|
|
num_tests = 0
|
2013-01-24 20:42:04 +00:00
|
|
|
add_error = False
|
|
|
|
keys = []
|
2012-11-28 17:15:12 +00:00
|
|
|
for line in f:
|
|
|
|
if line.startswith((". ", "F ")) and "test_comics" in line:
|
2013-01-24 20:42:04 +00:00
|
|
|
add_error = line.startswith("F ")
|
2012-11-28 17:15:12 +00:00
|
|
|
num_tests += 1
|
2013-01-24 20:42:04 +00:00
|
|
|
key, entry = get_testentry(line)
|
|
|
|
keys.append(key)
|
|
|
|
update_testentry(key, entry, testinfo)
|
|
|
|
elif add_error and line.startswith(" E "):
|
|
|
|
entry["error"] = line[3:].strip()
|
2012-11-28 17:15:12 +00:00
|
|
|
if num_tests % 5 == 0:
|
|
|
|
print(num_tests, end=" ", file=sys.stderr)
|
2013-01-24 20:42:04 +00:00
|
|
|
orphan_entries(keys, testinfo)
|
|
|
|
save_result(testinfo, json_file)
|
|
|
|
return testinfo
|
|
|
|
|
|
|
|
|
|
|
|
def get_testentry(line):
|
|
|
|
"""Get one test entry."""
|
|
|
|
scraper = get_testscraper(line)
|
|
|
|
key = scraper.__name__
|
|
|
|
name = scraper.get_name()
|
|
|
|
if len(name) > 40:
|
|
|
|
name = name[:37] + "..."
|
|
|
|
entry = {
|
|
|
|
"status": Status.ok if line.startswith(". ") else Status.error,
|
|
|
|
"name": name,
|
2013-02-04 20:00:26 +00:00
|
|
|
"url": scraper.url,
|
2013-01-24 20:42:04 +00:00
|
|
|
"description": scraper.description,
|
|
|
|
"error": None,
|
|
|
|
"adult": scraper.adult,
|
|
|
|
}
|
|
|
|
return key, entry
|
|
|
|
|
|
|
|
|
|
|
|
def orphan_entries(keys, testinfo):
|
|
|
|
"""Mark all entries that are in testinfo but not in keys as orphaned."""
|
|
|
|
for key, entry in testinfo.items():
|
|
|
|
if key not in keys:
|
|
|
|
entry["status"] = Status.orphan
|
|
|
|
|
|
|
|
|
|
|
|
def update_testentry(key, entry, testinfo):
|
2013-01-28 17:52:32 +00:00
|
|
|
"""Update one entry with testinfo information."""
|
2013-01-24 20:42:04 +00:00
|
|
|
if key not in testinfo:
|
|
|
|
# add dosage version for this comic
|
|
|
|
# XXX replace this after next release
|
|
|
|
if key.startswith("Arcamax") or key in ("AmazingSuperPowers", "PandyLand"):
|
|
|
|
entry["since"] = DosageVersion
|
|
|
|
else:
|
|
|
|
entry["since"] = "1.8"
|
|
|
|
else:
|
|
|
|
entry["since"] = testinfo[key]["since"]
|
|
|
|
testinfo[key] = entry
|
|
|
|
|
|
|
|
|
|
|
|
def get_html_index(testinfo):
|
|
|
|
"""Get HTML content for test output index."""
|
2012-11-28 17:15:12 +00:00
|
|
|
res = []
|
2013-01-24 20:42:04 +00:00
|
|
|
for key in sorted(testinfo.keys()):
|
|
|
|
entry = testinfo[key]
|
|
|
|
css = entry["status"]
|
|
|
|
url = "comics/%s.html" % key
|
|
|
|
if entry["error"]:
|
|
|
|
title = entry["error"]
|
|
|
|
elif entry["description"]:
|
2013-01-29 17:45:59 +00:00
|
|
|
title = entry["description"][:100]
|
2012-11-28 17:15:12 +00:00
|
|
|
else:
|
2013-01-24 20:42:04 +00:00
|
|
|
title = entry["name"]
|
|
|
|
args = {
|
|
|
|
"url": quote(url),
|
|
|
|
"title": quote(title),
|
|
|
|
"css": quote(css),
|
|
|
|
"name": quote(entry["name"]),
|
|
|
|
}
|
|
|
|
template = entrytemplate_url if url else entrytemplate_nourl
|
|
|
|
entryhtml = template % args
|
|
|
|
res.append('<div class="item">%s</div>' % entryhtml)
|
2012-11-28 17:15:12 +00:00
|
|
|
return os.linesep.join(res)
|
|
|
|
|
|
|
|
|
2013-01-24 20:42:04 +00:00
|
|
|
def write_html(testinfo, outputdir, modified):
|
|
|
|
"""Write index page and all comic pages."""
|
|
|
|
content = get_html_index(testinfo)
|
|
|
|
date = strdate(modified)
|
|
|
|
args = {"date": quote(date), "content": content}
|
|
|
|
fname = os.path.join(outputdir, "comic_index.html")
|
2013-01-28 05:53:27 +00:00
|
|
|
with codecs.open(fname, 'w', 'utf-8') as fp:
|
2013-01-24 20:42:04 +00:00
|
|
|
fp.write(indextemplate % args)
|
|
|
|
comicdir = os.path.join(outputdir, "comics")
|
|
|
|
if not os.path.isdir(comicdir):
|
|
|
|
os.mkdir(comicdir)
|
|
|
|
for key, entry in testinfo.items():
|
|
|
|
write_html_comic(key, entry, comicdir, date)
|
|
|
|
|
|
|
|
|
|
|
|
def write_html_comic(key, entry, outputdir, date):
|
|
|
|
"""Write a comic page."""
|
|
|
|
args = {
|
|
|
|
"url": quote(entry["url"]),
|
|
|
|
"name": quote(entry["name"]),
|
|
|
|
"adult": quote("yes" if entry["adult"] else "no"),
|
|
|
|
"since": quote(entry["since"]),
|
|
|
|
"description": quote(entry["description"]),
|
|
|
|
"status": quote(entry["status"]),
|
|
|
|
"date": quote(date),
|
|
|
|
}
|
|
|
|
fname = os.path.join(outputdir, key+".html")
|
2013-01-28 05:53:27 +00:00
|
|
|
with codecs.open(fname, 'w', 'utf-8') as fp:
|
2013-01-24 20:42:04 +00:00
|
|
|
fp.write(comic_template % args)
|
|
|
|
|
|
|
|
|
|
|
|
def quote(arg):
|
2013-01-29 17:45:59 +00:00
|
|
|
"""CGI-escape and jinja-escape the argument."""
|
|
|
|
return cgi.escape(arg.replace('{', '').replace('}', ''), quote=True)
|
2012-12-18 22:39:55 +00:00
|
|
|
|
|
|
|
|
2012-11-28 17:15:12 +00:00
|
|
|
def main(args):
|
2013-01-09 21:26:00 +00:00
|
|
|
"""Generate HTML output for test result."""
|
2012-12-07 23:45:18 +00:00
|
|
|
filename = args[0]
|
2013-01-24 20:42:04 +00:00
|
|
|
outputdir = args[1]
|
2012-11-28 17:15:12 +00:00
|
|
|
modified = get_mtime(filename)
|
2013-01-24 20:42:04 +00:00
|
|
|
testinfo = get_testinfo(filename, modified)
|
|
|
|
write_html(testinfo, outputdir, modified)
|
2012-11-28 17:15:12 +00:00
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main(sys.argv[1:]))
|