summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2018-03-24 19:29:47 +0100
committerRobin Krahl <me@robin-krahl.de>2018-03-24 19:30:35 +0100
commite6703218e956db94d6db164e8686652f6341f39c (patch)
tree0d6b913e71b727c346d7892e3ad19251a91b3983
parent6384d1815135649af4fa647f3205c16ed7dce95a (diff)
downloadbibtool-master.tar.gz
bibtool-master.tar.bz2
Add fetch command to get BibTeX data for DOIHEADmaster
-rw-r--r--bibtool/cli.py27
-rw-r--r--bibtool/extract.py8
2 files changed, 28 insertions, 7 deletions
diff --git a/bibtool/cli.py b/bibtool/cli.py
index 63e3d5b..a6aa1bf 100644
--- a/bibtool/cli.py
+++ b/bibtool/cli.py
@@ -11,9 +11,22 @@ import click
TYPE_FILE = click.Path(exists=True, file_okay=True, dir_okay=False)
+TYPE_FILENAME = click.Path(exists=False, file_okay=True, dir_okay=False)
TYPE_DIR = click.Path(exists=True, file_okay=False, dir_okay=True)
+def _get_bibtex_filename(bibtex_data, directory, output=None):
+ assert bibtex_data.entries
+
+ entry = bibtex_data.entries[0]
+ if not output:
+ output = os.path.join(directory, entry['ID'] + '.bib')
+ elif not os.path.isabs(output):
+ output = os.path.join(directory, output)
+
+ return output
+
+
@click.group()
def cli():
pass
@@ -29,6 +42,13 @@ def _extract(filename, print_all):
return
+@cli.command('fetch')
+@click.argument('doi')
+def _fetch(doi):
+ bibtex_data = bibtool.extract.get_bibtex_data(doi)
+ print(bibtexparser.dumps(bibtex_data))
+
+
@cli.command('import')
@click.argument('filename', type=TYPE_FILE)
@click.option('--directory', type=TYPE_DIR, default=os.getcwd())
@@ -43,11 +63,6 @@ def _import(filename, directory, delete, doi):
bibtex_data = bibtool.extract.get_bibtex_data(doi)
- if not bibtex_data.entries:
- raise Exception('Did not find any Bibtex entry.')
- if len(bibtex_data.entries) > 1:
- raise Exception('Found more than one Bibtex entries.')
-
entry = bibtex_data.entries[0]
click.echo('Found one Bibtex entry: ' + entry['ID'])
if click.confirm('Do you want to edit the entry?'):
@@ -66,7 +81,7 @@ def _import(filename, directory, delete, doi):
click.confirm('Add {} to the repository?'.format(entry['ID']),
default=True, abort=True)
- bibfilename = os.path.join(directory, entry['ID'] + '.bib')
+ bibfilename = _get_bibtex_filename(bibtex_data, directory)
outfileext = os.path.splitext(filename)[1]
outfilename = os.path.join(directory, entry['ID'] + outfileext)
diff --git a/bibtool/extract.py b/bibtool/extract.py
index 5793da6..824c077 100644
--- a/bibtool/extract.py
+++ b/bibtool/extract.py
@@ -57,4 +57,10 @@ def get_bibtex_data(doi):
request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request) as response:
bibtex = response.read().decode('utf-8')
- return bibtexparser.loads(bibtex)
+
+ bibtex_data = bibtexparser.loads(bibtex)
+ if not bibtex_data.entries:
+ raise Exception('Did not find any Bibtex entry.')
+ if len(bibtex_data.entries) > 1:
+ raise Exception('Found more than one Bibtex entries.')
+ return bibtex_data