gnunetbib

Bibliography (BibTeX, based on AnonBib)
Log | Files | Refs | README | LICENSE

commit 887c2fa6a74d8a008b65658f36d56fba3c420f85
parent 5053fc5ba490263b1d67b89dd5dedc9b5cf97f01
Author: Nils Gillmann <ng0@n0.is>
Date:   Mon,  8 Oct 2018 19:47:59 +0000

writeHTML.py: use future.

Signed-off-by: Nils Gillmann <ng0@n0.is>

Diffstat:
MwriteHTML.py | 109++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 63 insertions(+), 46 deletions(-)

diff --git a/writeHTML.py b/writeHTML.py @@ -1,8 +1,12 @@ #!/usr/bin/python2 # Copyright 2003-2008, Nick Mathewson. See LICENSE for licensing info. +# Copyright 2018 Nils Gillmann -"""Generate indices by author, topic, date, and BibTeX key.""" - +""" +Generate indices by author, topic, date, and BibTeX key. +""" +from __future__ import print_function +from future.utils import raise_with_traceback import sys import re import os @@ -15,9 +19,9 @@ assert sys.version_info[:3] >= (2, 2, 0) os.umask(022) def getTemplate(name): - f = open(name) - template = f.read() - f.close() + template_file = open(name) + template = template_file.read() + template_file.close() template_s, template_e = template.split("%(entries)s") return template_s, template_e @@ -30,31 +34,38 @@ def pathLength(s): s = parent return n -def writeBody(f, sections, section_urls, cache_path, base_url): - '''f: an open file - sections: list of (sectionname, [list of BibTeXEntry]) - section_urls: map from sectionname to external url''' +def writeBody(target_file, sections, section_urls, cache_path, base_url): + """ + target_file: an open file + sections: list of (sectionname, [list of BibTeXEntry]) + section_urls: map from sectionname to external url + """ for s, entries in sections: u = section_urls.get(s) sDisp = re.sub(r'\s+', ' ', s.strip()) sDisp = sDisp.replace(" ", "&nbsp;") if u: - print >>f, ('<li><h3><a name="%s"></a><a href="%s">%s</a></h3>'%( - (BibTeX.url_untranslate(s), u, sDisp))) + print(('<li><h3><a name="%s"></a><a href="%s">%s</a></h3>' + %((BibTeX.url_untranslate(s), u, sDisp))), + file=target_file) else: - print >>f, ('<li><h3><a name="%s">%s</a></h3>'%( - BibTeX.url_untranslate(s), sDisp)) - print >>f, "<ul class='expand'>" + print(('<li><h3><a name="%s">%s</a></h3>' + %(BibTeX.url_untranslate(s), sDisp)), + file=target_file) + print("<ul class='expand'>", file=target_file) for e in entries: - print >>f, e.to_html(cache_path=cache_path, base_url=base_url) - print >>f, "</ul></li>" + print(e.to_html(cache_path=cache_path, base_url=base_url), + file=target_file) + print("</ul></li>", file=target_file) def writeHTML(f, sections, sectionType, fieldName, choices, tag, config, cache_url_path, section_urls={}): - """sections: list of (sectionname, [list of BibTeXEntry])''' - sectionType: str - fieldName: str - choices: list of (choice, url)""" + """ + sections: list of (sectionname, [list of BibTeXEntry])''' + sectionType: str + fieldName: str + choices: list of (choice, url) + """ title = config.TAG_TITLES[tag] short_title = config.TAG_SHORT_TITLES[tag] @@ -103,10 +114,10 @@ def writeHTML(f, sections, sectionType, fieldName, choices, "root" : root,} header, footer = getTemplate(config.TEMPLATE_FILE) - print >>f, header%fields + print(header%fields, file=f) writeBody(f, sections, section_urls, cache_path=cache_url_path, base_url=root) - print >>f, footer%fields + print(footer%fields, file=f) def jsonDumper(obj): if isinstance(obj, BibTeX.BibTeXEntry): @@ -114,7 +125,8 @@ def jsonDumper(obj): e['key'] = obj.key return e else: - raise TypeError("Do not know how to serialize %s"%(obj.__class,)) + #raise TypeError("Do not know how to serialize %s"%(obj.__class,)) + raise_with_traceback(TypeError("Do not know how to serialize %s"%(obj.__class,))) def writePageSet(config, bib, tag): if tag: @@ -124,7 +136,7 @@ def writePageSet(config, bib, tag): bib_entries = bib.entries[:] if not bib_entries: - print >>sys.stderr, "No entries with tag %r; skipping"%tag + print("No entries with tag %r; skipping"%tag, file=sys.stderr) return tagdir = config.TAG_DIRECTORIES[tag] @@ -178,27 +190,27 @@ def writePageSet(config, bib, tag): if entries[-1][0] == 'Unknown': years.append("Unknown") - f = open(os.path.join(outdir, "date.html"), 'w') - writeHTML(f, entries, "Years", "date", + date_file = open(os.path.join(outdir, "date.html"), 'w') + writeHTML(date_file, entries, "Years", "date", (("By topic", "./topic.html"), ("By date", None), ("By author", "./author.html")), tag=tag, config=config, cache_url_path=cache_url_path) - f.close() + date_file.close() ## By author entries, url_map = BibTeX.splitEntriesByAuthor(bib_entries) - f = open(os.path.join(outdir, "author.html"), 'w') - writeHTML(f, entries, "Authors", "author", + author_file = open(os.path.join(outdir, "author.html"), 'w') + writeHTML(author_file, entries, "Authors", "author", (("By topic", "./topic.html"), ("By date", "./date.html"), ("By author", None),), tag=tag, config=config, cache_url_path=cache_url_path, section_urls=url_map) - f.close() + author_file.close() ## The big BibTeX file @@ -210,31 +222,36 @@ def writePageSet(config, bib, tag): ## Finding the root directory is done by writeHTML(), but ## the BibTeX file doesn't use that, so repeat the code here root = "../"*pathLength(config.TAG_DIRECTORIES[tag]) - if root == "": root = "." + if root == "": + root = "." header, footer = getTemplate(config.BIBTEX_TEMPLATE_FILE) - f = open(os.path.join(outdir, "bibtex.html"), 'w') - print >>f, header % {'command_line' : "", - 'title': config.TAG_TITLES[tag], - 'root': root} + bibtex_file = open(os.path.join(outdir, "bibtex.html"), 'w') + print(header%{'command_line' : "", + 'title': config.TAG_TITLES[tag], + 'root': root}, + file=bibtex_file) for ent in entries: - print >>f, ( - ("<tr><td class='bibtex'><a name='%s'>%s</a>" - "<pre class='bibtex'>%s</pre></td></tr>") - %(BibTeX.url_untranslate(ent.key), ent.key, ent.format(90, 8, 1))) - print >>f, footer - f.close() + print((("<tr><td class='bibtex'><a name='%s'>%s</a>" + "<pre class='bibtex'>%s</pre></td></tr>") + % (BibTeX.url_untranslate(ent.key), + ent.key, + ent.format(90, 8, 1))), + file=bibtex_file) + print(footer, file=bibtex_file) + bibtex_file.close() - f = open(os.path.join(outdir, "bibtex.json"), 'w') - json.dump(entries, f, default=jsonDumper) - f.close() + bibtex_json_file = open(os.path.join(outdir, "bibtex.json"), 'w') + json.dump(entries, bibtex_json_file, default=jsonDumper) + bibtex_json_file.close() if __name__ == '__main__': if len(sys.argv) == 2: - print "Loading from %s"%sys.argv[1] + print("Loading from %s"%sys.argv[1]) else: - print >>sys.stderr, "Expected a single configuration file as an argument" + print("Expected a single configuration file as an argument", + file=sys.stderr) sys.exit(1) config.load(sys.argv[1])