From 82eb6e17ac5b9a22e7bef0d904c8e5525780f9e7 Mon Sep 17 00:00:00 2001 From: belissent Date: Thu, 20 Aug 2015 12:51:08 +0200 Subject: [PATCH] Create a GRAMPS environment ENV for variables substitutions in paths --- example/gramps/example.gramps | 2 +- gramps/gen/const.py | 42 +++++++++++++++++----------- gramps/gen/utils/file.py | 32 +++++++++++---------- gramps/gen/utils/test/file_test.py | 23 +++++++-------- gramps/plugins/importer/importxml.py | 4 +-- 5 files changed, 56 insertions(+), 47 deletions(-) diff --git a/example/gramps/example.gramps b/example/gramps/example.gramps index 23fcd15e9..ea96f78f6 100644 --- a/example/gramps/example.gramps +++ b/example/gramps/example.gramps @@ -7,7 +7,7 @@ Alex Roitman,,, - $GRAMPS_RESOURCES/example/gramps + {GRAMPS_RESOURCES}/example/gramps diff --git a/gramps/gen/const.py b/gramps/gen/const.py index fcb8d3bc1..c0c14f382 100644 --- a/gramps/gen/const.py +++ b/gramps/gen/const.py @@ -116,22 +116,6 @@ USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, TEMP_DIR, THUMB_DIR, THUMB_NORMAL, THUMB_LARGE, USER_PLUGINS) -# Update environment -# This could be of general use, for example when using os.path.expandvars -os.environ['GRAMPS_VERSION'] = VERSION -os.environ['GRAMPS_VERSION_MAJOR'] = major_version -os.environ['GRAMPS_VERSION_DIR'] = VERSION_DIR -os.environ['GRAMPS_ENV_DIR'] = ENV_DIR -os.environ['GRAMPS_TEMP_DIR'] = TEMP_DIR -os.environ['GRAMPS_THUMB_DIR'] = THUMB_DIR -os.environ['GRAMPS_THUMB_NORMAL'] = THUMB_NORMAL -os.environ['GRAMPS_THUMB_LARGE'] = THUMB_LARGE -os.environ['GRAMPS_USER_PLUGINS'] = USER_PLUGINS -# Attention: $GRAMPSHOME should NOT be set, because it redefines the HOME_DIR behavior -# This leads to bugs, especially when a test calls GRAMPS again: -# the HOME_DIR is then re-computed with a wrong $GRAMPSHOME - - #------------------------------------------------------------------------- # # Paths to python modules - assumes that the root directory is one level @@ -187,6 +171,32 @@ LOGO = os.path.join(IMAGE_DIR, "logo.png") SPLASH = os.path.join(IMAGE_DIR, "splash.jpg") LICENSE_FILE = os.path.join(_resources.doc_dir, 'COPYING') + +#------------------------------------------------------------------------- +# +# GRAMPS environment variables dictionary +# +#------------------------------------------------------------------------- +ENV = { + "USER_HOME": USER_HOME, + "HOME_DIR": HOME_DIR, + "VERSION": VERSION, + "major_version": major_version, + "VERSION_DIR": VERSION_DIR, + "ENV_DIR": ENV_DIR, + "TEMP_DIR": TEMP_DIR, + "THUMB_DIR": THUMB_DIR, + "THUMB_NORMAL": THUMB_NORMAL, + "THUMB_LARGE": THUMB_LARGE, + "USER_PLUGINS": USER_PLUGINS, + "ROOT_DIR": ROOT_DIR, + "GLADE_DIR": GLADE_DIR, + "PLUGINS_DIR": PLUGINS_DIR, + "WEB_DIR": WEB_DIR, + "DATA_DIR": DATA_DIR, + "IMAGE_DIR": IMAGE_DIR, +} + #------------------------------------------------------------------------- # # Init Localization diff --git a/gramps/gen/utils/file.py b/gramps/gen/utils/file.py index feb4b6c28..18ca99388 100644 --- a/gramps/gen/utils/file.py +++ b/gramps/gen/utils/file.py @@ -43,7 +43,7 @@ LOG = logging.getLogger(".gen.utils.file") # #------------------------------------------------------------------------- from ..constfunc import win, mac, conv_to_unicode, get_env_var -from ..const import TEMP_DIR, USER_HOME, GRAMPS_LOCALE as glocale +from ..const import TEMP_DIR, USER_HOME, ENV, GRAMPS_LOCALE as glocale #------------------------------------------------------------------------- # @@ -146,19 +146,21 @@ def relative_path(original, base): rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:] return os.path.join(*rel_list) -def expanded_vars_path(path): +def expand_path(path, normalize = True): """ Expand environment variables in a path - $GRAMPSHOME is set and restored afterwards, - because undefined $GRAMPSHOME has a special meaning (see const.py). + Uses both the environment variables and the GRAMPS environment + The expansion uses the str.format, e.g. "~/{GRAMPSHOME}/{VERSION}/filename.txt" + We make the assumption that the user will not use a path that contain variable names + (it is technically possible to use characters "{", "}" in paths) """ - grampshome_added = False - if not 'GRAMPSHOME' in os.environ: - os.environ['GRAMPSHOME'] = USER_HOME - grampshome_added = True - path = os.path.expandvars(path) - if (grampshome_added): - del os.environ['GRAMPSHOME'] + environment = dict(os.environ) + environment.update(ENV) + if not 'GRAMPSHOME' in environment: + environment['GRAMPSHOME'] = USER_HOME + path = path.format(**environment) + if normalize: + path = os.path.normcase(os.path.normpath(os.path.abspath(path))) return path def media_path(db): @@ -166,13 +168,13 @@ def media_path(db): Given a database, return the mediapath to use as basedir for media """ mpath = db.get_mediapath() - return norm_media_path(mpath, db) + return expand_media_path(mpath, db) -def norm_media_path(mpath, db): +def expand_media_path(mpath, db): """ Normalize a mediapath: - Relative mediapath are considered as relative to the database - - Expand variables ($GRAMPSHOME, $GRAMPS_RESOURCES, etc.) + - Expand variables, see expand_path - Convert to absolute path - Convert slashes and case (on Windows) """ @@ -180,7 +182,7 @@ def norm_media_path(mpath, db): if mpath is None: mpath = os.path.abspath(USER_HOME) # Expand environment variables - mpath = expanded_vars_path(mpath) + mpath = expand_path(mpath, False) # Relative mediapath are considered as relative to the database if not os.path.isabs(mpath): basepath = db.get_save_path() diff --git a/gramps/gen/utils/test/file_test.py b/gramps/gen/utils/test/file_test.py index 071938c2e..9ef040e9c 100644 --- a/gramps/gen/utils/test/file_test.py +++ b/gramps/gen/utils/test/file_test.py @@ -35,11 +35,10 @@ import unittest # Gramps modules # #------------------------------------------------------------------------- -from gramps.gen.const import TEMP_DIR, USER_HOME, USER_PLUGINS +from gramps.gen.const import TEMP_DIR, USER_HOME, USER_PLUGINS, VERSION from gramps.gen.constfunc import get_env_var from gramps.gen.utils.file import media_path, get_empty_tempdir from gramps.gen.dbstate import DbState -from gramps.version import VERSION #------------------------------------------------------------------------- # @@ -71,29 +70,27 @@ class FileTest(unittest.TestCase): self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel")))) # Test with environment variable - db.set_mediapath("/test/$GRAMPS_VERSION/test_var") + db.set_mediapath("/test/{VERSION}/test_var") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/test/" + VERSION + "/test_var")))) - db.set_mediapath("${GRAMPS_USER_PLUGINS}/test_var") + db.set_mediapath("{USER_PLUGINS}/test_var") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_PLUGINS + "/test_var")))) + db.set_mediapath("{VERSION}/test_var") + self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/" + VERSION + "/test_var")))) # Test with $GRAMPSHOME environment variable not set - grampshome = None + old_env = os.environ.copy() if 'GRAMPSHOME' in os.environ: - grampshome = os.environ['GRAMPSHOME'] del os.environ['GRAMPSHOME'] - db.set_mediapath("$GRAMPSHOME/test_var") + db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_var")))) # Test with $GRAMPSHOME environment variable set os.environ['GRAMPSHOME'] = "/this/is/a/test" - db.set_mediapath("$GRAMPSHOME/test_var") + db.set_mediapath("{GRAMPSHOME}/test_var") self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/this/is/a/test/test_var")))) - # Restore $GRAMPSHOME - if grampshome: - os.environ['GRAMPSHOME'] = grampshome - else: - del os.environ['GRAMPSHOME'] + # Restore environment + os.environ = old_env #------------------------------------------------------------------------- diff --git a/gramps/plugins/importer/importxml.py b/gramps/plugins/importer/importxml.py index 3802f0100..030099b71 100644 --- a/gramps/plugins/importer/importxml.py +++ b/gramps/plugins/importer/importxml.py @@ -62,7 +62,7 @@ from gramps.gen.errors import GrampsImportError from gramps.gen.utils.id import create_id from gramps.gen.utils.db import family_name from gramps.gen.utils.unknown import make_unknown, create_explanation_note -from gramps.gen.utils.file import create_checksum, media_path, norm_media_path +from gramps.gen.utils.file import create_checksum, media_path, expand_media_path from gramps.gen.datehandler import parser, set_date from gramps.gen.display.name import displayer as name_displayer from gramps.gen.db.dbconst import (PERSON_KEY, FAMILY_KEY, SOURCE_KEY, @@ -941,7 +941,7 @@ class GrampsParser(UpdateCallback): if self.mediapath: if not self.db.get_mediapath(): self.db.set_mediapath(self.mediapath) - elif not media_path(self.db) == norm_media_path(self.mediapath, self.db): + elif not media_path(self.db) == expand_media_path(self.mediapath, self.db): self.user.notify_error(_("Could not change media path"), _("The opened file has media path %s, which conflicts with" " the media path of the Family Tree you import into. "