Add --keep argument for delete action, see issue #26

This commit is contained in:
Phyks 2015-06-09 18:09:35 +02:00
parent 3232fc68be
commit b655c50f07
2 changed files with 21 additions and 16 deletions

11
bmc.py
View File

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

View File

@ -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)):