Added function to open file by id + try / except when writing index file

This commit is contained in:
Phyks 2014-05-02 12:46:37 +02:00
parent 787113db66
commit 344029ca27
3 changed files with 70 additions and 23 deletions

View File

@ -107,14 +107,13 @@ 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.
20. No DOI for HAL
30. Parameter to disable remote search 30. Parameter to disable remote search
40. Open file
45. Doc / Man 45. Doc / Man
50. Webserver interface 50. Webserver interface
60. Categories 60. Categories
70. Edit an entry instead of deleting it and adding it again 70. Edit an entry instead of deleting it and adding it again
80. Automatic download of new arXiv version 80. Automatic download of new arXiv version
90. No DOI for HAL => thanks all for metadata with SOAP API… don't want to handle it for now :/
## Issues ? ## Issues ?

View File

@ -53,17 +53,25 @@ def bibtexAppend(data):
data is a dict for one entry in bibtex, as the one from bibtexparser output data is a dict for one entry in bibtex, as the one from bibtexparser output
""" """
try:
with open(params.folder+'index.bib', 'a') as fh: with open(params.folder+'index.bib', 'a') as fh:
fh.write(parsed2Bibtex(data)+"\n") fh.write(parsed2Bibtex(data)+"\n")
except:
tools.warning("Unable to open index file.")
return False
def bibtexEdit(ident, modifs): def bibtexEdit(ident, modifs):
"""Update ident key in bibtex file, modifications are in modifs dict""" """Update ident key in bibtex file, modifications are in modifs dict"""
try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
except:
tools.warning("Unable to open index file.")
return False
for key in modifs.keys(): for key in modifs.keys():
bibtex[ident][key] = modifs[key] bibtex[ident][key] = modifs[key]
@ -78,16 +86,24 @@ def bibtexRewrite(data):
bibtex = '' bibtex = ''
for entry in data.keys(): for entry in data.keys():
bibtex += parsed2Bibtex(data[entry])+"\n" bibtex += parsed2Bibtex(data[entry])+"\n"
try:
with open(params.folder+'index.bib', 'w') as fh: with open(params.folder+'index.bib', 'w') as fh:
fh.write(bibtex) fh.write(bibtex)
except:
tools.warning("Unable to open index file.")
return False
def deleteId(ident): def deleteId(ident):
"""Delete a file based on its id in the bibtex file""" """Delete a file based on its id in the bibtex file"""
try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
except:
tools.warning("Unable to open index file.")
return False
if ident not in bibtex.keys(): if ident not in bibtex.keys():
return False return False
@ -108,10 +124,14 @@ def deleteId(ident):
def deleteFile(filename): def deleteFile(filename):
"""Delete a file based on its filename""" """Delete a file based on its filename"""
try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
except:
tools.warning("Unable to open index file.")
return False
found = False found = False
for key in bibtex.keys(): for key in bibtex.keys():
@ -141,11 +161,14 @@ def diffFilesIndex():
""" """
files = tools.listDir(params.folder) files = tools.listDir(params.folder)
try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r') as fh:
index = BibTexParser(fh.read(), index = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
index_diff = index.get_entry_dict() index_diff = index.get_entry_dict()
except:
tools.warning("Unable to open index file.")
return False
for key in index_diff.keys(): for key in index_diff.keys():
if index_diff[key]['file'] not in files: if index_diff[key]['file'] not in files:

25
main.py
View File

@ -155,6 +155,23 @@ def downloadFile(url, filetype):
return False return False
def openFile(ident):
try:
with open(params.folder+'index.bib', 'r') as fh:
bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()
except:
tools.warning("Unable to open index file.")
return False
if ident not in bibtex.keys():
return False
else:
subprocess.Popen(['xdg-open', bibtex[ident]['file']])
return True
def resync(): def resync():
diff = backend.diffFilesIndex() diff = backend.diffFilesIndex()
@ -284,6 +301,14 @@ if __name__ == '__main__':
elif sys.argv[1] == 'search': elif sys.argv[1] == 'search':
raise Exception('TODO') raise Exception('TODO')
elif sys.argv[1] == 'open':
if len(sys.argv) < 3:
sys.exit("Usage: " + sys.argv[0] +
" open ID")
if not openFile(sys.argv[2]):
sys.exit("Unable to open file associated " +
"to ident "+sys.argv[2])
elif sys.argv[1] == 'resync': elif sys.argv[1] == 'resync':
if len(sys.argv) > 2 and sys.argv[2] == 'help': if len(sys.argv) > 2 and sys.argv[2] == 'help':
sys.exit("Usage: " + sys.argv[0] + " resync") sys.exit("Usage: " + sys.argv[0] + " resync")