Repository: mne-tools/mne-python

Issue:
BUG: Download dir
We should default to `~/` somewhere, people during teaching are having problems with putting it in `examples/` by default.



Discussion:
+10

+11

How about `~/mne_data`? If that's acceptable I'll make a PR

yes +1 for ~/mne_data like sklearn and nilearn do it


Resolving patch:
diff --git a/mne/datasets/utils.py b/mne/datasets/utils.py
--- a/mne/datasets/utils.py
+++ b/mne/datasets/utils.py
@@ -95,35 +95,31 @@ def _dataset_version(path, name):
 
 def _get_path(path, key, name):
     """Helper to get a dataset path"""
-    if path is None:
-        # use an intelligent guess if it's not defined
-        def_path = op.realpath(op.join(op.dirname(__file__), '..', '..',
-                                       'examples'))
-        if get_config(key) is None:
-            key = 'MNE_DATA'
-        path = get_config(key, def_path)
-
-        # use the same for all datasets
-        if not op.exists(path) or not os.access(path, os.W_OK):
-            try:
-                os.mkdir(path)
-            except OSError:
-                try:
-                    logger.info('Checking for %s data in '
-                                '"~/mne_data"...' % name)
-                    path = op.join(op.expanduser("~"), "mne_data")
-                    if not op.exists(path):
-                        logger.info("Trying to create "
-                                    "'~/mne_data' in home directory")
-                        os.mkdir(path)
-                except OSError:
-                    raise OSError("User does not have write permissions "
-                                  "at '%s', try giving the path as an "
-                                  "argument to data_path() where user has "
-                                  "write permissions, for ex:data_path"
-                                  "('/home/xyz/me2/')" % (path))
-    if not isinstance(path, string_types):
-        raise ValueError('path must be a string or None')
+    # 1. Input
+    if path is not None:
+        if not isinstance(path, string_types):
+            raise ValueError('path must be a string or None')
+        return path
+    # 2. get_config(key)
+    # 3. get_config('MNE_DATA')
+    path = get_config(key, get_config('MNE_DATA'))
+    if path is not None:
+        return path
+    # 4. ~/mne_data (but use a fake home during testing so we don't
+    #    unnecessarily create ~/mne_data)
+    logger.info('Using default location ~/mne_data for %s...' % name)
+    path = op.join(os.getenv('_MNE_FAKE_HOME_DIR',
+                             op.expanduser("~")), 'mne_data')
+    if not op.exists(path):
+        logger.info('Creating ~/mne_data')
+        try:
+            os.mkdir(path)
+        except OSError:
+            raise OSError("User does not have write permissions "
+                          "at '%s', try giving the path as an "
+                          "argument to data_path() where user has "
+                          "write permissions, for ex:data_path"
+                          "('/home/xyz/me2/')" % (path))
     return path
 
 
diff --git a/mne/utils.py b/mne/utils.py
--- a/mne/utils.py
+++ b/mne/utils.py
@@ -1130,6 +1130,8 @@ def get_subjects_dir(subjects_dir=None, raise_error=False):
 def _get_extra_data_path(home_dir=None):
     """Get path to extra data (config, tables, etc.)"""
     global _temp_home_dir
+    if home_dir is None:
+        home_dir = os.environ.get('_MNE_FAKE_HOME_DIR')
     if home_dir is None:
         # this has been checked on OSX64, Linux64, and Win32
         if 'nt' == os.name.lower():


Test patch:
diff --git a/mne/datasets/tests/test_datasets.py b/mne/datasets/tests/test_datasets.py
--- a/mne/datasets/tests/test_datasets.py
+++ b/mne/datasets/tests/test_datasets.py
@@ -1,3 +1,4 @@
+import os
 from os import path as op
 from nose.tools import assert_true, assert_equal
 
@@ -19,6 +20,15 @@ def test_datasets():
         assert_true(isinstance(dataset.get_version(), string_types))
     else:
         assert_true(dataset.get_version() is None)
+    tempdir = _TempDir()
+    # don't let it read from the config file to get the directory,
+    # force it to look for the default
+    os.environ['_MNE_FAKE_HOME_DIR'] = tempdir
+    try:
+        assert_equal(datasets.utils._get_path(None, 'foo', 'bar'),
+                     op.join(tempdir, 'mne_data'))
+    finally:
+        del os.environ['_MNE_FAKE_HOME_DIR']
 
 
 @requires_good_network