From bd91fa2e498a46b90cd366228a55b5c55be7b4f4 Mon Sep 17 00:00:00 2001 From: Phyks Date: Sun, 4 May 2014 01:50:41 +0200 Subject: [PATCH] Added an option to edit entries --- README.md | 7 +++---- backend.py | 30 ++++++++++++++++++++++++++- main.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 0948000..5224524 100644 --- a/README.md +++ b/README.md @@ -107,12 +107,11 @@ 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. +40. Categories 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 :/ +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 :/ ## Issues ? diff --git a/backend.py b/backend.py index 60b2147..f339bc8 100644 --- a/backend.py +++ b/backend.py @@ -159,7 +159,6 @@ def diffFilesIndex(): * full bibtex entry with file='' if file is not found * only file entry if file with missing bibtex entry """ - files = tools.listDir(params.folder) try: with open(params.folder+'index.bib', 'r') as fh: @@ -180,3 +179,32 @@ def diffFilesIndex(): index_diff[filename] = {'file': filename} return index + + +def getBibtex(entry, file_id = 'both'): + """Returns the bibtex entry corresponding to entry, as a dict + + entry is either a filename or a bibtex ident + file_id is file or id or both to search for a file / id / both + """ + 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 + + bibtex_entry = False + if file_id == 'both' or file_id == 'id': + try: + bibtex_entry = bibtex[entry] + except KeyError: + pass + elif file_id == 'both' or file_id == 'file': + for key in bibtex.keys(): + if bibtex[key]['file'] == filename: + bibtex_entry = bibtex[key] + break + return bibtex_entry diff --git a/main.py b/main.py index c5fd860..8472041 100755 --- a/main.py +++ b/main.py @@ -145,6 +145,31 @@ def addFile(src, filetype, manual): return new_name +def editEntry(entry, file_id = 'both'): + bibtex = backend.getBibtex(entry, file_id) + if bibtex is False: + tools.warning("Entry "+entry+" does not exist.") + + if file_id == 'file': + filename = entry + else: + filename = bibtex['file'] + bibtex = checkBibtex(filename, bibtex) + + try: + with open(params.folder+'index.bib', 'r') as fh: + index = BibTexParser(fh.read(), + customization=homogeneize_latex_encoding) + index = index.get_entry_dict() + except: + tools.warning("Unable to open index file.") + return False + + index[bibtex['id']] = bibtex + backend.bibtexRewrite(index) + return True + + def downloadFile(url, filetype, manual): dl, contenttype = fetcher.download(url) @@ -280,12 +305,27 @@ if __name__ == '__main__': parser_import.set_defaults(func='import') parser_delete = subparsers.add_parser('delete', help="delete help") - parser_delete.add_argument('files', metavar='entry', nargs='+', + parser_delete.add_argument('entries', metavar='entry', nargs='+', help="a filename or an identifier") + group = parser_delete.add_mutually_exclusive_group() + group.add_argument('--id', action="store_true", default=False, + help="id based deletion") + group.add_argument('--file', action="store_true", default=False, + help="file based deletion") parser_delete.add_argument('-f', '--force', default=False, action='store_true', help="delete without confirmation") parser_delete.set_defaults(func='delete') + + parser_edit = subparsers.add_parser('edit', help="edit help") + parser_edit.add_argument('entries', metavar='entry', nargs='+', + help="a filename or an identifier") + group = parser_edit.add_mutually_exclusive_group() + group.add_argument('--id', action="store_true", default=False, + help="id based deletion") + group.add_argument('--file', action="store_true", default=False, + help="file based deletion") + parser_edit.set_defaults(func='edit') parser_list = subparsers.add_parser('list', help="list help") parser_list.set_defaults(func='list') @@ -324,7 +364,7 @@ if __name__ == '__main__': sys.exit() elif args.func == 'delete': - for filename in args.file: + for filename in args.entries: if not args.force: confirm = tools.rawInput("Are you sure you want to " + "delete "+filename+" ? [y/N] ") @@ -332,14 +372,25 @@ if __name__ == '__main__': confirm = 'y' if confirm.lower() == 'y': - if not backend.deleteId(filename): - if not backend.deleteFile(filename): + if args.file or not backend.deleteId(filename): + if args.id or not backend.deleteFile(filename): tools.warning("Unable to delete "+filename) sys.exit(1) print(filename+" successfully deleted.") sys.exit() + elif args.fund == 'edit': + for filename in args.entries: + if args.file: + file_id = 'file' + elif args.id: + file_id = 'id' + else: + file_id = 'both' + editEntry(filename, file_id) + sys.exit() + elif args.func == 'list': raise Exception('TODO') @@ -351,11 +402,13 @@ if __name__ == '__main__': if not openFile(filename): sys.exit("Unable to open file associated " + "to ident "+filename) + sys.exit() elif args.func == 'resync': confirm = tools.rawInput("Resync files and bibtex index? [y/N] ") if confirm.lower() == 'y': resync() + sys.exit() except KeyboardInterrupt: sys.exit()