CLI -r/--remove asks firsts, uses reg exp

This commit is contained in:
Doug Blank 2015-12-18 17:10:11 -05:00
parent fc55aba6b3
commit 4d5c28ad3a
3 changed files with 22 additions and 16 deletions

View File

@ -407,7 +407,7 @@ class ArgHandler(object):
# Handle the "--remove" Family Tree
if self.removes:
for name in self.removes:
self.dbman.remove_database(name)
self.dbman.remove_database(name, self.user)
if should_exit:
sys.exit(0)
else:

View File

@ -62,7 +62,7 @@ Application options
-C, --create=FAMILY_TREE Create on open if new Family Tree
-i, --import=FILENAME Import file
-e, --export=FILENAME Export file
-r, --remove=FAMILY_TREE Remove a Family Tree
-r, --remove=FAMILY_TREE_PATTERN Remove matching Family Tree(s)
-f, --format=FORMAT Specify Family Tree format
-a, --action=ACTION Specify action
-p, --options=OPTIONS_STRING Specify options
@ -138,7 +138,7 @@ class ArgParser(object):
-C, --create=FAMILY_TREE Create on open if new Family Tree
-i, --import=FILENAME Import file
-e, --export=FILENAME Export file
-r, --remove=FAMILY_TREE Remove a Family Tree
-r, --remove=PATTERN Remove matching Family Tree(s)
-f, --format=FORMAT Specify Family Tree format
-a, --action=ACTION Specify action
-p, --options=OPTIONS_STRING Specify options

View File

@ -30,6 +30,7 @@ creating, and deleting of databases.
# Standard python modules
#
#-------------------------------------------------------------------------
import re
import os
import sys
import time
@ -380,9 +381,10 @@ class CLIDbManager(object):
return True
return False
def remove_database(self, dbname):
def remove_database(self, dbname, user=None):
"""
Deletes a database folder given its proper name.
Deletes a database folder given a pattenr that matches
its proper name.
"""
dbdir = os.path.expanduser(config.get('behavior.database-path'))
match_list = []
@ -393,21 +395,25 @@ class CLIDbManager(object):
file = open(path_name, 'r', encoding='utf8')
name = file.readline().strip()
file.close()
if name == dbname: # currently exact match; could add re.match
match_list.append(dirpath)
if re.match("^" + dbname + "$", name):
match_list.append((name, dirpath))
if len(match_list) == 0:
CLIDbManager.ERROR("Family tree not found",
"No matching family tree found: '%s'" % dbname)
# now delete them:
for directory in match_list:
try:
for (top, dirs, files) in os.walk(directory):
for filename in files:
os.unlink(os.path.join(top, filename))
os.rmdir(directory)
except (IOError, OSError) as msg:
CLIDbManager.ERROR(_("Could not delete Family Tree"),
str(msg))
for (name, directory) in match_list:
if user is None or user.prompt(
_('Remove family tree warning'),
_('Are you sure you want to remove the family tree named\n"%s"?' % name),
_('Yes'), _('No'), None):
try:
for (top, dirs, files) in os.walk(directory):
for filename in files:
os.unlink(os.path.join(top, filename))
os.rmdir(directory)
except (IOError, OSError) as msg:
CLIDbManager.ERROR(_("Could not delete Family Tree"),
str(msg))
def rename_database(self, filepath, new_text):
"""