Use $EDITOR

* Use $EDITOR to edit the bibtex entries, as `git commit` or `crontab -e` does.
* Bugfixes that prevented the import of an article without associated
  bibtex entry or without doi.
This commit is contained in:
Phyks 2014-04-27 09:46:43 +02:00
parent 49df58bf70
commit 872d93c6aa
2 changed files with 35 additions and 25 deletions

View File

@ -106,6 +106,7 @@ Here are some sources of inspirations for this project :
A list of ideas and TODO. Don't hesitate to give feedback on the ones you really want or to propose your owns. A list of ideas and TODO. Don't hesitate to give feedback on the ones you really want or to propose your owns.
* Open * Open
* Parameter to disable remote search
* Rebuild * Rebuild
* Webserver interface * Webserver interface
* Various re.compile ? * Various re.compile ?

59
main.py
View File

@ -22,6 +22,8 @@ from bibtexparser.bparser import BibTexParser
from termios import tcflush, TCIOFLUSH from termios import tcflush, TCIOFLUSH
import params import params
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
def rawInput(string): def rawInput(string):
tcflush(sys.stdin, TCIOFLUSH) tcflush(sys.stdin, TCIOFLUSH)
@ -167,11 +169,16 @@ def doi2Bib(doi):
""" """
url = "http://dx.doi.org/" + doi url = "http://dx.doi.org/" + doi
headers = {"accept": "application/x-bibtex"} headers = {"accept": "application/x-bibtex"}
r = requests.get(url, headers=headers) try:
r = requests.get(url, headers=headers)
if r.headers['content-type'] == 'application/x-bibtex': if r.headers['content-type'] == 'application/x-bibtex':
return r.text return r.text
else else:
return ''
except requests.exceptions.ConnectionError:
warning('Unable to contact remote server to get the bibtex entry ' +
'for doi '+doi)
return '' return ''
@ -207,29 +214,31 @@ def checkBibtex(filename, bibtex):
bibtex = StringIO(bibtex) bibtex = StringIO(bibtex)
bibtex = BibTexParser(bibtex) bibtex = BibTexParser(bibtex)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
bibtex_name = bibtex.keys()[0] if len(bibtex) > 0:
bibtex = bibtex[bibtex_name] bibtex_name = bibtex.keys()[0]
print(parsed2Bibtex(bibtex)) bibtex = bibtex[bibtex_name]
bibtex_string = parsed2Bibtex(bibtex)
else:
bibtex_string = ''
print(bibtex_string)
check = rawInput("Is it correct ? [Y/n] ") check = rawInput("Is it correct ? [Y/n] ")
while check.lower() == 'n': while check.lower() == 'n':
fields = [u'type', u'id'] + [i for i in sorted(bibtex) with tempfile.NamedTemporaryFile(suffix=".tmp") as tmpfile:
if i not in ['id', 'type']] tmpfile.write(bibtex_string)
tmpfile.flush()
for field in fields: subprocess.call([EDITOR, tmpfile.name])
new_value = rawInput(field.capitalize()+" ? ["+bibtex[field]+"] ") bibtex = BibTexParser(StringIO(tmpfile.read()+"\n"))
if new_value != '':
bibtex[field] = new_value
while True:
new_field = rawInput("Add a new field (leave empty to skip) ? ")
if new_field == '':
break
new_value = rawInput("Value for field "+new_field+" ? ")
bibtex[new_field] = new_value
bibtex = bibtex.get_entry_dict()
if len(bibtex) > 0:
bibtex_name = bibtex.keys()[0]
bibtex = bibtex[bibtex_name]
bibtex_string = parsed2Bibtex(bibtex)
else:
bibtex_string = ''
print("\nThe bibtex entry for "+filename+" is :") print("\nThe bibtex entry for "+filename+" is :")
print(parsed2Bibtex(bibtex)) print(bibtex_string)
check = rawInput("Is it correct ? [Y/n] ") check = rawInput("Is it correct ? [Y/n] ")
return bibtex return bibtex
@ -276,7 +285,7 @@ def addFile(src, filetype):
bibtex = isbn2Bib(isbn).strip()+"\n" bibtex = isbn2Bib(isbn).strip()+"\n"
else: else:
bibtex = '' bibtex = ''
bibtex = checkBibtex(src, bibtex) bibtex = checkBibtex(src, bibtex)
authors = re.split(' and ', bibtex['author']) authors = re.split(' and ', bibtex['author'])
@ -312,7 +321,7 @@ def addFile(src, filetype):
else: else:
new_name = rename new_name = rename
bibtex['file'] = new_name bibtex['file'] = new_name
try: try:
shutil.copy2(src, new_name) shutil.copy2(src, new_name)
except IOError: except IOError:
@ -417,7 +426,7 @@ if __name__ == '__main__':
new_name = addFile(sys.argv[2], filetype) new_name = addFile(sys.argv[2], filetype)
if new_name is not False: if new_name is not False:
print(sys.argv[2]+ " successfully imported as "+new_name+".") print(sys.argv[2]+" successfully imported as "+new_name+".")
sys.exit() sys.exit()
elif sys.argv[1] == 'delete': elif sys.argv[1] == 'delete':