New CLI commands; 'safe' mode and 'default'

This commit is contained in:
prculley 2018-11-10 11:13:57 -06:00 committed by Nick Hall
parent f4c94c48b4
commit 051311bbbd
3 changed files with 83 additions and 9 deletions

View File

@ -39,15 +39,16 @@ import sys
import os import os
import getopt import getopt
import logging import logging
import shutil
from glob import glob
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# gramps modules # gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.const import LONGOPTS, SHORTOPTS, PLUGINS_DIR, USER_PLUGINS from gramps.gen.const import (LONGOPTS, SHORTOPTS, USER_PLUGINS, VERSION_DIR,
from gramps.gen.plug import BasePluginManager HOME_DIR, TEMP_DIR, THUMB_DIR, ENV_DIR, USER_CSS)
from gramps.gen.config import config
from gramps.gen.utils.cast import get_type_converter from gramps.gen.utils.cast import get_type_converter
from gramps.gen.const import GRAMPS_LOCALE as glocale from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext _ = glocale.translation.gettext
@ -81,6 +82,14 @@ Application options
-y, --yes Don't ask to confirm dangerous actions (non-GUI mode only) -y, --yes Don't ask to confirm dangerous actions (non-GUI mode only)
-q, --quiet Suppress progress indication output (non-GUI mode only) -q, --quiet Suppress progress indication output (non-GUI mode only)
-v, --version Show versions -v, --version Show versions
-S, --safe Start Gramps in 'Safe mode'
(temporarily use default settings)
-D, --default=[APXFE] Reset settings to default;
A - addons are cleared
P - Preferences to default
X - Books are cleared, reports and tool settings to default
F - filters are cleared
E - Everything is set to default or cleared
""") """)
_USAGE = _(""" _USAGE = _("""
@ -342,6 +351,7 @@ class ArgParser:
elif option in ['-t']: elif option in ['-t']:
self.list_table = True self.list_table = True
elif option in ['-s', '--show']: elif option in ['-s', '--show']:
from gramps.gen.config import config
print(_("Gramps config settings from %s:" print(_("Gramps config settings from %s:"
) % config.filename) ) % config.filename)
for sect in config.data: for sect in config.data:
@ -351,6 +361,7 @@ class ArgParser:
print() print()
sys.exit(0) sys.exit(0)
elif option in ['-c', '--config']: elif option in ['-c', '--config']:
from gramps.gen.config import config
cfg_name = value cfg_name = value
set_value = False set_value = False
if cfg_name: if cfg_name:
@ -396,6 +407,44 @@ class ArgParser:
self.auto_accept = True self.auto_accept = True
elif option in ['-q', '--quiet']: elif option in ['-q', '--quiet']:
self.quiet = True self.quiet = True
elif option in ['-S', '--safe']:
cleandbg += [opt_ix]
elif option in ['-D', '--default']:
def rmtree(path):
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)
if 'E' in value or 'A' in value: # clear addons
rmtree(USER_PLUGINS)
if 'E' in value or 'P' in value: # clear ini preferences
for fil in glob(os.path.join(VERSION_DIR, "*.*")):
if "custom_filters.xml" in fil:
continue
os.remove(fil)
# create gramps.ini so config won't load the one from an
# older version of Gramps.
with open(os.path.join(VERSION_DIR, 'gramps.ini'), 'w'):
pass
if 'E' in value or 'F' in value: # clear filters
fil = os.path.join(VERSION_DIR, "custom_filters.xml")
if os.path.isfile(fil):
os.remove(fil)
if 'E' in value or 'X' in value: # clear xml reports/tools
for fil in glob(os.path.join(HOME_DIR, "*.xml")):
os.remove(fil)
if 'E' in value or 'Z' in value: # clear upgrade zips
for fil in glob(os.path.join(HOME_DIR, "*.zip")):
os.remove(fil)
if 'E' in value: # Everything else
rmtree(TEMP_DIR)
rmtree(THUMB_DIR)
rmtree(USER_CSS)
rmtree(ENV_DIR)
rmtree(os.path.join(HOME_DIR, "maps"))
for fil in glob(os.path.join(HOME_DIR, "*")):
if os.path.isfile(fil):
os.remove(fil)
sys.exit(0) # Done with Default
#clean options list #clean options list
cleandbg.reverse() cleandbg.reverse()

View File

@ -104,6 +104,13 @@ elif 'USERPROFILE' in os.environ:
else: else:
USER_HOME = get_env_var('HOME') USER_HOME = get_env_var('HOME')
HOME_DIR = os.path.join(USER_HOME, '.gramps') HOME_DIR = os.path.join(USER_HOME, '.gramps')
ORIG_HOME_DIR = HOME_DIR
if 'SAFEMODE' in os.environ:
if 'USERPROFILE' in os.environ:
USER_HOME = get_env_var('USERPROFILE')
else:
USER_HOME = get_env_var('HOME')
HOME_DIR = get_env_var('SAFEMODE')
VERSION_DIR = os.path.join( VERSION_DIR = os.path.join(
@ -267,6 +274,7 @@ LONGOPTS = [
"class=", "class=",
"config=", "config=",
"debug=", "debug=",
"default=",
"display=", "display=",
"disable-sound", "disable-sound",
"disable-crash-dialog", "disable-crash-dialog",
@ -294,6 +302,7 @@ LONGOPTS = [
"password=", "password=",
"create=", "create=",
"options=", "options=",
"safe",
"screen=", "screen=",
"show", "show",
"sm-client-id=", "sm-client-id=",
@ -307,7 +316,7 @@ LONGOPTS = [
"quiet", "quiet",
] ]
SHORTOPTS = "O:U:P:C:i:e:f:a:p:d:c:r:lLthuv?syq" SHORTOPTS = "O:U:P:C:i:e:f:a:p:d:c:r:lLthuv?syqSD:"
GRAMPS_UUID = uuid.UUID('516cd010-5a41-470f-99f8-eb22f1098ad6') GRAMPS_UUID = uuid.UUID('516cd010-5a41-470f-99f8-eb22f1098ad6')

View File

@ -37,16 +37,23 @@ LOG = logging.getLogger(".")
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
#-------------------------------------------------------------------------
# process 'safe mode'; set up for a temp directory for user data
# actual directory paths set up in const module
if "-S" in sys.argv or "--safe" in sys.argv:
from tempfile import TemporaryDirectory
tempdir = TemporaryDirectory(prefix='gramps_')
os.environ['SAFEMODE'] = tempdir.name
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Gramps modules # Gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from .gen.const import APP_GRAMPS, USER_DIRLIST, HOME_DIR from .gen.const import APP_GRAMPS, USER_DIRLIST, HOME_DIR, ORIG_HOME_DIR
from .gen.constfunc import mac from .gen.constfunc import mac
from .version import VERSION_TUPLE from .version import VERSION_TUPLE
from .gen.constfunc import win, get_env_var from .gen.constfunc import win, get_env_var
from .gen.config import config
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -436,6 +443,18 @@ def run():
argv_copy = sys.argv[:] argv_copy = sys.argv[:]
argpars = ArgParser(argv_copy) argpars = ArgParser(argv_copy)
# if in safe mode we should point the db dir back to the original dir.
# It is ok to import config here, 'Defaults' command had its chance...
from .gen.config import config
if 'SAFEMODE' in os.environ:
config.set('database.path', os.path.join(ORIG_HOME_DIR, 'grampsdb'))
# On windows the fontconfig handler may be a better choice; ask user to
# choose for now.
if(win() and ('PANGOCAIRO_BACKEND' not in os.environ) and
config.get('preferences.alternate-fonthandler')):
os.environ['PANGOCAIRO_BACKEND'] = "fontconfig"
# Calls to LOG must be after setup_logging() and ArgParser() # Calls to LOG must be after setup_logging() and ArgParser()
LOG = logging.getLogger(".locale") LOG = logging.getLogger(".locale")
LOG.debug("Encoding: %s", glocale.encoding) LOG.debug("Encoding: %s", glocale.encoding)
@ -477,9 +496,6 @@ def main():
resource_path, filename = os.path.split(os.path.abspath(__file__)) resource_path, filename = os.path.split(os.path.abspath(__file__))
resource_path, dirname = os.path.split(resource_path) resource_path, dirname = os.path.split(resource_path)
os.environ['GRAMPS_RESOURCES'] = resource_path os.environ['GRAMPS_RESOURCES'] = resource_path
if win() and ('PANGOCAIRO_BACKEND' not in os.environ) and \
config.get('preferences.alternate-fonthandler'):
os.environ['PANGOCAIRO_BACKEND'] = "fontconfig"
errors = run() errors = run()
if errors and isinstance(errors, list): if errors and isinstance(errors, list):
for error in errors: for error in errors: