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 # Handle the "--remove" Family Tree
if self.removes: if self.removes:
for name in self.removes: for name in self.removes:
self.dbman.remove_database(name) self.dbman.remove_database(name, self.user)
if should_exit: if should_exit:
sys.exit(0) sys.exit(0)
else: else:

View File

@ -62,7 +62,7 @@ Application options
-C, --create=FAMILY_TREE Create on open if new Family Tree -C, --create=FAMILY_TREE Create on open if new Family Tree
-i, --import=FILENAME Import file -i, --import=FILENAME Import file
-e, --export=FILENAME Export 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 -f, --format=FORMAT Specify Family Tree format
-a, --action=ACTION Specify action -a, --action=ACTION Specify action
-p, --options=OPTIONS_STRING Specify options -p, --options=OPTIONS_STRING Specify options
@ -138,7 +138,7 @@ class ArgParser(object):
-C, --create=FAMILY_TREE Create on open if new Family Tree -C, --create=FAMILY_TREE Create on open if new Family Tree
-i, --import=FILENAME Import file -i, --import=FILENAME Import file
-e, --export=FILENAME Export 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 -f, --format=FORMAT Specify Family Tree format
-a, --action=ACTION Specify action -a, --action=ACTION Specify action
-p, --options=OPTIONS_STRING Specify options -p, --options=OPTIONS_STRING Specify options

View File

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