2014-04-25 14:13:37 +02:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*- coding: utf8 -*-
|
2014-04-25 16:53:08 +02:00
|
|
|
|
2014-04-28 23:35:06 +02:00
|
|
|
import os
|
|
|
|
import re
|
2014-04-24 00:18:49 +02:00
|
|
|
import shutil
|
2014-04-24 16:18:56 +02:00
|
|
|
import subprocess
|
2014-04-28 23:35:06 +02:00
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import backend
|
|
|
|
import fetcher
|
|
|
|
import tearpages
|
|
|
|
import tools
|
|
|
|
import params
|
2014-04-24 00:18:49 +02:00
|
|
|
from bibtexparser.bparser import BibTexParser
|
2014-04-27 10:06:30 +02:00
|
|
|
from bibtexparser.customization import homogeneize_latex_encoding
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-27 09:46:43 +02:00
|
|
|
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
|
|
|
|
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-25 01:13:19 +02:00
|
|
|
def checkBibtex(filename, bibtex):
|
|
|
|
print("The bibtex entry found for "+filename+" is :")
|
|
|
|
|
2014-04-27 10:06:30 +02:00
|
|
|
bibtex = BibTexParser(bibtex, customization=homogeneize_latex_encoding)
|
2014-04-25 14:13:37 +02:00
|
|
|
bibtex = bibtex.get_entry_dict()
|
2014-04-27 09:46:43 +02:00
|
|
|
if len(bibtex) > 0:
|
|
|
|
bibtex_name = bibtex.keys()[0]
|
|
|
|
bibtex = bibtex[bibtex_name]
|
2014-04-28 23:35:06 +02:00
|
|
|
bibtex_string = backend.parsed2Bibtex(bibtex)
|
2014-04-27 09:46:43 +02:00
|
|
|
else:
|
|
|
|
bibtex_string = ''
|
|
|
|
print(bibtex_string)
|
2014-04-28 22:23:05 +02:00
|
|
|
check = tools.rawInput("Is it correct ? [Y/n] ")
|
2014-04-25 16:53:08 +02:00
|
|
|
|
2014-04-25 14:13:37 +02:00
|
|
|
while check.lower() == 'n':
|
2014-04-27 09:46:43 +02:00
|
|
|
with tempfile.NamedTemporaryFile(suffix=".tmp") as tmpfile:
|
|
|
|
tmpfile.write(bibtex_string)
|
|
|
|
tmpfile.flush()
|
|
|
|
subprocess.call([EDITOR, tmpfile.name])
|
2014-04-28 23:35:06 +02:00
|
|
|
bibtex = BibTexParser(tmpfile.read()+"\n",
|
|
|
|
customization=homogeneize_latex_encoding)
|
2014-04-27 09:46:43 +02:00
|
|
|
|
|
|
|
bibtex = bibtex.get_entry_dict()
|
|
|
|
if len(bibtex) > 0:
|
|
|
|
bibtex_name = bibtex.keys()[0]
|
|
|
|
bibtex = bibtex[bibtex_name]
|
2014-04-28 23:35:06 +02:00
|
|
|
bibtex_string = backend.parsed2Bibtex(bibtex)
|
2014-04-27 09:46:43 +02:00
|
|
|
else:
|
|
|
|
bibtex_string = ''
|
2014-04-25 14:13:37 +02:00
|
|
|
print("\nThe bibtex entry for "+filename+" is :")
|
2014-04-27 09:46:43 +02:00
|
|
|
print(bibtex_string)
|
2014-04-28 22:23:05 +02:00
|
|
|
check = tools.rawInput("Is it correct ? [Y/n] ")
|
2014-04-25 01:13:19 +02:00
|
|
|
return bibtex
|
|
|
|
|
|
|
|
|
2014-04-24 22:39:27 +02:00
|
|
|
def addFile(src, filetype):
|
2014-04-24 00:18:49 +02:00
|
|
|
"""
|
|
|
|
Add a file to the library
|
|
|
|
"""
|
2014-04-24 22:39:27 +02:00
|
|
|
if filetype == 'article' or filetype is None:
|
2014-04-28 23:35:06 +02:00
|
|
|
doi = fetcher.findDOI(src)
|
2014-04-24 22:39:27 +02:00
|
|
|
|
|
|
|
if filetype == 'book' or (filetype is None and doi is False):
|
2014-04-28 23:35:06 +02:00
|
|
|
isbn = fetcher.findISBN(src)
|
2014-04-24 22:39:27 +02:00
|
|
|
|
|
|
|
if doi is False and isbn is False:
|
|
|
|
if filetype is None:
|
2014-04-28 23:35:06 +02:00
|
|
|
tools.warning("Could not determine the DOI or the ISBN for " +
|
|
|
|
src+"."+"Switching to manual entry.")
|
2014-04-25 16:53:08 +02:00
|
|
|
doi_isbn = ''
|
2014-04-24 22:39:27 +02:00
|
|
|
while doi_isbn not in ['doi', 'isbn']:
|
2014-04-28 22:23:05 +02:00
|
|
|
doi_isbn = tools.rawInput("DOI / ISBN ? ").lower()
|
2014-04-24 22:39:27 +02:00
|
|
|
if doi_isbn == 'doi':
|
2014-04-28 22:23:05 +02:00
|
|
|
doi = tools.rawInput('DOI ? ')
|
2014-04-24 22:39:27 +02:00
|
|
|
else:
|
2014-04-28 22:23:05 +02:00
|
|
|
isbn = tools.rawInput('ISBN ? ')
|
2014-04-24 22:39:27 +02:00
|
|
|
elif filetype == 'article':
|
2014-04-28 22:23:05 +02:00
|
|
|
tools.warning("Could not determine the DOI for "+src +
|
2014-04-28 23:35:06 +02:00
|
|
|
", switching to manual entry.")
|
2014-04-28 22:23:05 +02:00
|
|
|
doi = tools.rawInput('DOI ? ')
|
2014-04-24 22:39:27 +02:00
|
|
|
elif filetype == 'book':
|
2014-04-28 22:23:05 +02:00
|
|
|
tools.warning("Could not determine the ISBN for "+src +
|
2014-04-28 23:35:06 +02:00
|
|
|
", switching to manual entry.")
|
2014-04-28 22:23:05 +02:00
|
|
|
isbn = tools.rawInput('ISBN ? ')
|
2014-04-24 22:39:27 +02:00
|
|
|
elif doi is not False:
|
2014-04-24 16:18:56 +02:00
|
|
|
print("DOI for "+src+" is "+doi+".")
|
2014-04-24 22:39:27 +02:00
|
|
|
elif isbn is not False:
|
|
|
|
print("ISBN for "+src+" is "+isbn+".")
|
2014-04-24 16:18:56 +02:00
|
|
|
|
2014-04-26 23:26:25 +02:00
|
|
|
if doi is not False and doi != '':
|
2014-04-25 14:13:37 +02:00
|
|
|
# Add extra \n for bibtexparser
|
2014-04-28 23:35:06 +02:00
|
|
|
bibtex = fetcher.doi2Bib(doi).strip().replace(',', ",\n")+"\n"
|
2014-04-26 23:26:25 +02:00
|
|
|
elif isbn is not False and isbn != '':
|
2014-04-25 14:13:37 +02:00
|
|
|
# Idem
|
2014-04-28 23:35:06 +02:00
|
|
|
bibtex = fetcher.isbn2Bib(isbn).strip()+"\n"
|
2014-04-26 23:26:25 +02:00
|
|
|
else:
|
|
|
|
bibtex = ''
|
2014-04-27 09:46:43 +02:00
|
|
|
|
2014-04-25 01:13:19 +02:00
|
|
|
bibtex = checkBibtex(src, bibtex)
|
2014-04-24 19:38:52 +02:00
|
|
|
|
|
|
|
authors = re.split(' and ', bibtex['author'])
|
2014-04-24 16:18:56 +02:00
|
|
|
|
2014-04-26 23:26:25 +02:00
|
|
|
if bibtex['type'] == 'article':
|
2014-04-24 22:39:27 +02:00
|
|
|
new_name = params.format_articles
|
2014-04-26 23:26:25 +02:00
|
|
|
try:
|
|
|
|
new_name = new_name.replace("%j", bibtex['journal'])
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
elif bibtex['type'] == 'book':
|
2014-04-24 22:39:27 +02:00
|
|
|
new_name = params.format_books
|
|
|
|
|
|
|
|
new_name = new_name.replace("%t", bibtex['title'])
|
2014-04-26 23:26:25 +02:00
|
|
|
try:
|
|
|
|
new_name = new_name.replace("%Y", bibtex['year'])
|
|
|
|
except:
|
|
|
|
pass
|
2014-04-24 19:38:52 +02:00
|
|
|
new_name = new_name.replace("%f", authors[0].split(',')[0].strip())
|
|
|
|
new_name = new_name.replace("%l", authors[-1].split(',')[0].strip())
|
|
|
|
new_name = new_name.replace("%a", ', '.join([i.split(',')[0].strip()
|
2014-04-24 22:39:27 +02:00
|
|
|
for i in authors]))
|
2014-04-24 16:18:56 +02:00
|
|
|
|
2014-04-28 22:23:05 +02:00
|
|
|
new_name = params.folder+tools.slugify(new_name)+tools.getExtension(src)
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-24 21:19:27 +02:00
|
|
|
while os.path.exists(new_name):
|
2014-04-28 22:23:05 +02:00
|
|
|
tools.warning("file "+new_name+" already exists.")
|
|
|
|
default_rename = new_name.replace(tools.getExtension(new_name),
|
|
|
|
" (2)"+tools.getExtension(new_name))
|
|
|
|
rename = tools.rawInput("New name ["+default_rename+"] ? ")
|
2014-04-24 21:19:27 +02:00
|
|
|
if rename == '':
|
|
|
|
new_name = default_rename
|
|
|
|
else:
|
|
|
|
new_name = rename
|
2014-04-25 15:36:54 +02:00
|
|
|
bibtex['file'] = new_name
|
2014-04-27 09:46:43 +02:00
|
|
|
|
2014-04-24 00:18:49 +02:00
|
|
|
try:
|
|
|
|
shutil.copy2(src, new_name)
|
|
|
|
except IOError:
|
2014-04-26 11:52:19 +02:00
|
|
|
new_name = False
|
2014-04-24 16:18:56 +02:00
|
|
|
sys.exit("Unable to move file to library dir " + params.folder+".")
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-26 23:26:25 +02:00
|
|
|
# Remove first page of IOP papers
|
|
|
|
if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article':
|
|
|
|
tearpages.tearpage(new_name)
|
|
|
|
|
2014-04-28 23:35:06 +02:00
|
|
|
backend.bibtexAppend(bibtex)
|
2014-04-26 11:52:19 +02:00
|
|
|
return new_name
|
2014-04-24 00:18:49 +02:00
|
|
|
|
|
|
|
|
2014-04-26 11:52:19 +02:00
|
|
|
def downloadFile(url, filetype):
|
2014-04-28 22:23:05 +02:00
|
|
|
dl, contenttype = fetcher.download(url)
|
2014-04-26 11:52:19 +02:00
|
|
|
|
2014-04-26 18:40:32 +02:00
|
|
|
if dl is not False:
|
2014-04-26 23:26:25 +02:00
|
|
|
tmp = tempfile.NamedTemporaryFile(suffix='.'+contenttype)
|
2014-04-26 18:27:01 +02:00
|
|
|
|
|
|
|
with open(tmp.name, 'w+') as fh:
|
2014-04-26 18:40:32 +02:00
|
|
|
fh.write(dl)
|
2014-04-26 18:27:01 +02:00
|
|
|
new_name = addFile(tmp.name, filetype)
|
|
|
|
tmp.close()
|
2014-04-26 11:52:19 +02:00
|
|
|
return new_name
|
|
|
|
else:
|
2014-04-28 22:23:05 +02:00
|
|
|
tools.warning("Could not fetch "+url)
|
2014-04-26 11:52:19 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-04-24 00:18:49 +02:00
|
|
|
if __name__ == '__main__':
|
2014-04-24 21:19:27 +02:00
|
|
|
try:
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
sys.exit("Usage : TODO")
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-24 21:19:27 +02:00
|
|
|
if sys.argv[1] == 'download':
|
2014-04-26 11:52:19 +02:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
sys.exit("Usage : " + sys.argv[0] +
|
|
|
|
" download FILE [article|book]")
|
|
|
|
|
|
|
|
filetype = None
|
|
|
|
if len(sys.argv) > 3 and sys.argv[3] in ["article", "book"]:
|
|
|
|
filetype = sys.argv[3].lower()
|
|
|
|
|
|
|
|
new_name = downloadFile(sys.argv[2], filetype)
|
|
|
|
if new_name is not False:
|
|
|
|
print(sys.argv[2]+" successfully imported as "+new_name)
|
|
|
|
sys.exit()
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-24 21:19:27 +02:00
|
|
|
if sys.argv[1] == 'import':
|
|
|
|
if len(sys.argv) < 3:
|
2014-04-25 16:53:08 +02:00
|
|
|
sys.exit("Usage : " + sys.argv[0] +
|
|
|
|
" import FILE [article|book]")
|
2014-04-24 22:39:27 +02:00
|
|
|
|
|
|
|
filetype = None
|
|
|
|
if len(sys.argv) > 3 and sys.argv[3] in ["article", "book"]:
|
|
|
|
filetype = sys.argv[3].lower()
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-26 11:52:19 +02:00
|
|
|
new_name = addFile(sys.argv[2], filetype)
|
|
|
|
if new_name is not False:
|
2014-04-27 09:46:43 +02:00
|
|
|
print(sys.argv[2]+" successfully imported as "+new_name+".")
|
2014-04-24 21:19:27 +02:00
|
|
|
sys.exit()
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-25 14:22:34 +02:00
|
|
|
elif sys.argv[1] == 'delete':
|
2014-04-25 15:36:54 +02:00
|
|
|
if len(sys.argv) < 3:
|
|
|
|
sys.exit("Usage : " + sys.argv[0] + " delete FILE|ID")
|
|
|
|
|
2014-04-28 23:35:06 +02:00
|
|
|
confirm = tools.rawInput("Are you sure you want to delete " +
|
|
|
|
sys.argv[2]+" ? [y/N] ")
|
2014-04-26 23:26:25 +02:00
|
|
|
|
|
|
|
if confirm.lower() == 'y':
|
2014-04-28 23:35:06 +02:00
|
|
|
if not backend.deleteId(sys.argv[2]):
|
|
|
|
if not backend.deleteFile(sys.argv[2]):
|
2014-04-28 22:23:05 +02:00
|
|
|
tools.warning("Unable to delete "+sys.argv[2])
|
2014-04-26 23:26:25 +02:00
|
|
|
sys.exit(1)
|
2014-04-25 15:36:54 +02:00
|
|
|
|
2014-04-26 23:26:25 +02:00
|
|
|
print(sys.argv[2]+" successfully deleted.")
|
2014-04-25 15:36:54 +02:00
|
|
|
sys.exit()
|
2014-04-25 14:22:34 +02:00
|
|
|
|
2014-04-24 21:19:27 +02:00
|
|
|
elif sys.argv[1] == 'list':
|
|
|
|
raise Exception('TODO')
|
2014-04-24 00:18:49 +02:00
|
|
|
|
2014-04-24 21:19:27 +02:00
|
|
|
elif sys.argv[1] == 'search':
|
|
|
|
raise Exception('TODO')
|
2014-04-25 15:36:54 +02:00
|
|
|
|
|
|
|
elif sys.argv[1] == 'rebuild':
|
|
|
|
raise Exception('TODO')
|
2014-04-24 21:19:27 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
sys.exit()
|