Repository: mne-tools/mne-python

Issue:
API: mne.get_config
should be callable without arguments and should return all mappings



Discussion:
+1

On Fri, Jan 31, 2014 at 6:20 PM, Denis A. Engemann <notifications@github.com

> wrote:
> 
> should be callable without arguments and should return all mappings
> 
> ## 
> 
> Reply to this email directly or view it on GitHubhttps://github.com/mne-tools/mne-python/issues/1112
> .

Should it return a json object?

@saketkc simply a dictionary. Once you open a json file and represent it's contents in Python what you get is a dict.

I think a `dict` would probably be more convenient.

Yes, my choice of `json` was a bit off, that's what I meant :)

@saketkc anyways, PR welcome :-) We can think about adding custom representations should we come to the conclusion that returning all json mappings as a dict is messy. E.g. as we did it with info objects https://github.com/mne-tools/mne-python/blob/master/mne/fiff/meas_info.py#L37


Resolving patch:
diff --git a/mne/utils.py b/mne/utils.py
--- a/mne/utils.py
+++ b/mne/utils.py
@@ -885,14 +885,15 @@ def set_memmap_min_size(memmap_min_size):
     ]
 
 
-def get_config(key, default=None, raise_error=False, home_dir=None):
+def get_config(key=None, default=None, raise_error=False, home_dir=None):
     """Read mne(-python) preference from env, then mne-python config
 
     Parameters
     ----------
-    key : str
+    key : None | str
         The preference key to look for. The os evironment is searched first,
         then the mne-python config file is parsed.
+        If None, all the config parameters present in the path are returned.
     default : str | None
         Value to return if the key is not found.
     raise_error : bool
@@ -904,15 +905,15 @@ def get_config(key, default=None, raise_error=False, home_dir=None):
 
     Returns
     -------
-    value : str | None
+    value : dict | str | None
         The preference key value.
     """
 
-    if not isinstance(key, string_types):
+    if key is not None and not isinstance(key, string_types):
         raise ValueError('key must be a string')
 
     # first, check to see if key is in env
-    if key in os.environ:
+    if key is not None and key in os.environ:
         return os.environ[key]
 
     # second, look for it in mne-python config file
@@ -923,6 +924,8 @@ def get_config(key, default=None, raise_error=False, home_dir=None):
     else:
         with open(config_path, 'r') as fid:
             config = json.load(fid)
+            if key is None:
+                return config
         key_found = True if key in config else False
         val = config.get(key, default)
 


Test patch:
diff --git a/mne/tests/test_utils.py b/mne/tests/test_utils.py
--- a/mne/tests/test_utils.py
+++ b/mne/tests/test_utils.py
@@ -137,6 +137,11 @@ def test_config():
         set_config(key, None, home_dir=tempdir)
     if old_val is not None:
         os.environ[key] = old_val
+    # Check if get_config with no input returns all config
+    key = 'MNE_PYTHON_TESTING_KEY'
+    config = {key: value}
+    set_config(key, value, home_dir=tempdir)
+    assert_equal(get_config(home_dir=tempdir), config)
 
 
 def test_show_fiff():