From 051311bbbda1611c1a109a5e92efbc633a7338e3 Mon Sep 17 00:00:00 2001 From: prculley Date: Sat, 10 Nov 2018 11:13:57 -0600 Subject: [PATCH] New CLI commands; 'safe' mode and 'default' --- gramps/cli/argparser.py | 55 ++++++++++++++++++++++++++++++++++++++--- gramps/gen/const.py | 11 ++++++++- gramps/grampsapp.py | 26 +++++++++++++++---- 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/gramps/cli/argparser.py b/gramps/cli/argparser.py index 82d398b05..6eac27ac0 100644 --- a/gramps/cli/argparser.py +++ b/gramps/cli/argparser.py @@ -39,15 +39,16 @@ import sys import os import getopt import logging +import shutil +from glob import glob #------------------------------------------------------------------------- # # gramps modules # #------------------------------------------------------------------------- -from gramps.gen.const import LONGOPTS, SHORTOPTS, PLUGINS_DIR, USER_PLUGINS -from gramps.gen.plug import BasePluginManager -from gramps.gen.config import config +from gramps.gen.const import (LONGOPTS, SHORTOPTS, USER_PLUGINS, VERSION_DIR, + HOME_DIR, TEMP_DIR, THUMB_DIR, ENV_DIR, USER_CSS) from gramps.gen.utils.cast import get_type_converter from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext @@ -81,6 +82,14 @@ Application options -y, --yes Don't ask to confirm dangerous actions (non-GUI mode only) -q, --quiet Suppress progress indication output (non-GUI mode only) -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 = _(""" @@ -342,6 +351,7 @@ class ArgParser: elif option in ['-t']: self.list_table = True elif option in ['-s', '--show']: + from gramps.gen.config import config print(_("Gramps config settings from %s:" ) % config.filename) for sect in config.data: @@ -351,6 +361,7 @@ class ArgParser: print() sys.exit(0) elif option in ['-c', '--config']: + from gramps.gen.config import config cfg_name = value set_value = False if cfg_name: @@ -396,6 +407,44 @@ class ArgParser: self.auto_accept = True elif option in ['-q', '--quiet']: 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 cleandbg.reverse() diff --git a/gramps/gen/const.py b/gramps/gen/const.py index 656012ba0..5a11da35e 100644 --- a/gramps/gen/const.py +++ b/gramps/gen/const.py @@ -104,6 +104,13 @@ elif 'USERPROFILE' in os.environ: else: USER_HOME = get_env_var('HOME') 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( @@ -267,6 +274,7 @@ LONGOPTS = [ "class=", "config=", "debug=", + "default=", "display=", "disable-sound", "disable-crash-dialog", @@ -294,6 +302,7 @@ LONGOPTS = [ "password=", "create=", "options=", + "safe", "screen=", "show", "sm-client-id=", @@ -307,7 +316,7 @@ LONGOPTS = [ "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') diff --git a/gramps/grampsapp.py b/gramps/grampsapp.py index 97f737275..6548893c0 100644 --- a/gramps/grampsapp.py +++ b/gramps/grampsapp.py @@ -37,16 +37,23 @@ LOG = logging.getLogger(".") 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 # #------------------------------------------------------------------------- -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 .version import VERSION_TUPLE from .gen.constfunc import win, get_env_var -from .gen.config import config #------------------------------------------------------------------------- # @@ -436,6 +443,18 @@ def run(): argv_copy = sys.argv[:] 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() LOG = logging.getLogger(".locale") LOG.debug("Encoding: %s", glocale.encoding) @@ -477,9 +496,6 @@ def main(): resource_path, filename = os.path.split(os.path.abspath(__file__)) resource_path, dirname = os.path.split(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() if errors and isinstance(errors, list): for error in errors: