Skip to content

Commit

Permalink
Create a GRAMPS environment ENV for variables substitutions in paths
Browse files Browse the repository at this point in the history
  • Loading branch information
belissent committed Aug 20, 2015
1 parent 4bbae0e commit 82eb6e1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
2 changes: 1 addition & 1 deletion example/gramps/example.gramps
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<researcher>
<resname>Alex Roitman,,,</resname>
</researcher>
<mediapath>$GRAMPS_RESOURCES/example/gramps</mediapath>
<mediapath>{GRAMPS_RESOURCES}/example/gramps</mediapath>
</header>
<name-formats>
<format number="-1" name="SURNAME, Given (Common)" fmt_str="SURNAME, given (common)" active="1"/>
Expand Down
42 changes: 26 additions & 16 deletions gramps/gen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,6 @@
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
Expand Down Expand Up @@ -187,6 +171,32 @@
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
Expand Down
32 changes: 17 additions & 15 deletions gramps/gen/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#
#-------------------------------------------------------------------------
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

#-------------------------------------------------------------------------
#
Expand Down Expand Up @@ -146,41 +146,43 @@ 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):
"""
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)
"""
# Use home dir if no media_path specified
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()
Expand Down
23 changes: 10 additions & 13 deletions gramps/gen/utils/test/file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
# 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

#-------------------------------------------------------------------------
#
Expand Down Expand Up @@ -71,29 +70,27 @@ def test_mediapath(self):
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


#-------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions gramps/plugins/importer/importxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
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,
Expand Down Expand Up @@ -941,7 +941,7 @@ def parse(self, ifile, linecount=0, personcount=0):
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. "
Expand Down

0 comments on commit 82eb6e1

Please sign in to comment.