aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2016-02-04 21:28:27 -0500
committerThibaut Horel <thibaut.horel@gmail.com>2016-02-04 21:28:27 -0500
commit59dfd23bc8abc331b44c5f87e6a1b5dae3dc1036 (patch)
treeaa12195663550b7099011785521eb1c6fbc88dda
parentb0a3a31d5caaeafb63ff415fc9b86b537b759f21 (diff)
downloadanonbib-59dfd23bc8abc331b44c5f87e6a1b5dae3dc1036.tar.gz
Nicer interface for BibTeX class
-rw-r--r--BibTeX.py19
-rwxr-xr-xwriteHTML.py17
2 files changed, 25 insertions, 11 deletions
diff --git a/BibTeX.py b/BibTeX.py
index e07fe63..6558ebd 100644
--- a/BibTeX.py
+++ b/BibTeX.py
@@ -35,10 +35,19 @@ class BibTeX:
return
self.entries[k.lower()] = ent
+ def __contains__(self, key):
+ return key.lower() in self.entries
+
+ def __getitem__(self, key):
+ return self.entries[key.lower()]
+
+ def __iter__(self):
+ return iter(self.entries.values())
+
def resolve(self):
"""Validate all entries in this file, and resolve cross-references"""
seen = {}
- for ent in self.entries.values():
+ for ent in self:
seen.clear()
while ent.get('crossref'):
try:
@@ -68,7 +77,7 @@ class BibTeX:
# entry will have a title.
rk = "title"
- for ent in self.entries.values():
+ for ent in self:
if ent.type in config.OMIT_ENTRIES or not ent.has_key(rk):
ent.check()
del self.entries[ent.key.lower()]
@@ -352,7 +361,7 @@ def parseFile(filename, result=None):
p = Parser(f, {}, result)
r = p.parse()
r.resolve()
- for e in r.entries.values():
+ for e in r:
e.check()
return r
@@ -363,7 +372,7 @@ def parseString(string, result=None):
p = Parser(f, {}, result)
r = p.parse()
r.resolve()
- for e in r.entries.values():
+ for e in r:
e.check()
return r
@@ -375,7 +384,7 @@ if __name__ == '__main__':
r = parseFile(fname)
- for e in r.entries.values():
+ for e in r:
if e.type in ("proceedings", "journal"):
continue
print e.to_html()
diff --git a/writeHTML.py b/writeHTML.py
index c43da6b..871839a 100755
--- a/writeHTML.py
+++ b/writeHTML.py
@@ -24,6 +24,7 @@ def getTemplate(name):
template_s, template_e = template.split("%(entries)s")
return template_s, template_e
+
def pathLength(s):
n = 0
while s:
@@ -33,6 +34,7 @@ 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])
@@ -52,6 +54,7 @@ def writeBody(f, sections, section_urls, cache_path, base_url):
print >>f, e.to_html(cache_path=cache_path, base_url=base_url)
print >>f, "</ul></li>"
+
def writeHTML(f, sections, sectionType, fieldName, choices,
tag, config, cache_url_path, section_urls={}):
"""sections: list of (sectionname, [list of BibTeXEntry])'''
@@ -107,10 +110,11 @@ def writeHTML(f, sections, sectionType, fieldName, choices,
}
header, footer = getTemplate(config.TEMPLATE_FILE)
- print >>f, header%fields
+ print >>f, header % fields
writeBody(f, sections, section_urls, cache_path=cache_url_path,
base_url=root)
- print >>f, footer%fields
+ print >>f, footer % fields
+
def jsonDumper(obj):
if isinstance(obj, BibTeX.BibTeXEntry):
@@ -118,17 +122,18 @@ 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,))
+
def writePageSet(config, bib, tag):
if tag:
- bib_entries = [ b for b in bib.entries.values()
+ bib_entries = [ b for b in bib
if tag in b.get('www_tags', "").split() ]
else:
- bib_entries = bib.entries.copy().values()
+ bib_entries = bib.entries.values()[:]
if not bib_entries:
- print >>sys.stderr, "No entries with tag %r; skipping"%tag
+ print >>sys.stderr, "No entries with tag %r; skipping" % tag
return
tagdir = config.TAG_DIRECTORIES[tag]