commit 2ea2082b537718412665bbc8896de6e45075a659
parent d02c932c8a02143ccbfee87addf32f4efb79cd90
Author: Nick Mathewson <nickm@torproject.org>
Date: Sun, 26 Nov 2006 06:20:01 +0000
r9809@Kushana: nickm | 2006-11-26 01:14:14 -0500
Write command-line code in a check for __name__=="__main__" so I can run anonbib stuff through pychecker.
svn:r186
Diffstat:
| M | reconcile.py | | | 93 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
| M | updateCache.py | | | 20 | +++++++++++--------- |
| M | writeHTML.py | | | 175 | ++++++++++++++++++++++++++++++++++++++++--------------------------------------- |
3 files changed, 146 insertions(+), 142 deletions(-)
diff --git a/reconcile.py b/reconcile.py
@@ -242,49 +242,50 @@ def emitKnown(f, ent, matches):
print >>f, "%%"
print >>f, "%"+(ent.format(77,4,1,invStrings).replace("\n", "\n%"))
-if len(sys.argv) != 2:
- print "reconcile.py expects 1 argument"
- sys.exit(1)
-
-print "========= Scanning master =========="
-master = MasterBibTeX()
-master = BibTeX.parseFile(config.MASTER_BIB, master)
-master.buildIndex()
-
-print "========= Scanning new file ========"
-try:
- fn = sys.argv[1]
- input = BibTeX.parseFile(fn)
-except BibTeX.ParseError, e:
- print "Error parsing %s: %s"%(fn,e)
- sys.exit(1)
-
-f = open('tmp.bib', 'w')
-keys = input.newStrings.keys()
-keys.sort()
-for k in keys:
- v = input.newStrings[k]
- print >>f, "@string{%s = {%s}}"%(k,v)
-
-invStrings = input.invStrings
-
-for e in input.entries:
- if not (e.get('title') and e.get('author')):
- print >>f, "%%\n%%%% Not enough information to search for a match: need title and author.\n%%"
- emit(f, e)
- continue
-
- matches = master.includes(e, all=1)
- if not matches:
- print >>f, "%%\n%%%% This entry is probably new: No match found.\n%%"
- emit(f, e)
- else:
- print >>f, "%%"
- print >>f, "%%%% Possible match found for this entry; max goodness",\
- matches[-1][0], "\n%%"
- emitKnown(f, e, matches)
-
-if not all_ok:
- print >>f, "\n\n\nErrors remain; not finished.\n"
-
-f.close()
+if __name__ == '__main__':
+ if len(sys.argv) != 2:
+ print "reconcile.py expects 1 argument"
+ sys.exit(1)
+
+ print "========= Scanning master =========="
+ master = MasterBibTeX()
+ master = BibTeX.parseFile(config.MASTER_BIB, master)
+ master.buildIndex()
+
+ print "========= Scanning new file ========"
+ try:
+ fn = sys.argv[1]
+ input = BibTeX.parseFile(fn)
+ except BibTeX.ParseError, e:
+ print "Error parsing %s: %s"%(fn,e)
+ sys.exit(1)
+
+ f = open('tmp.bib', 'w')
+ keys = input.newStrings.keys()
+ keys.sort()
+ for k in keys:
+ v = input.newStrings[k]
+ print >>f, "@string{%s = {%s}}"%(k,v)
+
+ invStrings = input.invStrings
+
+ for e in input.entries:
+ if not (e.get('title') and e.get('author')):
+ print >>f, "%%\n%%%% Not enough information to search for a match: need title and author.\n%%"
+ emit(f, e)
+ continue
+
+ matches = master.includes(e, all=1)
+ if not matches:
+ print >>f, "%%\n%%%% This entry is probably new: No match found.\n%%"
+ emit(f, e)
+ else:
+ print >>f, "%%"
+ print >>f, "%%%% Possible match found for this entry; max goodness",\
+ matches[-1][0], "\n%%"
+ emitKnown(f, e, matches)
+
+ if not all_ok:
+ print >>f, "\n\n\nErrors remain; not finished.\n"
+
+ f.close()
diff --git a/updateCache.py b/updateCache.py
@@ -150,12 +150,14 @@ def downloadAll(bibtex, missingOnly=0):
return errors
-if len(sys.argv) == 2:
- print "Loading from %s"%sys.argv[1]
-else:
- print >>sys.stderr, "Expected a single configuration file as an argument"
- sys.exit(1)
-config.load(sys.argv[1])
-
-bib = BibTeX.parseFile(config.MASTER_BIB)
-downloadAll(bib,missingOnly=1)
+if __name__ == '__main__':
+
+ if len(sys.argv) == 2:
+ print "Loading from %s"%sys.argv[1]
+ else:
+ print >>sys.stderr, "Expected a single configuration file as an argument"
+ sys.exit(1)
+ config.load(sys.argv[1])
+
+ bib = BibTeX.parseFile(config.MASTER_BIB)
+ downloadAll(bib,missingOnly=1)
diff --git a/writeHTML.py b/writeHTML.py
@@ -75,91 +75,92 @@ def writeHTML(f, sections, sectionType, fieldName, choices, section_urls={}):
writeBody(f, sections, section_urls)
print >>f, footer%fields
-if len(sys.argv) == 2:
- print "Loading from %s"%sys.argv[1]
-else:
- print >>sys.stderr, "Expected a single configuration file as an argument"
- sys.exit(1)
-config.load(sys.argv[1])
-
-bib = BibTeX.parseFile(config.MASTER_BIB)
-
-##### Sorted views:
-
-## By topic.
-
-entries = BibTeX.sortEntriesBy(bib.entries, "www_section", "ZZZZZZZZZZZZZZZZZ")
-entries = BibTeX.splitSortedEntriesBy(entries, "www_section")
-if entries[-1][0].startswith("<span class='bad'>"):
- entries[-1] = ("Miscellaneous", entries[-1][1])
-
-entries = [ (s, BibTeX.sortEntriesByDate(ents))
- for s, ents in entries
- ]
-
-f = open(os.path.join(config.OUTPUT_DIR,"topic.html"), 'w')
-writeHTML(f, entries, "Topics", "topic",
- (("By topic", None),
- ("By date", "./date.html"),
- ("By author", "./author.html")
- ))
-f.close()
-
-## By date.
-
-entries = BibTeX.sortEntriesByDate(bib.entries)
-entries = BibTeX.splitSortedEntriesBy(entries, 'year')
-for idx in -1, -2:
- if entries[idx][0].startswith("<span class='bad'>"):
- entries[idx] = ("Unknown", entries[idx][1])
- elif entries[idx][0].startswith("forthcoming"):
- entries[idx] = ("Forthcoming", entries[idx][1])
-sections = [ ent[0] for ent in entries ]
-
-first_year = int(entries[0][1][0]['year'])
-try:
- last_year = int(entries[-1][1][0].get('year'))
-except ValueError:
- last_year = int(entries[-2][1][0].get('year'))
-
-years = map(str, range(first_year, last_year+1))
-if entries[-1][0] == 'Unknown':
- years.append("Unknown")
-
-f = open(os.path.join(config.OUTPUT_DIR,"date.html"), 'w')
-writeHTML(f, entries, "Years", "date",
- (("By topic", "./topic.html"),
- ("By date", None),
- ("By author", "./author.html")
- ))
-f.close()
-
-## By author
-entries, url_map = BibTeX.splitEntriesByAuthor(bib.entries)
-
-f = open(os.path.join(config.OUTPUT_DIR,"author.html"), 'w')
-writeHTML(f, entries, "Authors", "author",
- (("By topic", "./topic.html"),
- ("By date", "./date.html"),
- ("By author", None),
- ),
- url_map)
-f.close()
-
-## The big BibTeX file
-
-entries = bib.entries[:]
-entries = [ (ent.key, ent) for ent in entries ]
-entries.sort()
-entries = [ ent[1] for ent in entries ]
-header,footer = getTemplate(config.BIBTEX_TEMPLATE_FILE)
-f = open(os.path.join(config.OUTPUT_DIR,"bibtex.html"), 'w')
-print >>f, header % { 'command_line' : "" }
-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()
+if __name__ == '__main__':
+ if len(sys.argv) == 2:
+ print "Loading from %s"%sys.argv[1]
+ else:
+ print >>sys.stderr, "Expected a single configuration file as an argument"
+ sys.exit(1)
+ config.load(sys.argv[1])
+
+ bib = BibTeX.parseFile(config.MASTER_BIB)
+
+ ##### Sorted views:
+
+ ## By topic.
+
+ entries = BibTeX.sortEntriesBy(bib.entries, "www_section", "ZZZZZZZZZZZZZZZZZ")
+ entries = BibTeX.splitSortedEntriesBy(entries, "www_section")
+ if entries[-1][0].startswith("<span class='bad'>"):
+ entries[-1] = ("Miscellaneous", entries[-1][1])
+
+ entries = [ (s, BibTeX.sortEntriesByDate(ents))
+ for s, ents in entries
+ ]
+
+ f = open(os.path.join(config.OUTPUT_DIR,"topic.html"), 'w')
+ writeHTML(f, entries, "Topics", "topic",
+ (("By topic", None),
+ ("By date", "./date.html"),
+ ("By author", "./author.html")
+ ))
+ f.close()
+
+ ## By date.
+
+ entries = BibTeX.sortEntriesByDate(bib.entries)
+ entries = BibTeX.splitSortedEntriesBy(entries, 'year')
+ for idx in -1, -2:
+ if entries[idx][0].startswith("<span class='bad'>"):
+ entries[idx] = ("Unknown", entries[idx][1])
+ elif entries[idx][0].startswith("forthcoming"):
+ entries[idx] = ("Forthcoming", entries[idx][1])
+ sections = [ ent[0] for ent in entries ]
+
+ first_year = int(entries[0][1][0]['year'])
+ try:
+ last_year = int(entries[-1][1][0].get('year'))
+ except ValueError:
+ last_year = int(entries[-2][1][0].get('year'))
+
+ years = map(str, range(first_year, last_year+1))
+ if entries[-1][0] == 'Unknown':
+ years.append("Unknown")
+
+ f = open(os.path.join(config.OUTPUT_DIR,"date.html"), 'w')
+ writeHTML(f, entries, "Years", "date",
+ (("By topic", "./topic.html"),
+ ("By date", None),
+ ("By author", "./author.html")
+ ))
+ f.close()
+
+ ## By author
+ entries, url_map = BibTeX.splitEntriesByAuthor(bib.entries)
+
+ f = open(os.path.join(config.OUTPUT_DIR,"author.html"), 'w')
+ writeHTML(f, entries, "Authors", "author",
+ (("By topic", "./topic.html"),
+ ("By date", "./date.html"),
+ ("By author", None),
+ ),
+ url_map)
+ f.close()
+
+ ## The big BibTeX file
+
+ entries = bib.entries[:]
+ entries = [ (ent.key, ent) for ent in entries ]
+ entries.sort()
+ entries = [ ent[1] for ent in entries ]
+ header,footer = getTemplate(config.BIBTEX_TEMPLATE_FILE)
+ f = open(os.path.join(config.OUTPUT_DIR,"bibtex.html"), 'w')
+ print >>f, header % { 'command_line' : "" }
+ 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()