Create a GRAMPS environment ENV for variables substitutions in paths

This commit is contained in:
belissent 2015-08-20 12:51:08 +02:00
parent 4bbae0e46a
commit 82eb6e17ac
5 changed files with 56 additions and 47 deletions

View File

@ -7,7 +7,7 @@
<researcher> <researcher>
<resname>Alex Roitman,,,</resname> <resname>Alex Roitman,,,</resname>
</researcher> </researcher>
<mediapath>$GRAMPS_RESOURCES/example/gramps</mediapath> <mediapath>{GRAMPS_RESOURCES}/example/gramps</mediapath>
</header> </header>
<name-formats> <name-formats>
<format number="-1" name="SURNAME, Given (Common)" fmt_str="SURNAME, given (common)" active="1"/> <format number="-1" name="SURNAME, Given (Common)" fmt_str="SURNAME, given (common)" active="1"/>

View File

@ -116,22 +116,6 @@ USER_DIRLIST = (USER_HOME, HOME_DIR, VERSION_DIR, ENV_DIR, TEMP_DIR, THUMB_DIR,
THUMB_NORMAL, THUMB_LARGE, USER_PLUGINS) 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 # 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") SPLASH = os.path.join(IMAGE_DIR, "splash.jpg")
LICENSE_FILE = os.path.join(_resources.doc_dir, 'COPYING') 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 # Init Localization

View File

@ -43,7 +43,7 @@ LOG = logging.getLogger(".gen.utils.file")
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ..constfunc import win, mac, conv_to_unicode, get_env_var 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:] rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:]
return os.path.join(*rel_list) return os.path.join(*rel_list)
def expanded_vars_path(path): def expand_path(path, normalize = True):
""" """
Expand environment variables in a path Expand environment variables in a path
$GRAMPSHOME is set and restored afterwards, Uses both the environment variables and the GRAMPS environment
because undefined $GRAMPSHOME has a special meaning (see const.py). 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 environment = dict(os.environ)
if not 'GRAMPSHOME' in os.environ: environment.update(ENV)
os.environ['GRAMPSHOME'] = USER_HOME if not 'GRAMPSHOME' in environment:
grampshome_added = True environment['GRAMPSHOME'] = USER_HOME
path = os.path.expandvars(path) path = path.format(**environment)
if (grampshome_added): if normalize:
del os.environ['GRAMPSHOME'] path = os.path.normcase(os.path.normpath(os.path.abspath(path)))
return path return path
def media_path(db): def media_path(db):
@ -166,13 +168,13 @@ def media_path(db):
Given a database, return the mediapath to use as basedir for media Given a database, return the mediapath to use as basedir for media
""" """
mpath = db.get_mediapath() 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: Normalize a mediapath:
- Relative mediapath are considered as relative to the database - 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 to absolute path
- Convert slashes and case (on Windows) - Convert slashes and case (on Windows)
""" """
@ -180,7 +182,7 @@ def norm_media_path(mpath, db):
if mpath is None: if mpath is None:
mpath = os.path.abspath(USER_HOME) mpath = os.path.abspath(USER_HOME)
# Expand environment variables # Expand environment variables
mpath = expanded_vars_path(mpath) mpath = expand_path(mpath, False)
# Relative mediapath are considered as relative to the database # Relative mediapath are considered as relative to the database
if not os.path.isabs(mpath): if not os.path.isabs(mpath):
basepath = db.get_save_path() basepath = db.get_save_path()

View File

@ -35,11 +35,10 @@ import unittest
# Gramps modules # 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.constfunc import get_env_var
from gramps.gen.utils.file import media_path, get_empty_tempdir from gramps.gen.utils.file import media_path, get_empty_tempdir
from gramps.gen.dbstate import DbState 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")))) 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 # 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")))) 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")))) 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 # Test with $GRAMPSHOME environment variable not set
grampshome = None old_env = os.environ.copy()
if 'GRAMPSHOME' in os.environ: if 'GRAMPSHOME' in os.environ:
grampshome = os.environ['GRAMPSHOME']
del 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")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_var"))))
# Test with $GRAMPSHOME environment variable set # Test with $GRAMPSHOME environment variable set
os.environ['GRAMPSHOME'] = "/this/is/a/test" 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")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/this/is/a/test/test_var"))))
# Restore $GRAMPSHOME # Restore environment
if grampshome: os.environ = old_env
os.environ['GRAMPSHOME'] = grampshome
else:
del os.environ['GRAMPSHOME']
#------------------------------------------------------------------------- #-------------------------------------------------------------------------

View File

@ -62,7 +62,7 @@ from gramps.gen.errors import GrampsImportError
from gramps.gen.utils.id import create_id from gramps.gen.utils.id import create_id
from gramps.gen.utils.db import family_name from gramps.gen.utils.db import family_name
from gramps.gen.utils.unknown import make_unknown, create_explanation_note 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.datehandler import parser, set_date
from gramps.gen.display.name import displayer as name_displayer from gramps.gen.display.name import displayer as name_displayer
from gramps.gen.db.dbconst import (PERSON_KEY, FAMILY_KEY, SOURCE_KEY, from gramps.gen.db.dbconst import (PERSON_KEY, FAMILY_KEY, SOURCE_KEY,
@ -941,7 +941,7 @@ class GrampsParser(UpdateCallback):
if self.mediapath: if self.mediapath:
if not self.db.get_mediapath(): if not self.db.get_mediapath():
self.db.set_mediapath(self.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"), self.user.notify_error(_("Could not change media path"),
_("The opened file has media path %s, which conflicts with" _("The opened file has media path %s, which conflicts with"
" the media path of the Family Tree you import into. " " the media path of the Family Tree you import into. "