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

7
bmc.py
View File

@ -465,6 +465,8 @@ if __name__ == '__main__':
help="Confirm all") help="Confirm all")
parser_download.add_argument('--tag', default='', parser_download.add_argument('--tag', default='',
help="Tag", type=commandline_arg) help="Tag", type=commandline_arg)
parser_download.add_argument('--keep', default=False,
help="Do not remove the file")
parser_download.add_argument('url', nargs='+', parser_download.add_argument('url', nargs='+',
help="url of the file to import", help="url of the file to import",
type=commandline_arg) type=commandline_arg)
@ -603,8 +605,9 @@ if __name__ == '__main__':
confirm = 'y' confirm = 'y'
if confirm.lower() == 'y': if confirm.lower() == 'y':
if args.file or not backend.deleteId(filename): if args.file or not backend.deleteId(filename, args.keep):
if args.id or not backend.deleteFile(filename): if(args.id or
not backend.deleteFile(filename, args.keep)):
tools.warning("Unable to delete "+filename) tools.warning("Unable to delete "+filename)
sys.exit(1) sys.exit(1)

View File

@ -131,7 +131,7 @@ def bibtexRewrite(data):
return False return False
def deleteId(ident): def deleteId(ident, keep=False):
"""Delete a file based on its id in the bibtex file""" """Delete a file based on its id in the bibtex file"""
try: try:
with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \ with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \
@ -145,11 +145,12 @@ def deleteId(ident):
if ident not in bibtex.keys(): if ident not in bibtex.keys():
return False return False
if not keep:
try: try:
os.remove(bibtex[ident]['file']) os.remove(bibtex[ident]['file'])
except (KeyError, OSError): except (KeyError, OSError):
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: try:
if not os.listdir(os.path.dirname(bibtex[ident]['file'])): if not os.listdir(os.path.dirname(bibtex[ident]['file'])):
@ -167,7 +168,7 @@ def deleteId(ident):
return True return True
def deleteFile(filename): def deleteFile(filename, keep=False):
"""Delete a file based on its filename""" """Delete a file based on its filename"""
try: try:
with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \ with open(config.get("folder")+'index.bib', 'r', encoding='utf-8') \
@ -183,11 +184,12 @@ def deleteFile(filename):
try: try:
if os.path.samefile(bibtex[key]['file'], filename): if os.path.samefile(bibtex[key]['file'], filename):
found = True found = True
if not keep:
try: try:
os.remove(bibtex[key]['file']) os.remove(bibtex[key]['file'])
except (KeyError, OSError): except (KeyError, OSError):
tools.warning("Unable to delete file associated to id " + tools.warning("Unable to delete file associated " +
key+" : "+bibtex[key]['file']) "to id " + key+" : "+bibtex[key]['file'])
try: try:
if not os.listdir(os.path.dirname(filename)): if not os.listdir(os.path.dirname(filename)):