diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2016-02-04 21:44:18 -0500 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2016-02-04 21:44:18 -0500 |
| commit | 2f94281f983f86b23f8e5bcdc790fd4e032a0a2d (patch) | |
| tree | a6094f5232663c8b983c9a6122014af34783b122 | |
| parent | 59dfd23bc8abc331b44c5f87e6a1b5dae3dc1036 (diff) | |
| download | anonbib-2f94281f983f86b23f8e5bcdc790fd4e032a0a2d.tar.gz | |
Modernizing Python style
| -rw-r--r-- | BibTeX.py | 18 | ||||
| -rw-r--r-- | entry.py | 77 |
2 files changed, 58 insertions, 37 deletions
@@ -33,7 +33,7 @@ class BibTeX: if k.lower() in self.entries: print >> sys.stderr, "Already have an entry named %s" % k return - self.entries[k.lower()] = ent + self[k] = ent def __contains__(self, key): return key.lower() in self.entries @@ -41,6 +41,12 @@ class BibTeX: def __getitem__(self, key): return self.entries[key.lower()] + def __setitem__(self, key, v): + self.entries[key.lower()] = v + + def __delitem__(self, key): + del self.entries[key.lower()] + def __iter__(self): return iter(self.entries.values()) @@ -51,7 +57,7 @@ class BibTeX: seen.clear() while ent.get('crossref'): try: - cr = self.entries[ent['crossref'].lower()] + cr = self[ent['crossref']] except KeyError: print "No such crossref: %s" % ent['crossref'] break @@ -63,8 +69,8 @@ class BibTeX: if cr.entryLine < ent.entryLine: print "Warning: crossref %s used after declaration" % cr.key - for k in cr.entries.keys(): - if ent.entries.has_key(k): + for k in cr: + if k in ent: print "ERROR: %s defined both in %s and in %s" % ( k, ent.key, cr.key) else: @@ -78,9 +84,9 @@ class BibTeX: rk = "title" for ent in self: - if ent.type in config.OMIT_ENTRIES or not ent.has_key(rk): + if ent.type in config.OMIT_ENTRIES or rk not in ent: ent.check() - del self.entries[ent.key.lower()] + del self[ent.key] class FileIter: @@ -15,40 +15,53 @@ WWW_FIELDS = ['www_section', 'www_important', 'www_remarks', 'www_excerpt_url', 'www_publisher_url', 'www_cache_section', 'www_tags'] +PROCEEDINGS_RE = re.compile( + r'((?:proceedings|workshop record) of(?: the)? )(.*)', + re.I) + + def author_url(author): """Given an author's name, return a URL for his/her homepage.""" for pat, url in config.AUTHOR_RE_LIST: if pat.search(author): return url return None -PROCEEDINGS_RE = re.compile( - r'((?:proceedings|workshop record) of(?: the)? )(.*)', - re.I) # List of fields that appear when we display the entries as BibTeX. -DISPLAYED_FIELDS = [ 'title', 'author', 'journal', 'booktitle', -'school', 'institution', 'organization', 'volume', 'number', 'year', -'month', 'address', 'location', 'chapter', 'edition', 'pages', 'editor', -'howpublished', 'key', 'publisher', 'type', 'note', 'series' ] +DISPLAYED_FIELDS = ['title', 'author', 'journal', 'booktitle', + 'school', 'institution', 'organization', 'volume', + 'number', 'year', 'month', 'address', 'location', + 'chapter', 'edition', 'pages', 'editor', 'howpublished', + 'key', 'publisher', 'type', 'note', 'series'] + class BibTeXEntry: """A single BibTeX entry.""" def __init__(self, type, key, entries): self.type = type # What kind of entry is it? (@book,@injournal,etc) - self.key = key # What key does it have? - self.entries = entries # Map from key to value. - self.entryLine = 0 # Defined on this line number + self.key = key # What key does it have? + self.entries = entries # Map from key to value. + self.entryLine = 0 # Defined on this line number + def get(self, k, v=None): - return self.entries.get(k,v) - def has_key(self, k): - return self.entries.has_key(k) + return self.entries.get(k, v) + + def __contains__(self, k): + return k in self.entries + def __getitem__(self, k): return self.entries[k] + def __setitem__(self, k, v): self.entries[k] = v + def __str__(self): - return self.format(70,1) + return self.format(70, 1) + + def __iter__(self): + return iter(self.entries.keys()) + def getURL(self): """Return the best URL to use for this paper, or None.""" best = None @@ -74,32 +87,34 @@ class BibTeXEntry: else: df = DISPLAYED_FIELDS for f in df: - if not self.entries.has_key(f): + if f not in self: continue v = self.entries[f] if v.startswith("<span class='bad'>"): d.append("%%%%% ERROR: Missing field\n") - d.append("%% %s = {?????},\n"%f) + d.append("%% %s = {?????},\n" % f) continue np = v.translate(ALLCHARS, PRINTINGCHARS) if np: - d.append("%%%%% "+("ERROR: Non-ASCII characters: '%r'\n"%np)) + d.append("%%%%% " + + ("ERROR: Non-ASCII characters: '%r'\n" % np)) d.append(" ") v = v.replace("&", "&") - if invStrings.has_key(v): - s = "%s = %s,\n" %(f, invStrings[v]) + if v in invStrings: + s = "%s = %s,\n" % (f, invStrings[v]) else: s = "%s = {%s},\n" % (f, v) - d.append(_split(s,width,indent)) + d.append(_split(s, width, indent)) d.append("}\n") return "".join(d) + def resolve(self): """Handle post-processing for this entry""" a = self.get('author') if a: self.parsedAuthor = parseAuthor(a) - #print a - #print " => ",repr(self.parsedAuthor) + # print a + # print " => ",repr(self.parsedAuthor) else: self.parsedAuthor = None @@ -136,15 +151,15 @@ class BibTeXEntry: fields = () else: fields = () - errs.append("ERROR: odd type %s"%self.type) + errs.append("ERROR: odd type %s" % self.type) if self.type != 'proceedings': fields += 'title', 'author', 'www_section', 'year' for field in fields: if self.get(field) is None or \ - self.get(field).startswith("<span class='bad'>"): + self.get(field).startswith("<span class='bad'>"): errs.append("ERROR: %s has no %s field" % (self.key, field)) - self.entries[field] = "<span class='bad'>%s:??</span>"%field + self.entries[field] = "<span class='bad'>%s:??</span>" % field if self.type == 'inproceedings': if self.get("booktitle"): @@ -152,8 +167,8 @@ class BibTeXEntry: not self['booktitle'].startswith("{Proceedings of"): errs.append("ERROR: %s's booktitle (%r) doesn't start with 'Proceedings of'" % (self.key, self['booktitle'])) - if self.has_key("pages") and not re.search(r'\d+--\d+', self['pages']): - errs.append("ERROR: Misformed pages in %s"%self.key) + if "pages" in self and not re.search(r'\d+--\d+', self['pages']): + errs.append("ERROR: Misformed pages in %s" % self.key) if self.type == 'proceedings': if self.get('title'): @@ -161,14 +176,14 @@ class BibTeXEntry: for field, value in self.entries.items(): if value.translate(ALLCHARS, PRINTINGCHARS): - errs.append("ERROR: %s.%s has non-ASCII characters"%( + errs.append("ERROR: %s.%s has non-ASCII characters" % ( self.key, field)) if field.startswith("www_") and field not in WWW_FIELDS: - errs.append("ERROR: unknown www field %s"% field) + errs.append("ERROR: unknown www field %s" % field) if value.strip()[-1:] == '.' and \ field not in ("notes", "www_remarks", "author"): - errs.append("ERROR: %s.%s has an extraneous period"%(self.key, - field)) + errs.append("ERROR: %s.%s has an extraneous period" % (self.key, + field)) return errs def biblio_to_html(self): |
