Refactor in progress
TODO : * Use new API from bibtex-parser * Split addFile / downloadFile in main to a function in main (interface) and one in backend
This commit is contained in:
parent
cce4fd50bb
commit
46fc17b2da
82
backend.py
Normal file
82
backend.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env python2
|
||||||
|
# coding=utf8
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tools
|
||||||
|
import params
|
||||||
|
from bibtexparser.bparser import BibTexParser
|
||||||
|
from bibtexparser.customization import homogeneize_latex_encoding
|
||||||
|
from bibtexparser.bwriter import bibtex as bibTexWriter
|
||||||
|
|
||||||
|
|
||||||
|
def parsed2Bibtex(parsed):
|
||||||
|
"""Convert a single bibtex entry dict to bibtex string"""
|
||||||
|
bibtex = '@'+parsed['type']+'{'+parsed['id']+",\n"
|
||||||
|
|
||||||
|
for field in [i for i in sorted(parsed) if i not in ['type', 'id']]:
|
||||||
|
bibtex += "\t"+field+"={"+parsed[field]+"},\n"
|
||||||
|
bibtex += "}\n"
|
||||||
|
return bibtex
|
||||||
|
|
||||||
|
|
||||||
|
def bibtexAppend(data):
|
||||||
|
"""Append data to the main bibtex file
|
||||||
|
|
||||||
|
data is a dict for one entry in bibtex, as the one from bibtexparser output
|
||||||
|
"""
|
||||||
|
with open(params.folder+'index.bib', 'a') as fh:
|
||||||
|
fh.write(parsed2Bibtex(data)+"\n")
|
||||||
|
|
||||||
|
|
||||||
|
def bibtexRewrite(data):
|
||||||
|
"""Rewrite the bibtex index file.
|
||||||
|
|
||||||
|
data is a dict of bibtex entry dict.
|
||||||
|
"""
|
||||||
|
bibtex = ''
|
||||||
|
for entry in data.keys():
|
||||||
|
bibtex += parsed2Bibtex(data[entry])+"\n"
|
||||||
|
with open(params.folder+'index.bib', 'w') as fh:
|
||||||
|
fh.write(bibtex)
|
||||||
|
|
||||||
|
|
||||||
|
def deleteId(ident):
|
||||||
|
"""Delete a file based on its id in the bibtex file"""
|
||||||
|
with open(params.folder+'index.bib', 'r') as fh:
|
||||||
|
bibtex = BibTexParser(fh.read(),
|
||||||
|
customization=homogeneize_latex_encoding)
|
||||||
|
bibtex = bibtex.get_entry_dict()
|
||||||
|
|
||||||
|
if ident not in bibtex.keys():
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
os.remove(bibtex[ident]['file'])
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to delete file associated to id "+ident+" : " +
|
||||||
|
bibtex[ident]['file'])
|
||||||
|
del(bibtex[ident])
|
||||||
|
bibtexRewrite(bibtex)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def deleteFile(filename):
|
||||||
|
"""Delete a file based on its filename"""
|
||||||
|
with open(params.folder+'index.bib', 'r') as fh:
|
||||||
|
bibtex = BibTexParser(fh.read(),
|
||||||
|
customization=homogeneize_latex_encoding)
|
||||||
|
bibtex = bibtex.get_entry_dict()
|
||||||
|
|
||||||
|
found = False
|
||||||
|
for key in bibtex.keys():
|
||||||
|
if bibtex[key]['file'] == filename:
|
||||||
|
found = True
|
||||||
|
try:
|
||||||
|
os.remove(bibtex[key]['file'])
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to delete file associated to id " +
|
||||||
|
key+" : "+bibtex[key]['file'])
|
||||||
|
del(bibtex[key])
|
||||||
|
if found:
|
||||||
|
bibtexRewrite(bibtex)
|
||||||
|
return found
|
@ -33,9 +33,9 @@ def download(url):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
return r.content, contenttype
|
return r.content, contenttype
|
||||||
# TODO : except trop large
|
except requests.exceptions.RequestException:
|
||||||
except:
|
tools.warning("Unable to get "+url+" using roxy "+proxy+". It " +
|
||||||
tools.warning("Proxy "+proxy+" not available.")
|
"may not be available.")
|
||||||
continue
|
continue
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
108
main.py
108
main.py
@ -1,34 +1,23 @@
|
|||||||
#!/usr/bin/env python2
|
#!/usr/bin/env python2
|
||||||
# -*- coding: utf8 -*-
|
# -*- coding: utf8 -*-
|
||||||
|
|
||||||
import tools
|
|
||||||
import fetcher
|
|
||||||
import backend
|
|
||||||
import tearpages
|
|
||||||
import sys
|
|
||||||
import shutil
|
|
||||||
import tempfile
|
|
||||||
import subprocess
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import backend
|
||||||
|
import fetcher
|
||||||
|
import tearpages
|
||||||
|
import tools
|
||||||
|
import params
|
||||||
from bibtexparser.bparser import BibTexParser
|
from bibtexparser.bparser import BibTexParser
|
||||||
from bibtexparser.customization import homogeneize_latex_encoding
|
from bibtexparser.customization import homogeneize_latex_encoding
|
||||||
import params
|
|
||||||
|
|
||||||
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
|
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
|
||||||
|
|
||||||
|
|
||||||
def parsed2Bibtex(parsed):
|
|
||||||
"""
|
|
||||||
Convert a single bibtex entry dict to bibtex string
|
|
||||||
"""
|
|
||||||
bibtex = '@'+parsed['type']+'{'+parsed['id']+",\n"
|
|
||||||
|
|
||||||
for field in [i for i in sorted(parsed) if i not in ['type', 'id']]:
|
|
||||||
bibtex += "\t"+field+"={"+parsed[field]+"},\n"
|
|
||||||
bibtex += "}\n"
|
|
||||||
return bibtex
|
|
||||||
|
|
||||||
|
|
||||||
def checkBibtex(filename, bibtex):
|
def checkBibtex(filename, bibtex):
|
||||||
print("The bibtex entry found for "+filename+" is :")
|
print("The bibtex entry found for "+filename+" is :")
|
||||||
|
|
||||||
@ -37,7 +26,7 @@ def checkBibtex(filename, bibtex):
|
|||||||
if len(bibtex) > 0:
|
if len(bibtex) > 0:
|
||||||
bibtex_name = bibtex.keys()[0]
|
bibtex_name = bibtex.keys()[0]
|
||||||
bibtex = bibtex[bibtex_name]
|
bibtex = bibtex[bibtex_name]
|
||||||
bibtex_string = parsed2Bibtex(bibtex)
|
bibtex_string = backend.parsed2Bibtex(bibtex)
|
||||||
else:
|
else:
|
||||||
bibtex_string = ''
|
bibtex_string = ''
|
||||||
print(bibtex_string)
|
print(bibtex_string)
|
||||||
@ -48,13 +37,14 @@ def checkBibtex(filename, bibtex):
|
|||||||
tmpfile.write(bibtex_string)
|
tmpfile.write(bibtex_string)
|
||||||
tmpfile.flush()
|
tmpfile.flush()
|
||||||
subprocess.call([EDITOR, tmpfile.name])
|
subprocess.call([EDITOR, tmpfile.name])
|
||||||
bibtex = BibTexParser(tmpfile.read()+"\n", customization=homogeneize_latex_encoding)
|
bibtex = BibTexParser(tmpfile.read()+"\n",
|
||||||
|
customization=homogeneize_latex_encoding)
|
||||||
|
|
||||||
bibtex = bibtex.get_entry_dict()
|
bibtex = bibtex.get_entry_dict()
|
||||||
if len(bibtex) > 0:
|
if len(bibtex) > 0:
|
||||||
bibtex_name = bibtex.keys()[0]
|
bibtex_name = bibtex.keys()[0]
|
||||||
bibtex = bibtex[bibtex_name]
|
bibtex = bibtex[bibtex_name]
|
||||||
bibtex_string = parsed2Bibtex(bibtex)
|
bibtex_string = backend.parsed2Bibtex(bibtex)
|
||||||
else:
|
else:
|
||||||
bibtex_string = ''
|
bibtex_string = ''
|
||||||
print("\nThe bibtex entry for "+filename+" is :")
|
print("\nThe bibtex entry for "+filename+" is :")
|
||||||
@ -68,15 +58,15 @@ def addFile(src, filetype):
|
|||||||
Add a file to the library
|
Add a file to the library
|
||||||
"""
|
"""
|
||||||
if filetype == 'article' or filetype is None:
|
if filetype == 'article' or filetype is None:
|
||||||
doi = findDOI(src)
|
doi = fetcher.findDOI(src)
|
||||||
|
|
||||||
if filetype == 'book' or (filetype is None and doi is False):
|
if filetype == 'book' or (filetype is None and doi is False):
|
||||||
isbn = findISBN(src)
|
isbn = fetcher.findISBN(src)
|
||||||
|
|
||||||
if doi is False and isbn is False:
|
if doi is False and isbn is False:
|
||||||
if filetype is None:
|
if filetype is None:
|
||||||
tools.warning("Could not determine the DOI or the ISBN for "+src+"." +
|
tools.warning("Could not determine the DOI or the ISBN for " +
|
||||||
"Switching to manual entry.")
|
src+"."+"Switching to manual entry.")
|
||||||
doi_isbn = ''
|
doi_isbn = ''
|
||||||
while doi_isbn not in ['doi', 'isbn']:
|
while doi_isbn not in ['doi', 'isbn']:
|
||||||
doi_isbn = tools.rawInput("DOI / ISBN ? ").lower()
|
doi_isbn = tools.rawInput("DOI / ISBN ? ").lower()
|
||||||
@ -86,11 +76,11 @@ def addFile(src, filetype):
|
|||||||
isbn = tools.rawInput('ISBN ? ')
|
isbn = tools.rawInput('ISBN ? ')
|
||||||
elif filetype == 'article':
|
elif filetype == 'article':
|
||||||
tools.warning("Could not determine the DOI for "+src +
|
tools.warning("Could not determine the DOI for "+src +
|
||||||
", switching to manual entry.")
|
", switching to manual entry.")
|
||||||
doi = tools.rawInput('DOI ? ')
|
doi = tools.rawInput('DOI ? ')
|
||||||
elif filetype == 'book':
|
elif filetype == 'book':
|
||||||
tools.warning("Could not determine the ISBN for "+src +
|
tools.warning("Could not determine the ISBN for "+src +
|
||||||
", switching to manual entry.")
|
", switching to manual entry.")
|
||||||
isbn = tools.rawInput('ISBN ? ')
|
isbn = tools.rawInput('ISBN ? ')
|
||||||
elif doi is not False:
|
elif doi is not False:
|
||||||
print("DOI for "+src+" is "+doi+".")
|
print("DOI for "+src+" is "+doi+".")
|
||||||
@ -99,10 +89,10 @@ def addFile(src, filetype):
|
|||||||
|
|
||||||
if doi is not False and doi != '':
|
if doi is not False and doi != '':
|
||||||
# Add extra \n for bibtexparser
|
# Add extra \n for bibtexparser
|
||||||
bibtex = doi2Bib(doi).strip().replace(',', ",\n")+"\n"
|
bibtex = fetcher.doi2Bib(doi).strip().replace(',', ",\n")+"\n"
|
||||||
elif isbn is not False and isbn != '':
|
elif isbn is not False and isbn != '':
|
||||||
# Idem
|
# Idem
|
||||||
bibtex = isbn2Bib(isbn).strip()+"\n"
|
bibtex = fetcher.isbn2Bib(isbn).strip()+"\n"
|
||||||
else:
|
else:
|
||||||
bibtex = ''
|
bibtex = ''
|
||||||
|
|
||||||
@ -152,54 +142,10 @@ def addFile(src, filetype):
|
|||||||
if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article':
|
if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article':
|
||||||
tearpages.tearpage(new_name)
|
tearpages.tearpage(new_name)
|
||||||
|
|
||||||
bibtexAppend(bibtex)
|
backend.bibtexAppend(bibtex)
|
||||||
return new_name
|
return new_name
|
||||||
|
|
||||||
|
|
||||||
def deleteId(ident):
|
|
||||||
"""
|
|
||||||
Delete a file based on its id in the bibtex file
|
|
||||||
"""
|
|
||||||
with open(params.folder+'index.bib', 'r') as fh:
|
|
||||||
bibtex = BibTexParser(fh.read(), customization=homogeneize_latex_encoding)
|
|
||||||
bibtex = bibtex.get_entry_dict()
|
|
||||||
|
|
||||||
if ident not in bibtex.keys():
|
|
||||||
return False
|
|
||||||
|
|
||||||
try:
|
|
||||||
os.remove(bibtex[ident]['file'])
|
|
||||||
except:
|
|
||||||
tools.warning("Unable to delete file associated to id "+ident+" : " +
|
|
||||||
bibtex[ident]['file'])
|
|
||||||
del(bibtex[ident])
|
|
||||||
bibtexRewrite(bibtex)
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def deleteFile(filename):
|
|
||||||
"""
|
|
||||||
Delete a file based on its filename
|
|
||||||
"""
|
|
||||||
with open(params.folder+'index.bib', 'r') as fh:
|
|
||||||
bibtex = BibTexParser(fh.read(), customization=homogeneize_latex_encoding)
|
|
||||||
bibtex = bibtex.get_entry_dict()
|
|
||||||
|
|
||||||
found = False
|
|
||||||
for key in bibtex.keys():
|
|
||||||
if bibtex[key]['file'] == filename:
|
|
||||||
found = True
|
|
||||||
try:
|
|
||||||
os.remove(bibtex[key]['file'])
|
|
||||||
except:
|
|
||||||
tools.warning("Unable to delete file associated to id "+key+" : " +
|
|
||||||
bibtex[key]['file'])
|
|
||||||
del(bibtex[key])
|
|
||||||
if found:
|
|
||||||
bibtexRewrite(bibtex)
|
|
||||||
return found
|
|
||||||
|
|
||||||
|
|
||||||
def downloadFile(url, filetype):
|
def downloadFile(url, filetype):
|
||||||
dl, contenttype = fetcher.download(url)
|
dl, contenttype = fetcher.download(url)
|
||||||
|
|
||||||
@ -253,12 +199,12 @@ if __name__ == '__main__':
|
|||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
sys.exit("Usage : " + sys.argv[0] + " delete FILE|ID")
|
sys.exit("Usage : " + sys.argv[0] + " delete FILE|ID")
|
||||||
|
|
||||||
confirm = tools.rawInput("Are you sure you want to delete "+sys.argv[2] +
|
confirm = tools.rawInput("Are you sure you want to delete " +
|
||||||
" ? [y/N] ")
|
sys.argv[2]+" ? [y/N] ")
|
||||||
|
|
||||||
if confirm.lower() == 'y':
|
if confirm.lower() == 'y':
|
||||||
if not deleteId(sys.argv[2]):
|
if not backend.deleteId(sys.argv[2]):
|
||||||
if not deleteFile(sys.argv[2]):
|
if not backend.deleteFile(sys.argv[2]):
|
||||||
tools.warning("Unable to delete "+sys.argv[2])
|
tools.warning("Unable to delete "+sys.argv[2])
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user