Skip to content

Commit 9979d61

Browse files
committed
Only load mpl style when using CLI
This avoids altering mpl settings in a script that imports stagpy. `mplstyle` and `xkcd` options of `conf.plot` are now CLI only. This commit also avoids calling `load_mplstyle` twice when using the CLI. The `load_mplstyle` function is no longer part of the public API.
1 parent 58dec4b commit 9979d61

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

stagpy/__init__.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,6 @@ def _check_config() -> None:
7575
shutil.copy(str(stfile_local), str(stfile_conf))
7676

7777

78-
def load_mplstyle() -> None:
79-
"""Try to load conf.plot.mplstyle matplotlib style."""
80-
import matplotlib.style as mpls
81-
if conf.plot.mplstyle:
82-
for style in conf.plot.mplstyle.split():
83-
style_fname = style + ".mplstyle"
84-
if not ISOLATED:
85-
stfile = config.CONFIG_DIR / style_fname
86-
if stfile.is_file():
87-
mpls.use(str(stfile))
88-
continue
89-
# try packaged version
90-
if imlr.is_resource(_styles, style_fname):
91-
with imlr.path(_styles, style_fname) as stfile:
92-
mpls.use(str(stfile))
93-
continue
94-
mpls.use(style)
95-
if conf.plot.xkcd:
96-
import matplotlib.pyplot as plt
97-
plt.xkcd()
98-
99-
10078
if DEBUG:
10179
print('StagPy runs in DEBUG mode because the environment variable',
10280
'STAGPY_DEBUG is set to "True"', sep='\n', end='\n\n')
@@ -119,7 +97,5 @@ def load_mplstyle() -> None:
11997
_check_config()
12098
PARSING_OUT = conf.read_configs_()
12199

122-
load_mplstyle()
123-
124100
if not DEBUG:
125101
signal.signal(signal.SIGINT, _PREV_INT)

stagpy/args.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
from __future__ import annotations
44
from inspect import isfunction
55
from types import MappingProxyType
6+
import importlib.resources as imlr
67
import typing
78

89
from loam.tools import set_conf_str, create_complete_files
910
from loam.cli import Subcmd, CLIManager
11+
import matplotlib.pyplot as plt
12+
import matplotlib.style as mpls
1013

1114
from . import __doc__ as doc_module
12-
from . import conf, PARSING_OUT, load_mplstyle
13-
from . import commands, field, rprof, time_series, refstate, plates
15+
from . import conf, PARSING_OUT, ISOLATED
16+
from . import (
17+
commands, config, field, rprof, time_series, refstate, plates, _styles,
18+
)
1419
from ._helpers import baredoc
1520
from .config import CONFIG_DIR
1621

@@ -30,6 +35,26 @@ def _bare_cmd() -> None:
3035
print('Run `stagpy -h` for usage')
3136

3237

38+
def _load_mplstyle() -> None:
39+
"""Try to load conf.plot.mplstyle matplotlib style."""
40+
if conf.plot.mplstyle:
41+
for style in conf.plot.mplstyle.split():
42+
style_fname = style + ".mplstyle"
43+
if not ISOLATED:
44+
stfile = config.CONFIG_DIR / style_fname
45+
if stfile.is_file():
46+
mpls.use(str(stfile))
47+
continue
48+
# try packaged version
49+
if imlr.is_resource(_styles, style_fname):
50+
with imlr.path(_styles, style_fname) as stfile:
51+
mpls.use(str(stfile))
52+
continue
53+
mpls.use(style)
54+
if conf.plot.xkcd:
55+
plt.xkcd()
56+
57+
3358
SUB_CMDS = MappingProxyType({
3459
'common_': Subcmd(doc_module, 'common', func=_bare_cmd),
3560
'field': _sub(field, 'core', 'plot', 'scaling'),
@@ -75,6 +100,6 @@ def parse_args(arglist: Optional[List[str]] = None) -> Callable[[], None]:
75100
if conf.common.config:
76101
commands.config_pp(climan.sections_list(sub_cmd))
77102

78-
load_mplstyle()
103+
_load_mplstyle()
79104

80105
return cmd_args.func

stagpy/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
False, 'arbitrary isoline value, comma separated'),
5757
mplstyle=Conf('stagpy-paper', True, None,
5858
{'nargs': '?', 'const': '', 'type': str},
59-
True, 'matplotlib style'),
60-
xkcd=Conf(False, False, None, {}, True, 'use the xkcd style'),
59+
False, 'matplotlib style'),
60+
xkcd=command_flag(None, 'use the xkcd style'),
6161
)
6262

6363
CONF_DEF['scaling'] = dict(

0 commit comments

Comments
 (0)