Skip to content

Commit 6ea48d5

Browse files
committed
Improve _load_file implementation (fix #333)
1 parent 428227d commit 6ea48d5

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

WHATSNEW.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,10 @@ Bug fixes
235235
max of `levels` when passed to `~proplot.colors.DiscreteNorm` (:commit:`e9ed16c1`).
236236
* Fix issue where unevenly-spaced `levels` combined with
237237
`~proplot.colors.DiscreteColormap` incorrectly samples the color list (:issue:`299`).
238-
* Fix issue where `~proplot.axes.Axes.legend` ignores the user-input
239-
`fontsize` (:issue:`331`).
238+
* Fix issue where `~proplot.axes.Axes.legend` ignores the user-input `fontsize`
239+
(:issue:`331`).
240+
* Fix issue where ``proplotrc`` settings are ignored if a subsequent line contains
241+
an overlapping meta-setting (:issue:`333`).
240242
* Fix issue where setting :rcraw:`legend.facecolor` or :rcraw:`legend.edgecolor` to
241243
``'inherit'`` (or passing as keyword argument) raises error (:issue:`298`).
242244
* Fix issue where settings :rcraw:`grid.pad` and :rcraw:`grid.labelpad` and settings

proplot/config.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,16 +1411,17 @@ def context(self, *args, mode=0, file=None, **kwargs):
14111411
>>> ax.format(ticklen=5, metalinewidth=2)
14121412
"""
14131413
# Add input dictionaries
1414+
# WARNING: Critical to fully apply
14141415
for arg in args:
14151416
if not isinstance(arg, dict):
1416-
raise ValueError('Non-dictionary argument {arg!r}.')
1417+
raise ValueError(f'Non-dictionary argument {arg!r}.')
14171418
kwargs.update(arg)
14181419

14191420
# Add settings from file
14201421
if file is not None:
1421-
kw_proplot, kw_matplotlib = self._load_file(file)
1422-
kwargs.update(kw_proplot)
1423-
kwargs.update(kw_matplotlib)
1422+
kw = self._load_file(file)
1423+
kw = {key: value for key, value in kw.items() if key not in kwargs}
1424+
kwargs.update(kw)
14241425

14251426
# Activate context object
14261427
if mode not in range(3):
@@ -1569,10 +1570,11 @@ def _load_file(self, path):
15691570
"""
15701571
Return dictionaries of proplot and matplotlib settings loaded from the file.
15711572
"""
1572-
added = set()
1573+
# WARNING: Critical to not yet apply _get_item_dicts() syncing or else we
1574+
# can overwrite input settings (e.g. label.size followed by font.size).
15731575
path = os.path.expanduser(path)
1574-
kw_proplot = {}
1575-
kw_matplotlib = {}
1576+
added = set()
1577+
rcdict = {}
15761578
with open(path, 'r') as fh:
15771579
for idx, line in enumerate(fh):
15781580
# Strip comments
@@ -1587,33 +1589,34 @@ def _load_file(self, path):
15871589
warnings._warn_proplot(f'Illegal {message}:\n{line}"')
15881590
continue
15891591
# Detect duplicates
1590-
key, val = map(str.strip, pair)
1592+
key, value = map(str.strip, pair)
15911593
if key in added:
15921594
warnings._warn_proplot(f'Duplicate rc key {key!r} on {message}.')
15931595
added.add(key)
15941596
# Get child dictionaries. Careful to have informative messages
15951597
with warnings.catch_warnings():
15961598
warnings.simplefilter('error', warnings.ProplotWarning)
15971599
try:
1598-
ikw_proplot, ikw_matplotlib = self._get_item_dicts(key, val)
1600+
key, value = self._validate_key(key, value)
1601+
value = self._validate_value(key, value)
15991602
except KeyError:
16001603
warnings.simplefilter('default', warnings.ProplotWarning)
16011604
warnings._warn_proplot(f'Invalid rc key {key!r} on {message}.')
16021605
continue
16031606
except ValueError as err:
16041607
warnings.simplefilter('default', warnings.ProplotWarning)
1605-
warnings._warn_proplot(f'Invalid rc val {val!r} for key {key!r} on {message}: {err}') # noqa: E501
1608+
warnings._warn_proplot(f'Invalid rc value {value!r} for key {key!r} on {message}: {err}') # noqa: E501
16061609
continue
16071610
except warnings.ProplotWarning as err:
16081611
warnings.simplefilter('default', warnings.ProplotWarning)
16091612
warnings._warn_proplot(f'Outdated rc key {key!r} on {message}: {err}') # noqa: E501
16101613
warnings.simplefilter('ignore', warnings.ProplotWarning)
1611-
ikw_proplot, ikw_matplotlib = self._get_item_dicts(key, val)
1614+
key, value = self._validate_key(key, value)
1615+
value = self._validate_value(key, value)
16121616
# Update the settings
1613-
kw_proplot.update(ikw_proplot)
1614-
kw_matplotlib.update(ikw_matplotlib)
1617+
rcdict[key] = value
16151618

1616-
return kw_proplot, kw_matplotlib
1619+
return rcdict
16171620

16181621
def load(self, path):
16191622
"""
@@ -1628,9 +1631,9 @@ def load(self, path):
16281631
--------
16291632
Configurator.save
16301633
"""
1631-
kw_proplot, kw_matplotlib = self._load_file(path)
1632-
rc_proplot.update(kw_proplot)
1633-
rc_matplotlib.update(kw_matplotlib)
1634+
rcdict = self._load_file(path)
1635+
for key, value in rcdict.items():
1636+
self.__setitem__(key, value)
16341637

16351638
@staticmethod
16361639
def _save_rst(path):
@@ -1688,10 +1691,12 @@ def save(self, path=None, user=True, comment=None, backup=True, description=Fals
16881691
backup : bool, default: True
16891692
Whether to "backup" an existing file by renaming with the suffix ``.bak``
16901693
or overwrite an existing file.
1691-
comment : bool, default: `user`
1692-
Whether to comment out the default settings.
1694+
comment : bool, optional
1695+
Whether to comment out the default settings. If not passed
1696+
this takes the same value as `user`.
16931697
description : bool, default: False
1694-
Whether to include descriptions of each setting as comments.
1698+
Whether to include descriptions of each setting (as seen in the
1699+
:ref:`user guide table <ug_rctable>`) as comments.
16951700
16961701
See also
16971702
--------

0 commit comments

Comments
 (0)