Convert to Python 3

This commit is contained in:
Jaculabilis 2017-11-02 12:40:25 -05:00
parent 0bc0c1ccdd
commit 40d55997a5
1 changed files with 29 additions and 33 deletions

View File

@ -31,10 +31,6 @@ def titlestrip(s):
if s.startswith("A "): return s[2:] if s.startswith("A "): return s[2:]
return s return s
def cmp_title(x, y):
"""Compares strings in titular order, ignoring prefixed articles."""
return cmp(titlestrip(x), titlestrip(y))
def link_formatter(written_articles): def link_formatter(written_articles):
""" """
Creates a lambda that formats citation links and handles the phantom class. Creates a lambda that formats citation links and handles the phantom class.
@ -137,19 +133,19 @@ def parse_lex_from_directory(directory):
Output: a list of parsed lex file structures Output: a list of parsed lex file structures
""" """
lexes = [] lexes = []
print "Reading lex files from", directory print("Reading lex files from", directory)
for filename in os.listdir(directory): for filename in os.listdir(directory):
path = directory + filename path = directory + filename
# Read only .lex files # Read only .lex files
if path[-4:] == ".lex": if path[-4:] == ".lex":
print " Parsing", path, print(" Parsing", path, end=" ")
with open(path) as lex_file: with open(path) as lex_file:
lex_raw = lex_file.read() lex_raw = lex_file.read()
parsed_lex = parse_lex(lex_raw) parsed_lex = parse_lex(lex_raw)
if "error" in parsed_lex: if "error" in parsed_lex:
print "ERROR:", parsed_lex["error"] print("ERROR:", parsed_lex["error"])
else: else:
print "SUCCESS:", parsed_lex["title"] print("SUCCESS:", parsed_lex["title"])
lexes.append(parsed_lex) lexes.append(parsed_lex)
return lexes return lexes
@ -197,7 +193,7 @@ def make_cite_map(lex_list):
cite_map = {} cite_map = {}
for lex in lex_list: for lex in lex_list:
cited_titles = [cite_tuple[1] for format_id, cite_tuple in lex["citations"].items()] cited_titles = [cite_tuple[1] for format_id, cite_tuple in lex["citations"].items()]
cite_map[lex["title"]] = sorted(set(cited_titles), cmp=cmp_title) cite_map[lex["title"]] = sorted(set(cited_titles), key=titlestrip)
return cite_map return cite_map
def format_content(lex, format_func): def format_content(lex, format_func):
@ -225,7 +221,7 @@ def citation_lists(title, cite_map):
citers = [citer_title citers = [citer_title
for citer_title, cited_titles in cite_map.items() for citer_title, cited_titles in cite_map.items()
if title in cited_titles] if title in cited_titles]
return cite_map[title], citers return cite_map[title] if title in cite_map else [], citers
def build_article_page(lex, cite_map, config): def build_article_page(lex, cite_map, config):
""" """
@ -241,7 +237,9 @@ def build_article_page(lex, cite_map, config):
# Build the article citeblock # Build the article citeblock
cites, citedby = citation_lists(lex["title"], cite_map) cites, citedby = citation_lists(lex["title"], cite_map)
cites_str = " | ".join([lf(None, title, title) for title in cites]) cites_str = " | ".join([lf(None, title, title) for title in cites])
if len(cites_str) < 1: cites_str = "--"
citedby_str = " | ".join([lf(None, title, title) for title in citedby]) citedby_str = " | ".join([lf(None, title, title) for title in citedby])
if len(citedby_str) < 1: citedby_str = "--"
citeblock = ""\ citeblock = ""\
"<div class=\"content citeblock\">\n"\ "<div class=\"content citeblock\">\n"\
"<p>Citations: {cites}</p>\n"\ "<p>Citations: {cites}</p>\n"\
@ -335,10 +333,8 @@ def build_index_page(cite_map, config):
Output: the HTML of the index page Output: the HTML of the index page
""" """
# Count up all the titles # Count up all the titles
titles = sorted( titles = set(cite_map.keys()) | set([title for cited_titles in cite_map.values() for title in cited_titles])
(set(cite_map.keys()) | titles = sorted(set(titles), key=titlestrip)
set([title for cited_titles in cite_map.values() for title in cited_titles]))
, cmp=cmp_title)
content = "" content = ""
if len(titles) == len(cite_map.keys()): if len(titles) == len(cite_map.keys()):
content = "<p>There are <b>{0}</b> entries in this lexicon.</p>\n<ul>\n".format(len(titles)) content = "<p>There are <b>{0}</b> entries in this lexicon.</p>\n<ul>\n".format(len(titles))
@ -496,7 +492,7 @@ def build_statistics_page(cite_map, config):
def command_build(argv): def command_build(argv):
if len(argv) >= 3 and (argv[2] != "partial" and argv[2] != "full"): if len(argv) >= 3 and (argv[2] != "partial" and argv[2] != "full"):
print "unknown build type: " + argv[2] print("unknown build type: " + argv[2])
return return
# Set up the entries # Set up the entries
config = load_config() config = load_config()
@ -507,68 +503,68 @@ def command_build(argv):
written_entries = cite_map.keys() written_entries = cite_map.keys()
phantom_entries = set([title for cites in cite_map.values() for title in cites if title not in written_entries]) phantom_entries = set([title for cites in cite_map.values() for title in cites if title not in written_entries])
# Clear the folder # Clear the folder
print "Clearing old HTML files" print("Clearing old HTML files")
for filename in os.listdir("out/"): for filename in os.listdir("out/"):
if filename[-5:] == ".html": if filename[-5:] == ".html":
os.remove("out/" + filename) os.remove("out/" + filename)
# Write the written entries # Write the written entries
print "Writing written articles..." print("Writing written articles...")
for lex in lexes: for lex in lexes:
page = build_article_page(lex, cite_map, config) page = build_article_page(lex, cite_map, config)
with open("out/" + lex["filename"] + ".html", "w") as f: with open("out/" + lex["filename"] + ".html", "w") as f:
f.write(page) f.write(page)
print " Wrote " + lex["title"] print(" Wrote " + lex["title"])
# Write the unwritten entries # Write the unwritten entries
if len(phantom_entries) > 0: if len(phantom_entries) > 0:
if len(argv) < 3 or argv[2] == "partial": if len(argv) < 3 or argv[2] == "partial":
print "Writing phantom articles..." print("Writing phantom articles...")
for title in phantom_entries: for title in phantom_entries:
page = build_phantom_page(title, cite_map, config) page = build_phantom_page(title, cite_map, config)
with open("out/" + as_filename(title) + ".html", "w") as f: with open("out/" + as_filename(title) + ".html", "w") as f:
f.write(page) f.write(page)
print " Wrote " + title print(" Wrote " + title)
elif argv[2] == "full": elif argv[2] == "full":
print "Writing stub articles..." print("Writing stub articles...")
for title in phantom_entries: for title in phantom_entries:
page = build_stub_page(title, cite_map, config) page = build_stub_page(title, cite_map, config)
with open("out/" + as_filename(title) + ".html", "w") as f: with open("out/" + as_filename(title) + ".html", "w") as f:
f.write(page) f.write(page)
print " Wrote " + title print(" Wrote " + title)
else: else:
print "ERROR: build type was " + argv[2] print("ERROR: build type was " + argv[2])
return return
# Write the default pages # Write the default pages
print "Writing default pages" print("Writing default pages")
page = build_rules_page(config) page = build_rules_page(config)
with open("out/rules.html", "w") as f: with open("out/rules.html", "w") as f:
f.write(page) f.write(page)
print " Wrote Rules" print(" Wrote Rules")
page = build_formatting_page(config) page = build_formatting_page(config)
with open("out/formatting.html", "w") as f: with open("out/formatting.html", "w") as f:
f.write(page) f.write(page)
print " Wrote Formatting" print(" Wrote Formatting")
page = build_index_page(cite_map, config) page = build_index_page(cite_map, config)
with open("out/index.html", "w") as f: with open("out/index.html", "w") as f:
f.write(page) f.write(page)
print " Wrote Index" print(" Wrote Index")
page = build_session_page(config) page = build_session_page(config)
with open("out/session.html", "w") as f: with open("out/session.html", "w") as f:
f.write(page) f.write(page)
print " Wrote Session" print(" Wrote Session")
page = build_statistics_page(cite_map, config) page = build_statistics_page(cite_map, config)
with open("out/stats.html", "w") as f: with open("out/stats.html", "w") as f:
f.write(page) f.write(page)
print " Wrote Statistics" print(" Wrote Statistics")
def main(): def main():
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Available commands:" print("Available commands:")
print " - build [partial] : Build the lexicon and generate phantom stubs for all unwritten articles." print(" - build [partial] : Build the lexicon and generate phantom stubs for all unwritten articles.")
print " - build full : Build the lexicon and generate Ersatz pages for all unwritten articles." print(" - build full : Build the lexicon and generate Ersatz pages for all unwritten articles.")
elif sys.argv[1] == "build": elif sys.argv[1] == "build":
command_build(sys.argv) command_build(sys.argv)
else: else:
print "Unknown command: " + sys.argv[1] print("Unknown command: " + sys.argv[1])
if __name__ == "__main__": if __name__ == "__main__":
main() main()