From b655c50f0760455514dcd8ce4c4623fd2e2ab20e Mon Sep 17 00:00:00 2001 From: Phyks Date: Tue, 9 Jun 2015 18:09:35 +0200 Subject: [PATCH] Add --keep argument for delete action, see issue #26 --- bmc.py | 11 +++++++---- libbmc/backend.py | 26 ++++++++++++++------------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/bmc.py b/bmc.py index 3b08ea6..8e93f7a 100755 --- a/bmc.py +++ b/bmc.py @@ -465,6 +465,8 @@ if __name__ == '__main__': help="Confirm all") parser_download.add_argument('--tag', default='', help="Tag", type=commandline_arg) + parser_download.add_argument('--keep', default=False, + help="Do not remove the file") parser_download.add_argument('url', nargs='+', help="url of the file to import", type=commandline_arg) @@ -483,8 +485,8 @@ if __name__ == '__main__': parser_import.add_argument('--tag', default='', help="Tag", type=commandline_arg) parser_import.add_argument('--in-place', default=False, - dest="inplace", action='store_true', - help="Leave the imported file in place",) + dest="inplace", action='store_true', + help="Leave the imported file in place",) parser_import.add_argument('file', nargs='+', help="path to the file to import", type=commandline_arg) @@ -603,8 +605,9 @@ if __name__ == '__main__': confirm = 'y' if confirm.lower() == 'y': - if args.file or not backend.deleteId(filename): - if args.id or not backend.deleteFile(filename): + if args.file or not backend.deleteId(filename, args.keep): + if(args.id or + not backend.deleteFile(filename, args.keep)): tools.warning("Unable to delete "+filename) sys.exit(1) diff --git a/libbmc/backend.py b/libbmc/backend.py index d3d67e3..b70cca7 100644 --- a/libbmc/backend.py +++ b/libbmc/backend.py @@ -131,7 +131,7 @@ def bibtexRewrite(data): return False -def deleteId(ident): +def deleteId(ident, keep=False): """Delete a file based on its id in the bibtex file""" try: with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \ @@ -145,11 +145,12 @@ def deleteId(ident): if ident not in bibtex.keys(): return False - try: - os.remove(bibtex[ident]['file']) - except (KeyError, OSError): - tools.warning("Unable to delete file associated to id "+ident+" : " + - bibtex[ident]['file']) + if not keep: + try: + os.remove(bibtex[ident]['file']) + except (KeyError, OSError): + tools.warning("Unable to delete file associated to id " + ident + + " : " + bibtex[ident]['file']) try: if not os.listdir(os.path.dirname(bibtex[ident]['file'])): @@ -167,7 +168,7 @@ def deleteId(ident): return True -def deleteFile(filename): +def deleteFile(filename, keep=False): """Delete a file based on its filename""" try: with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \ @@ -183,11 +184,12 @@ def deleteFile(filename): try: if os.path.samefile(bibtex[key]['file'], filename): found = True - try: - os.remove(bibtex[key]['file']) - except (KeyError, OSError): - tools.warning("Unable to delete file associated to id " + - key+" : "+bibtex[key]['file']) + if not keep: + try: + os.remove(bibtex[key]['file']) + except (KeyError, OSError): + tools.warning("Unable to delete file associated " + + "to id " + key+" : "+bibtex[key]['file']) try: if not os.listdir(os.path.dirname(filename)):