Skip to content

Commit b6c44ad

Browse files
committed
Add method to extract fmts from preset
1 parent c4cc90f commit b6c44ad

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎psyplot/project.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,36 @@ def load_preset(self, preset: str, **kwargs):
484484
sp.update(fmt=fmts, **kwargs)
485485
self.start_update()
486486

487+
@staticmethod
488+
def extract_fmts_from_preset(preset: str, plotmethod: str):
489+
"""Extract the formatoptions for a plotmethod from a given preset
490+
491+
This method takes the preset and extracts the formatoptions valid for
492+
the given plotmethod
493+
494+
Parameters
495+
----------
496+
%(Project._load_preset.parameters)s
497+
plotmethod: str
498+
The plotmethod to use"""
499+
preset = Project._load_preset(preset)
500+
try:
501+
plotmethod._method
502+
except AttributeError:
503+
method = getattr(plot, plotmethod)
504+
else:
505+
method = plotmethod
506+
plotmethod = method._method
507+
508+
plotmethods = plot._plot_methods
509+
pm_config, defaults = utils.sort_kwargs(preset, plotmethods)
510+
valid = list(method.plotter_cls._get_formatoptions())
511+
fmts = {key: val for key, val in defaults.items()
512+
if key in valid}
513+
fmts.update(pm_config.get(plotmethod, {}))
514+
return fmts
515+
516+
487517
def save_preset(self, fname=None, include_defaults=False, update=False):
488518
"""Save the formatoptions of this project as a preset
489519

‎tests/test_project.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ def test_load_preset(project, preset, path):
5858
os.remove(path)
5959

6060

61+
def test_extract_preset(project):
62+
preset = {"fmt1": "test1", "test_plotter": {"fmt2": "test2"},
63+
"not_existent": 1}
64+
fmts = project.extract_fmts_from_preset(preset, "test_plotter")
65+
assert fmts == {"fmt1": "test1", "fmt2": "test2"}
66+
67+
6168
def test_save_preset(project):
6269
sp = project.plot.test_plotter(xr.Dataset({"x": (('a'), [1])}),
6370
name=['x', 'x'])

0 commit comments

Comments
 (0)