8000 Merge pull request #15029 from anntzer/rc2 · matplotlib/matplotlib@5698325 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5698325

Browse files
authored
Merge pull request #15029 from anntzer/rc2
Get default params from matplotlibrc.template.
2 parents 2240f34 + 178515c commit 5698325

File tree

6 files changed

+468
-530
lines changed

6 files changed

+468
-530
lines changed

.flake8

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,8 @@ per-file-ignores =
6363
lib/matplotlib/mathtext.py: E221, E251
6464
lib/matplotlib/pylab.py: F401, F403
6565
lib/matplotlib/pyplot.py: F401, F811
66-
lib/matplotlib/rcsetup.py: E501
6766
lib/matplotlib/style/__init__.py: F401
6867
lib/matplotlib/testing/conftest.py: F401
69-
lib/matplotlib/testing/compare.py: F401
7068
lib/matplotlib/testing/decorators.py: F401
7169
lib/matplotlib/tests/conftest.py: F401
7270
lib/matplotlib/tests/test_backend_qt.py: F401

lib/matplotlib/__init__.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
from . import cbook, rcsetup
108108
from matplotlib.cbook import MatplotlibDeprecationWarning, sanitize_sequence
109109
from matplotlib.cbook import mplDeprecation # deprecated
110-
from matplotlib.rcsetup import defaultParams, validate_backend, cycler
110+
from matplotlib.rcsetup import validate_backend, cycler
111111

112112
import numpy
113113

@@ -524,7 +524,6 @@ def get_data_path(*, _from_rc=None):
524524
removal='3.4')
525525
path = Path(_from_rc)
526526
if path.is_dir():
527-
defaultParams['datapath'][0] = str(path)
528527
return str(path)
529528
else:
530529
warnings.warn(f"You passed datapath: {_from_rc!r} in your "
@@ -539,7 +538,6 @@ def get_data_path(*, _from_rc=None):
539538
def _get_data_path():
540539
path = Path(__file__).with_name("mpl-data")
541540
if path.is_dir():
542-
defaultParams['datapath'][0] = str(path)
543541
return str(path)
544542

545543
cbook.warn_deprecated(
@@ -648,9 +646,7 @@ class RcParams(MutableMapping, dict):
648646
:ref:`customizing-with-matplotlibrc-files`
649647
"""
650648

651-
validate = {key: converter
652-
for key, (default, converter) in defaultParams.items()
653-
if key not in _all_deprecated}
649+
validate = rcsetup._validators
654650

655651
# validate values on the way in
656652
def __init__(self, *args, **kwargs):
@@ -706,6 +702,9 @@ def __getitem__(self, key):
706702
from matplotlib import pyplot as plt
707703
plt.switch_backend(rcsetup._auto_backend_sentinel)
708704

705+
elif key == "datapath":
706+
return get_data_path()
707+
709708
return dict.__getitem__(self, key)
710709

711710
def __repr__(self):
@@ -776,19 +775,31 @@ def _open_file_or_url(fname):
776775
yield f
777776

778777

779-
def _rc_params_in_file(fname, fail_on_error=False):
778+
def _rc_params_in_file(fname, transform=lambda x: x, fail_on_error=False):
780779
"""
781780
Construct a `RcParams` instance from file *fname*.
782781
783782
Unlike `rc_params_from_file`, the configuration class only contains the
784783
parameters specified in the file (i.e. default values are not filled in).
784+
785+
Parameters
786+
----------
787+
fname : path-like
788+
The loaded file.
789+
transform : callable, default: the identity function
790+
A function called on each individual line of the file to transform it,
791+
before further parsing.
792+
fail_on_error : bool, default: False
793+
Whether invalid entries should result in an exception or a warning.
785794
"""
795+
786796
_error_details_fmt = 'line #%d\n\t"%s"\n\tin file "%s"'
787797

788798
rc_temp = {}
789799
with _open_file_or_url(fname) as fd:
790800
try:
791801
for line_no, line in enumerate(fd, 1):
802+
line = transform(line)
792803
strippedline = line.split('#', 1)[0].strip()
793804
if not strippedline:
794805
continue
@@ -815,7 +826,7 @@ def _rc_params_in_file(fname, fail_on_error=False):
815826
config = RcParams()
816827

817828
for key, (val, line, line_no) in rc_temp.items():
818-
if key in defaultParams:
829+
if key in rcsetup._validators:
819830
if fail_on_error:
820831
config[key] = val # try to convert to proper type or raise
821832
else:
@@ -856,16 +867,13 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
856867
in the given file. If False, the configuration class only contains the
857868
parameters specified in the file. (Useful for updating dicts.)
858869
"""
859-
config_from_file = _rc_params_in_file(fname, fail_on_error)
870+
config_from_file = _rc_params_in_file(fname, fail_on_error=fail_on_error)
860871

861872
if not use_default_template:
862873
return config_from_file
863874

864-
iter_params = defaultParams.items()
865875
with cbook._suppress_matplotlib_deprecation_warning():
866-
config = RcParams([(key, default) for key, (default, _) in iter_params
867-
if key not in _all_deprecated])
868-
config.update(config_from_file)
876+
config = RcParams({**rcParamsDefault, **config_from_file})
869877

870878
with cbook._suppress_matplotlib_deprecation_warning():
871879
if config['datapath'] is None:
@@ -886,16 +894,28 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
886894
return config
887895

888896

889-
# this is the instance used by the matplotlib classes
890-
rcParams = rc_params()
891-
892-
897+
# When constructing the global instances, we need to perform certain updates
898+
# by explicitly calling the superclass (dict.update, dict.items) to avoid
899+
# triggering resolution of _auto_backend_sentinel.
900+
rcParamsDefault = _rc_params_in_file(
901+
cbook._get_data_path("matplotlibrc"),
902+
# Strip leading comment.
903+
transform=lambda line: line[1:] if line.startswith("#") else line,
904+
fail_on_error=True)
905+
dict.update(rcParamsDefault, rcsetup._hardcoded_defaults)
906+
rcParams = RcParams() # The global instance.
907+
dict.update(rcParams, dict.items(rcParamsDefault))
908+
dict.update(rcParams, _rc_params_in_file(matplotlib_fname()))
893909
with cbook._suppress_matplotlib_deprecation_warning():
894910
rcParamsOrig = RcParams(rcParams.copy())
895-
rcParamsDefault = RcParams([(key, default) for key, (default, converter) in
896-
defaultParams.items()
897-
if key not in _all_deprecated])
898-
911+
# This also checks that all rcParams are indeed listed in the template.
912+
# Assiging to rcsetup.defaultParams is left only for backcompat.
913+
defaultParams = rcsetup.defaultParams = {
914+
# We want to resolve deprecated rcParams, but not backend...
915+
key: [(rcsetup._auto_backend_sentinel if key == "backend" else
916+
rcParamsDefault[key]),
917+
validator]
918+
for key, validator in rcsetup._validators.items()}
899919
if rcParams['axes.formatter.use_locale']:
900920
locale.setlocale(locale.LC_ALL, '')
901921

lib/matplotlib/cbook/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def get_sample_data(fname, asfileobj=True):
444444
445445
If the filename ends in .gz, the file is implicitly ungzipped.
446446
"""
447-
path = Path(matplotlib.get_data_path(), 'sample_data', fname)
447+
path = _get_data_path('sample_data', fname)
448448
if asfileobj:
449449
suffix = path.suffix.lower()
450450
if suffix == '.gz':

0 commit comments

Comments
 (0)
0