Tag handling + various bugfixes
This commit is contained in:
parent
bf1eca4c42
commit
e17c97bc4a
@ -107,11 +107,10 @@ 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.
|
||||||
|
|
||||||
40. Categories
|
|
||||||
45. Doc / Man
|
|
||||||
50. Webserver interface
|
|
||||||
60. Automatic download of new arXiv version
|
60. Automatic download of new arXiv version
|
||||||
70. No DOI for HAL => thanks all for metadata with SOAP API… don't want to handle it for now :/
|
70. No DOI for HAL => metadata with SOAP API… don't want to handle it for now :/
|
||||||
|
80. Search engine
|
||||||
|
90. Webserver interface
|
||||||
|
|
||||||
## Issues ?
|
## Issues ?
|
||||||
|
|
||||||
|
38
backend.py
38
backend.py
@ -9,7 +9,7 @@ from bibtexparser.bparser import BibTexParser
|
|||||||
from bibtexparser.customization import homogeneize_latex_encoding
|
from bibtexparser.customization import homogeneize_latex_encoding
|
||||||
|
|
||||||
|
|
||||||
def getNewName(src, bibtex):
|
def getNewName(src, bibtex, tag=''):
|
||||||
"""
|
"""
|
||||||
Return the formatted name according to params for the given
|
Return the formatted name according to params for the given
|
||||||
bibtex entry
|
bibtex entry
|
||||||
@ -35,7 +35,21 @@ def getNewName(src, bibtex):
|
|||||||
new_name = new_name.replace("%a", ', '.join([i.split(',')[0].strip()
|
new_name = new_name.replace("%a", ', '.join([i.split(',')[0].strip()
|
||||||
for i in authors]))
|
for i in authors]))
|
||||||
|
|
||||||
new_name = params.folder+tools.slugify(new_name)+tools.getExtension(src)
|
if tag == '':
|
||||||
|
new_name = (params.folder + tools.slugify(new_name) +
|
||||||
|
tools.getExtension(src))
|
||||||
|
else:
|
||||||
|
if not os.path.isdir(params.folder + tag):
|
||||||
|
try:
|
||||||
|
os.mkdir(params.folder + tag)
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to create tag dir " +
|
||||||
|
params.folder+tag+".")
|
||||||
|
|
||||||
|
new_name = (params.folder + tools.slugify(tag) +
|
||||||
|
tools.slugify(new_name) + tools.getExtension(src))
|
||||||
|
|
||||||
|
return new_name
|
||||||
|
|
||||||
|
|
||||||
def parsed2Bibtex(parsed):
|
def parsed2Bibtex(parsed):
|
||||||
@ -113,6 +127,14 @@ def deleteId(ident):
|
|||||||
except:
|
except:
|
||||||
tools.warning("Unable to delete file associated to id "+ident+" : " +
|
tools.warning("Unable to delete file associated to id "+ident+" : " +
|
||||||
bibtex[ident]['file'])
|
bibtex[ident]['file'])
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not os.listdir(os.path.dirname(bibtex[ident]['file'])):
|
||||||
|
os.rmdir(os.path.dirname(bibtex[ident]['file']))
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to delete empty tag dir " +
|
||||||
|
os.path.dirname(bibtex[ident]['file']))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del(bibtex[ident])
|
del(bibtex[ident])
|
||||||
bibtexRewrite(bibtex)
|
bibtexRewrite(bibtex)
|
||||||
@ -142,6 +164,14 @@ def deleteFile(filename):
|
|||||||
except:
|
except:
|
||||||
tools.warning("Unable to delete file associated to id " +
|
tools.warning("Unable to delete file associated to id " +
|
||||||
key+" : "+bibtex[key]['file'])
|
key+" : "+bibtex[key]['file'])
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not os.listdir(os.path.dirname(filename)):
|
||||||
|
os.rmdir(os.path.dirname(filename))
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to delete empty tag dir " +
|
||||||
|
os.path.dirname(filename))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
del(bibtex[key])
|
del(bibtex[key])
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@ -181,7 +211,7 @@ def diffFilesIndex():
|
|||||||
return index
|
return index
|
||||||
|
|
||||||
|
|
||||||
def getBibtex(entry, file_id = 'both'):
|
def getBibtex(entry, file_id='both'):
|
||||||
"""Returns the bibtex entry corresponding to entry, as a dict
|
"""Returns the bibtex entry corresponding to entry, as a dict
|
||||||
|
|
||||||
entry is either a filename or a bibtex ident
|
entry is either a filename or a bibtex ident
|
||||||
@ -204,7 +234,7 @@ def getBibtex(entry, file_id = 'both'):
|
|||||||
pass
|
pass
|
||||||
elif file_id == 'both' or file_id == 'file':
|
elif file_id == 'both' or file_id == 'file':
|
||||||
for key in bibtex.keys():
|
for key in bibtex.keys():
|
||||||
if os.path.samepath(bibtex[key]['file'], filename):
|
if os.path.samepath(bibtex[key]['file'], entry):
|
||||||
bibtex_entry = bibtex[key]
|
bibtex_entry = bibtex[key]
|
||||||
break
|
break
|
||||||
return bibtex_entry
|
return bibtex_entry
|
||||||
|
51
main.py
51
main.py
@ -118,7 +118,10 @@ def addFile(src, filetype, manual):
|
|||||||
|
|
||||||
bibtex = checkBibtex(src, bibtex)
|
bibtex = checkBibtex(src, bibtex)
|
||||||
|
|
||||||
new_name = backend.getNewName(src, bibtex)
|
tag = tools.rawInput("Tag for this paper (leave empty for default) ? ")
|
||||||
|
bibtex['tag'] = tag
|
||||||
|
|
||||||
|
new_name = backend.getNewName(src, bibtex, tag)
|
||||||
|
|
||||||
while os.path.exists(new_name):
|
while os.path.exists(new_name):
|
||||||
tools.warning("file "+new_name+" already exists.")
|
tools.warning("file "+new_name+" already exists.")
|
||||||
@ -145,7 +148,7 @@ def addFile(src, filetype, manual):
|
|||||||
return new_name
|
return new_name
|
||||||
|
|
||||||
|
|
||||||
def editEntry(entry, file_id = 'both'):
|
def editEntry(entry, file_id='both'):
|
||||||
bibtex = backend.getBibtex(entry, file_id)
|
bibtex = backend.getBibtex(entry, file_id)
|
||||||
if bibtex is False:
|
if bibtex is False:
|
||||||
tools.warning("Entry "+entry+" does not exist.")
|
tools.warning("Entry "+entry+" does not exist.")
|
||||||
@ -154,7 +157,39 @@ def editEntry(entry, file_id = 'both'):
|
|||||||
filename = entry
|
filename = entry
|
||||||
else:
|
else:
|
||||||
filename = bibtex['file']
|
filename = bibtex['file']
|
||||||
bibtex = checkBibtex(filename, bibtex)
|
new_bibtex = checkBibtex(filename, bibtex)
|
||||||
|
|
||||||
|
# Tag update
|
||||||
|
if new_bibtex['tag'] != bibtex['tag']:
|
||||||
|
print("Editing tag, moving file.")
|
||||||
|
new_name = backend.getNewName(new_bibtex['file'],
|
||||||
|
new_bibtex,
|
||||||
|
new_bibtex['tag'])
|
||||||
|
|
||||||
|
while os.path.exists(new_name):
|
||||||
|
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+"]? ")
|
||||||
|
if rename == '':
|
||||||
|
new_name = default_rename
|
||||||
|
else:
|
||||||
|
new_name = rename
|
||||||
|
new_bibtex['file'] = new_name
|
||||||
|
|
||||||
|
try:
|
||||||
|
shutil.move(bibtex['file'], new_bibtex['file'])
|
||||||
|
except:
|
||||||
|
raise Exception('Unable to move file '+bibtex['file']+' to ' +
|
||||||
|
new_bibtex['file'] + ' according to tag edit.')
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not os.listdir(os.path.dirname(bibtex['file'])):
|
||||||
|
os.rmdir(os.path.dirname(bibtex['file']))
|
||||||
|
except:
|
||||||
|
tools.warning("Unable to delete empty tag dir " +
|
||||||
|
os.path.dirname(bibtex['file']))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(params.folder+'index.bib', 'r') as fh:
|
with open(params.folder+'index.bib', 'r') as fh:
|
||||||
@ -165,7 +200,7 @@ def editEntry(entry, file_id = 'both'):
|
|||||||
tools.warning("Unable to open index file.")
|
tools.warning("Unable to open index file.")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
index[bibtex['id']] = bibtex
|
index[new_bibtex['id']] = new_bibtex
|
||||||
backend.bibtexRewrite(index)
|
backend.bibtexRewrite(index)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -275,6 +310,14 @@ def resync():
|
|||||||
backend.deleteFile(entry['file'])
|
backend.deleteFile(entry['file'])
|
||||||
print(entry['file'] + " removed from disk and " +
|
print(entry['file'] + " removed from disk and " +
|
||||||
"index.")
|
"index.")
|
||||||
|
# Check for empty tag dirs
|
||||||
|
for i in os.listdir(params.folder):
|
||||||
|
if os.path.isdir(i) and not os.listdir(params.folder + i):
|
||||||
|
try:
|
||||||
|
os.rmdir(params.folder + i)
|
||||||
|
except:
|
||||||
|
tools.warning("Found empty tag dir "+params.folder + i +
|
||||||
|
" but could not delete it.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
Reference in New Issue
Block a user