Tag handling + various bugfixes

This commit is contained in:
Phyks 2014-05-05 00:19:29 +02:00
parent bf1eca4c42
commit e17c97bc4a
3 changed files with 95 additions and 23 deletions

View File

@ -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 ?

View File

@ -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
@ -189,13 +219,13 @@ def getBibtex(entry, file_id = 'both'):
""" """
try: 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: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")
return False return False
bibtex_entry = False bibtex_entry = False
if file_id == 'both' or file_id == 'id': if file_id == 'both' or file_id == 'id':
try: try:
@ -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

67
main.py
View File

@ -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,8 +157,40 @@ 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:
index = BibTexParser(fh.read(), index = BibTexParser(fh.read(),
@ -164,8 +199,8 @@ def editEntry(entry, file_id = 'both'):
except: except:
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__':
@ -309,22 +352,22 @@ if __name__ == '__main__':
help="a filename or an identifier") help="a filename or an identifier")
group = parser_delete.add_mutually_exclusive_group() group = parser_delete.add_mutually_exclusive_group()
group.add_argument('--id', action="store_true", default=False, group.add_argument('--id', action="store_true", default=False,
help="id based deletion") help="id based deletion")
group.add_argument('--file', action="store_true", default=False, group.add_argument('--file', action="store_true", default=False,
help="file based deletion") help="file based deletion")
parser_delete.add_argument('-f', '--force', default=False, parser_delete.add_argument('-f', '--force', default=False,
action='store_true', action='store_true',
help="delete without confirmation") help="delete without confirmation")
parser_delete.set_defaults(func='delete') parser_delete.set_defaults(func='delete')
parser_edit = subparsers.add_parser('edit', help="edit help") parser_edit = subparsers.add_parser('edit', help="edit help")
parser_edit.add_argument('entries', metavar='entry', nargs='+', parser_edit.add_argument('entries', metavar='entry', nargs='+',
help="a filename or an identifier") help="a filename or an identifier")
group = parser_edit.add_mutually_exclusive_group() group = parser_edit.add_mutually_exclusive_group()
group.add_argument('--id', action="store_true", default=False, group.add_argument('--id', action="store_true", default=False,
help="id based deletion") help="id based deletion")
group.add_argument('--file', action="store_true", default=False, group.add_argument('--file', action="store_true", default=False,
help="file based deletion") help="file based deletion")
parser_edit.set_defaults(func='edit') parser_edit.set_defaults(func='edit')
parser_list = subparsers.add_parser('list', help="list help") parser_list = subparsers.add_parser('list', help="list help")