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") 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)
@ -483,8 +485,8 @@ if __name__ == '__main__':
parser_import.add_argument('--tag', default='', help="Tag", parser_import.add_argument('--tag', default='', help="Tag",
type=commandline_arg) type=commandline_arg)
parser_import.add_argument('--in-place', default=False, parser_import.add_argument('--in-place', default=False,
dest="inplace", action='store_true', dest="inplace", action='store_true',
help="Leave the imported file in place",) help="Leave the imported file in place",)
parser_import.add_argument('file', nargs='+', parser_import.add_argument('file', nargs='+',
help="path to the file to import", help="path to 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
try: if not keep:
os.remove(bibtex[ident]['file']) try:
except (KeyError, OSError): os.remove(bibtex[ident]['file'])
tools.warning("Unable to delete file associated to id "+ident+" : " + except (KeyError, OSError):
bibtex[ident]['file']) tools.warning("Unable to delete file associated to id " + ident +
" : " + 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
try: if not keep:
os.remove(bibtex[key]['file']) try:
except (KeyError, OSError): os.remove(bibtex[key]['file'])
tools.warning("Unable to delete file associated to id " + except (KeyError, OSError):
key+" : "+bibtex[key]['file']) tools.warning("Unable to delete file associated " +
"to id " + key+" : "+bibtex[key]['file'])
try: try:
if not os.listdir(os.path.dirname(filename)): if not os.listdir(os.path.dirname(filename)):