Skip to content
Prev Previous commit
Next Next commit
Add parsing functions to transform user-config to mpl-config
  • Loading branch information
tonysyu committed Jan 17, 2014
commit 8caa77d61c918e4825c3e7a0c6a396a1f081475a
8 changes: 8 additions & 0 deletions lib/matplotlib/config/config_alias_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"lines.linewidth":
[
"collections.LineCollection:__init__:linewidths",
"contour.ContourSet:__init__:linewidths",
"lines.Line2D:__init__:linewidth"
]
}
66 changes: 66 additions & 0 deletions lib/matplotlib/config/parse_user_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
import json

import six


LOCAL_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_ALIAS_MAPPING = os.path.join(LOCAL_DIR, 'config_alias_map.json')
ALIAS_MAPPING = {}


def update_config_from_dict_path(config_dict, dict_path, value):
"""Set value in a config dictionary using a path string.

Parameters
----------
config_dict : dict
Configuration dictionary matching format expected by
``matplotlib.config.mpl_config.MPLConfig``.
dict_path : str
String with nested dictionary keys separated by a colon.
For example, 'a:b:c' maps to the key ``some_dict['a']['b']['c']``.
value : object
Configuration value.
"""
dict_keys = dict_path.split(':')
key_to_set = dict_keys.pop()

inner_dict = config_dict
for key in dict_keys:
if key not in inner_dict:
inner_dict[key] = {}
inner_dict = inner_dict[key]
inner_dict[key_to_set] = value


def load_config_mapping(filename):
"""Return dictionary mapping config labels to config paths.
"""
with open(filename) as f:
config_mapping = json.load(f)
return config_mapping


def update_alias_mapping(filename):
"""Update mappings from user-config aliases to config dict paths. """
ALIAS_MAPPING.update(load_config_mapping(filename))

update_alias_mapping(DEFAULT_ALIAS_MAPPING)


def user_key_to_dict_paths(key):
"""Return config-dict paths from user-config alias.

See also ``update_config_from_dict_path``.
"""
return ALIAS_MAPPING[key]


def update_config_dict_from_user_config(config_dict, user_config):
"""Update internal configuration dict from user-config dict.
"""
for user_key, value in six.iteritems(user_config):
dict_paths = user_key_to_dict_paths(user_key)
for path in dict_paths:
update_config_from_dict_path(config_dict, path, value)
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from matplotlib.config.parse_user_config import (
update_config_from_dict_path, update_config_dict_from_user_config
)


def test_set_config_dict_path():
config_dict = {}
update_config_from_dict_path(config_dict, 'a:b:c', 1)
assert config_dict['a']['b']['c'] == 1


def test_set_config_dict_values_user_config():
user_config = {'lines.linewidth': 100}
config_dict = {}
update_config_dict_from_user_config(config_dict, user_config)

value = config_dict['collections.LineCollection']['__init__']['linewidths']
assert value == 100
value = config_dict['contour.ContourSet']['__init__']['linewidths']
assert value == 100
value = config_dict['lines.Line2D']['__init__']['linewidth']
assert value == 100


if __name__ == '__main__':
from numpy import testing
testing.run_module_suite()