Address omangin's code review.

* This fixes the logic in the `pubs add` command so that an arxiv ID doesn't
  overwrite a DOI.  This also changes the logic so that if an invalid DOI, ISBN,
  or arXiv ID is provided the program will raise an error.

* The code now uses the bibtexparser package to generate the bibtex file for
  arxiv papers.

* A dedicated exception is added for references that can't be found.
main
J. Antognini 7 years ago
parent 30f5f86c9d
commit ccdbe72eb7

@ -1,9 +1,14 @@
"""Interface for Remote Bibliographic APIs""" """Interface for Remote Bibliographic APIs"""
import requests import requests
import bibtexparser
from bibtexparser.bibdatabase import BibDatabase
import feedparser import feedparser
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from uis import get_ui
class ReferenceNotFoundException(Exception):
pass
def doi2bibtex(doi): def doi2bibtex(doi):
@ -38,22 +43,21 @@ def arxiv2bibtex(arxiv_id):
entry = feed.entries[0] entry = feed.entries[0]
if 'title' not in entry: if 'title' not in entry:
ui = get_ui() raise ReferenceNotFoundException('arXiv ID not found.')
ui.error('malformed arXiv ID: {}'.format(arxiv_id))
bibtex = None
elif 'arxiv_doi' in entry: elif 'arxiv_doi' in entry:
bibtex = doi2bibtex(entry['arxiv_doi']) bibtex = doi2bibtex(entry['arxiv_doi'])
else: else:
# Create a bibentry from the metadata. # Create a bibentry from the metadata.
bibtex = '@misc{{{},\n'.format(arxiv_id) db = BibDatabase()
bibtex += 'Author = {' author_str = ' and '.join(
for i, author in enumerate(entry['authors']): [author['name'] for author in entry['authors']])
bibtex += author['name'] db.entries = [{
if i < len(entry['authors']) - 1: 'ENTRYTYPE': 'misc',
bibtex += ' and ' 'ID': arxiv_id,
bibtex += '},\n' 'author': author_str,
bibtex += 'Title = {{{}}},\n'.format(entry['title'].strip('\n')) 'title': entry['title'],
bibtex += 'Year = {{{}}},\n'.format(entry['published_parsed'].tm_year) 'year': str(entry['published_parsed'].tm_year),
bibtex += 'Eprint = {{arXiv:{}}},\n'.format(arxiv_id) 'Eprint': arxiv_id,
bibtex += '}' }]
bibtex = bibtexparser.dumps(db)
return bibtex return bibtex

@ -84,28 +84,33 @@ def command(conf, args):
# get bibtex entry # get bibtex entry
if bibfile is None: if bibfile is None:
if args.doi is None and args.isbn is None: if args.doi is None and args.isbn is None and args.arxiv is None:
bibentry = bibentry_from_editor(conf, ui, rp) bibentry = bibentry_from_editor(conf, ui, rp)
else: else:
bibentry = None bibentry = None
if args.doi is not None: try:
bibentry_raw = apis.doi2bibtex(args.doi) if args.doi is not None:
bibentry = rp.databroker.verify(bibentry_raw) bibentry_raw = apis.doi2bibtex(args.doi)
if bibentry is None: bibentry = rp.databroker.verify(bibentry_raw)
ui.error('invalid doi {} or unable to retrieve bibfile from it.'.format(args.doi)) if bibentry is None:
if args.isbn is not None: raise apis.ReferenceNotFoundException(
bibentry_raw = apis.isbn2bibtex(args.isbn) 'invalid doi {} or unable to retrieve bibfile from it.'.format(args.doi))
bibentry = rp.databroker.verify(bibentry_raw) elif args.isbn is not None:
if bibentry is None: bibentry_raw = apis.isbn2bibtex(args.isbn)
ui.error('invalid isbn {} or unable to retrieve bibfile from it.'.format(args.isbn)) bibentry = rp.databroker.verify(bibentry_raw)
# TODO distinguish between cases, offer to open the error page in a webbrowser. if bibentry is None:
# TODO offer to confirm/change citekey raise apis.ReferenceNotFoundException(
if args.arxiv is not None: 'invalid isbn {} or unable to retrieve bibfile from it.'.format(args.isbn))
bibentry_raw = apis.arxiv2bibtex(args.arxiv) # TODO distinguish between cases, offer to open the error page in a webbrowser.
bibentry = rp.databroker.verify(bibentry_raw) # TODO offer to confirm/change citekey
if bibentry is None: elif args.arxiv is not None:
ui.error('invalid arxiv id {} or unable to retrieve bibfile from it.'.format(args.arxiv_id)) bibentry_raw = apis.arxiv2bibtex(args.arxiv)
if bibentry is None: bibentry = rp.databroker.verify(bibentry_raw)
if bibentry is None:
raise apis.ReferenceNotFoundException(
'invalid arxiv id {} or unable to retrieve bibfile from it.'.format(args.arxiv_id))
except apis.ReferenceNotFoundException as e:
ui.error(e.message)
ui.exit(1) ui.exit(1)
else: else:
bibentry_raw = content.get_content(bibfile, ui=ui) bibentry_raw = content.get_content(bibfile, ui=ui)

Loading…
Cancel
Save