8000 Add rc_params_in_file to return partially-filled RcParams · matplotlib/matplotlib@3270aa4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3270aa4

Browse files
committed
Add rc_params_in_file to return partially-filled RcParams
This allows style sheets to update the current settings instead of overwriting them.
1 parent 643c74b commit 3270aa4

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

lib/matplotlib/__init__.py

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -878,9 +878,14 @@ def rc_params(fail_on_error=False):
878878
return rc_params_from_file(fname, fail_on_error)
879879

880880

881-
def rc_params_from_file(fname, fail_on_error=False):
882-
"""Return a :class:`matplotlib.RcParams` instance from the
883-
contents of the given filename.
881+
_error_details_fmt = 'line #%d\n\t"%s"\n\tin file "%s"'
882+
883+
884+
def rc_params_in_file(fname, fail_on_error=False):
885+
"""Return :class:`matplotlib.RcParams` from the contents of the given file.
886+
887+
Unlike `rc_params_from_file`, the configuration class only contains the
888+
parameters specified in the file (i.e. default values are not filled in).
884889
"""
885890
cnt = 0
886891
rc_temp = {}
@@ -891,8 +896,8 @@ def rc_params_from_file(fname, fail_on_error=False):
891896
if not strippedline: continue
892897
tup = strippedline.split(':', 1)
893898
if len(tup) != 2:
894-
warnings.warn('Illegal line #%d\n\t%s\n\tin file "%s"' % \
895-
(cnt, line, fname))
899+
error_details = _error_details_fmt % (cnt, line, fname)
900+
warnings.warn('Illegal %s' % error_details)
896901
continue
897902
key, val = tup
898903
key = key.strip()
@@ -902,34 +907,35 @@ def rc_params_from_file(fname, fail_on_error=False):
902907
(fname, cnt))
903908
rc_temp[key] = (val, line, cnt)
904909

905-
ret = RcParams([(key, default) for key, (default, _) in \
906-
six.iteritems(defaultParams)])
910+
config = RcParams()
907911

908912
for key in ('verbose.level', 'verbose.fileo'):
909913
if key in rc_temp:
910914
val, line, cnt = rc_temp.pop(key)
911915
if fail_on_error:
912-
ret[key] = val # try to convert to proper type or raise
916+
config[key] = val # try to convert to proper type or raise
913917
else:
914-
try: ret[key] = val # try to convert to proper type or skip
918+
try:
919+
config[key] = val # try to convert to proper type or skip
915920
except Exception as msg:
916-
warnings.warn('Bad val "%s" on line #%d\n\t"%s"\n\tin file \
917-
"%s"\n\t%s' % (val, cnt, line, fname, msg))
918-
919-
verbose.set_level(ret['verbose.level'])
920-
verbose.set_fileo(ret['verbose.fileo'])
921+
error_details = _error_details_fmt % (cnt, line, fname)
922+
warnings.warn('Bad val "%s" on %s\n\t%s' %
923+
(val, error_details, msg))
921924

922925
for key, (val, line, cnt) in six.iteritems(rc_temp):
923926
if key in defaultParams:
924927
if fail_on_error:
925-
ret[key] = val # try to convert to proper type or raise
928+
config[key] = val # try to convert to proper type or raise
926929
else:
927-
try: ret[key] = val # try to convert to proper type or skip
930+
try:
931+
config[key] = val # try to convert to proper type or skip
928932
except Exception as msg:
929-
warnings.warn('Bad val "%s" on line #%d\n\t"%s"\n\tin file \
930-
"%s"\n\t%s' % (val, cnt, line, fname, msg))
933+
error_details = _error_details_fmt % (cnt, line, fname)
934+
warnings.warn('Bad val "%s" on %s\n\t%s' %
935+
(val, error_details, msg))
931936
elif key in _deprecated_ignore_map:
932-
warnings.warn('%s is deprecated. Update your matplotlibrc to use %s instead.'% (key, _deprecated_ignore_map[key]))
937+
warnings.warn('%s is deprecated. Update your matplotlibrc to use '
938+
'%s instead.'% (key, _deprecated_ignore_map[key]))
933939

934940
else:
935941
print("""
@@ -939,21 +945,45 @@ def rc_params_from_file(fname, fail_on_error=False):
939945
http://matplotlib.sf.net/_static/matplotlibrc or from the matplotlib source
940946
distribution""" % (key, cnt, fname), file=sys.stderr)
941947

942-
if ret['datapath'] is None:
943-
ret['datapath'] = get_data_path()
948+
return config
949+
950+
951+
952+
953+
def rc_params_from_file(fname, fail_on_error=False):
954+
"""Return :class:`matplotlib.RcParams` from the contents of the given file.
955+
956+
Parameters
957+
----------
958+
fname : str
959+
Name of file parsed for matplotlib settings.
960+
fail_on_error : bool
961+
If True, raise an error when the parser fails to convert a parameter.
962+
"""
963+
964+
config = RcParams([(key, default)
965+
for key, (default, _) in six.iteritems(defaultParams)])
966+
967+
config.update(rc_params_in_file(fname, fail_on_error))
968+
969+
verbose.set_level(config['verbose.level'])
970+
verbose.set_fileo(config['verbose.fileo'])
971+
972+
if config['datapath'] is None:
973+
config['datapath'] = get_data_path()
944974

945-
if not ret['text.latex.preamble'] == ['']:
975+
if not config['text.latex.preamble'] == ['']:
946976
verbose.report("""
947977
*****************************************************************
948978
You have the following UNSUPPORTED LaTeX preamble customizations:
949979
%s
950980
Please do not ask for support with these customizations active.
951981
*****************************************************************
952-
"""% '\n'.join(ret['text.latex.preamble']), 'helpful')
982+
"""% '\n'.join(config['text.latex.preamble']), 'helpful')
953983

954984
verbose.report('loaded rc file %s'%fname)
955985

956-
return ret
986+
return config
957987

958988

959989
# this is the instance used by the matplotlib classes

lib/matplotlib/style/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def read_style_directory(style_dir):
7777
"""Return dictionary of styles defined in `style_dir`."""
7878
styles = dict()
7979
for path, name in iter_style_files(style_dir):
80-
styles[name] = mpl.rc_params_from_file(path)
80+
styles[name] = mpl.rc_params_in_file(path)
8181
return styles
8282

8383

0 commit comments

Comments
 (0)
0