Skip to content

Commit

Permalink
FIX : avoid writing outside of build dir when running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
agramfort committed Jan 18, 2014
1 parent 01ce021 commit b84425c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
10 changes: 5 additions & 5 deletions mne/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ def test_config():
del os.environ[key]
# catch the warning about it being a non-standard config key
with warnings.catch_warnings(True) as w:
set_config(key, None)
set_config(key, None, home_dir=tempdir)
assert_true(len(w) == 1)
assert_true(get_config(key) is None)
assert_true(get_config(key, home_dir=tempdir) is None)
assert_raises(KeyError, get_config, key, raise_error=True)
with warnings.catch_warnings(True):
set_config(key, value)
assert_true(get_config(key) == value)
set_config(key, None)
set_config(key, value, home_dir=tempdir)
assert_true(get_config(key, home_dir=tempdir) == value)
set_config(key, None, home_dir=tempdir)
if old_val is not None:
os.environ[key] = old_val

Expand Down
34 changes: 23 additions & 11 deletions mne/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,26 +708,33 @@ def get_subjects_dir(subjects_dir=None, raise_error=False):
return subjects_dir


def get_config_path():
def get_config_path(home_dir=None):
"""Get path to standard mne-python config file
Parameters
----------
home_dir : str | None
The folder that contains the .mne config folder.
If None, it is found automatically.
Returns
-------
config_path : str
The path to the mne-python configuration file. On windows, this
will be '%APPDATA%\.mne\mne-python.json'. On every other
system, this will be $HOME/.mne/mne-python.json.
"""
if home_dir is None:
# this has been checked on OSX64, Linux64, and Win32
home_dir = os.getenv('APPDATA' if 'nt' == os.name.lower() else 'HOME',
None)

# this has been checked on OSX64, Linux64, and Win32
val = os.getenv('APPDATA' if 'nt' == os.name.lower() else 'HOME', None)
if val is None:
if home_dir is None:
raise ValueError('mne-python config file path could '
'not be determined, please report this '
'error to mne-python developers')

val = op.join(val, '.mne', 'mne-python.json')
return val
return op.join(home_dir, '.mne', 'mne-python.json')


def set_cache_dir(cache_dir):
Expand Down Expand Up @@ -791,7 +798,7 @@ def set_memmap_min_size(memmap_min_size):
]


def get_config(key, default=None, raise_error=False):
def get_config(key, default=None, raise_error=False, home_dir=None):
"""Read mne(-python) preference from env, then mne-python config
Parameters
Expand All @@ -804,6 +811,9 @@ def get_config(key, default=None, raise_error=False):
raise_error : bool
If True, raise an error if the key is not found (instead of returning
default).
home_dir : str | None
The folder that contains the .mne config folder.
If None, it is found automatically.
Returns
-------
Expand All @@ -819,7 +829,7 @@ def get_config(key, default=None, raise_error=False):
return os.environ[key]

# second, look for it in mne-python config file
config_path = get_config_path()
config_path = get_config_path(home_dir=home_dir)
if not op.isfile(config_path):
key_found = False
val = default
Expand All @@ -842,7 +852,7 @@ def get_config(key, default=None, raise_error=False):
return val


def set_config(key, value):
def set_config(key, value, home_dir=None):
"""Set mne-python preference in config
Parameters
Expand All @@ -852,8 +862,10 @@ def set_config(key, value):
value : str | None
The value to assign to the preference key. If None, the key is
deleted.
home_dir : str | None
The folder that contains the .mne config folder.
If None, it is found automatically.
"""

if not isinstance(key, basestring):
raise ValueError('key must be a string')
# While JSON allow non-string types, we allow users to override config
Expand All @@ -865,7 +877,7 @@ def set_config(key, value):
warnings.warn('Setting non-standard config type: "%s"' % key)

# Read all previous values
config_path = get_config_path()
config_path = get_config_path(home_dir=home_dir)
if op.isfile(config_path):
with open(config_path, 'r') as fid:
config = json.load(fid)
Expand Down

0 comments on commit b84425c

Please sign in to comment.