Added function to open file by id + try / except when writing index file
This commit is contained in:
parent
787113db66
commit
344029ca27
@ -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.
|
||||
|
||||
20. No DOI for HAL
|
||||
30. Parameter to disable remote search
|
||||
40. Open file
|
||||
45. Doc / Man
|
||||
50. Webserver interface
|
||||
60. Categories
|
||||
70. Edit an entry instead of deleting it and adding it again
|
||||
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 ?
|
||||
|
||||
|
65
backend.py
65
backend.py
@ -53,17 +53,25 @@ def bibtexAppend(data):
|
||||
|
||||
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")
|
||||
try:
|
||||
with open(params.folder+'index.bib', 'a') as fh:
|
||||
fh.write(parsed2Bibtex(data)+"\n")
|
||||
except:
|
||||
tools.warning("Unable to open index file.")
|
||||
return False
|
||||
|
||||
|
||||
def bibtexEdit(ident, modifs):
|
||||
"""Update ident key in bibtex file, modifications are in modifs dict"""
|
||||
|
||||
with open(params.folder+'index.bib', 'r') as fh:
|
||||
bibtex = BibTexParser(fh.read(),
|
||||
customization=homogeneize_latex_encoding)
|
||||
bibtex = bibtex.get_entry_dict()
|
||||
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
|
||||
|
||||
for key in modifs.keys():
|
||||
bibtex[ident][key] = modifs[key]
|
||||
@ -78,16 +86,24 @@ def bibtexRewrite(data):
|
||||
bibtex = ''
|
||||
for entry in data.keys():
|
||||
bibtex += parsed2Bibtex(data[entry])+"\n"
|
||||
with open(params.folder+'index.bib', 'w') as fh:
|
||||
fh.write(bibtex)
|
||||
try:
|
||||
with open(params.folder+'index.bib', 'w') as fh:
|
||||
fh.write(bibtex)
|
||||
except:
|
||||
tools.warning("Unable to open index file.")
|
||||
return False
|
||||
|
||||
|
||||
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()
|
||||
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
|
||||
@ -108,10 +124,14 @@ def deleteId(ident):
|
||||
|
||||
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()
|
||||
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
|
||||
|
||||
found = False
|
||||
for key in bibtex.keys():
|
||||
@ -141,11 +161,14 @@ def diffFilesIndex():
|
||||
"""
|
||||
|
||||
files = tools.listDir(params.folder)
|
||||
with open(params.folder+'index.bib', 'r') as fh:
|
||||
index = BibTexParser(fh.read(),
|
||||
customization=homogeneize_latex_encoding)
|
||||
|
||||
index_diff = index.get_entry_dict()
|
||||
try:
|
||||
with open(params.folder+'index.bib', 'r') as fh:
|
||||
index = BibTexParser(fh.read(),
|
||||
customization=homogeneize_latex_encoding)
|
||||
index_diff = index.get_entry_dict()
|
||||
except:
|
||||
tools.warning("Unable to open index file.")
|
||||
return False
|
||||
|
||||
for key in index_diff.keys():
|
||||
if index_diff[key]['file'] not in files:
|
||||
|
25
main.py
25
main.py
@ -155,6 +155,23 @@ def downloadFile(url, filetype):
|
||||
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():
|
||||
diff = backend.diffFilesIndex()
|
||||
|
||||
@ -284,6 +301,14 @@ if __name__ == '__main__':
|
||||
elif sys.argv[1] == 'search':
|
||||
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':
|
||||
if len(sys.argv) > 2 and sys.argv[2] == 'help':
|
||||
sys.exit("Usage: " + sys.argv[0] + " resync")
|
||||
|
Loading…
Reference in New Issue
Block a user