Skip to content

Commit 1dcf301

Browse files
committed
Use importlib.resources to access mpl styles
1 parent 860ff81 commit 1dcf301

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

setup.cfg

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ classifiers =
2727
python_requires = >=3.7
2828
include_package_data = true
2929
packages =
30-
stagpy
30+
find:
3131
install_requires =
3232
loam>=0.5.2,<0.6.0
3333
f90nml>=1.3.1
@@ -43,4 +43,9 @@ console_scripts =
4343
stagpy = stagpy.__main__:main
4444

4545
[options.package_data]
46-
stagpy = py.typed, stagpy-*.mplstyle
46+
stagpy = py.typed
47+
stagpy._styles = stagpy-*.mplstyle
48+
49+
[options.packages.find]
50+
include = stagpy*
51+
exclude = stagpy_git

stagpy/__init__.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
"""
1818

1919
from __future__ import annotations
20+
import importlib.resources as imlr
2021
import os
21-
import pathlib
2222
import shutil
2323
import signal
2424
import sys
@@ -27,10 +27,10 @@
2727
from setuptools_scm import get_version
2828
from loam.manager import ConfigurationManager
2929

30-
from . import config
30+
from . import config, _styles
3131

3232
if typing.TYPE_CHECKING:
33-
from typing import NoReturn, Any
33+
from typing import NoReturn, Any, Iterator
3434

3535

3636
def _env(var: str) -> bool:
@@ -53,6 +53,12 @@ def sigint_handler(*_: Any) -> NoReturn:
5353
sys.exit()
5454

5555

56+
def _iter_styles() -> Iterator[str]:
57+
for resource in imlr.contents(_styles):
58+
if resource.endswith(".mplstyle"):
59+
yield resource
60+
61+
5662
def _check_config() -> None:
5763
"""Create config files as necessary."""
5864
config.CONFIG_DIR.mkdir(parents=True, exist_ok=True)
@@ -62,12 +68,11 @@ def _check_config() -> None:
6268
verfile.write_text(__version__)
6369
if not (uptodate and config.CONFIG_FILE.is_file()):
6470
conf.create_config_(update=True)
65-
for stfile in ('stagpy-paper.mplstyle',
66-
'stagpy-slides.mplstyle'):
71+
for stfile in _iter_styles():
6772
stfile_conf = config.CONFIG_DIR / stfile
6873
if not (uptodate and stfile_conf.is_file()):
69-
stfile_local = pathlib.Path(__file__).parent / stfile
70-
shutil.copy(str(stfile_local), str(stfile_conf))
74+
with imlr.path(_styles, stfile) as stfile_local:
75+
shutil.copy(str(stfile_local), str(stfile_conf))
7176

7277

7378
def load_mplstyle() -> None:
@@ -76,16 +81,18 @@ def load_mplstyle() -> None:
7681
if conf.plot.mplstyle:
7782
for style in conf.plot.mplstyle.split():
7883
found = False
84+
style_fname = style + ".mplstyle"
7985
if not ISOLATED:
80-
stfile = config.CONFIG_DIR / (style + '.mplstyle')
86+
stfile = config.CONFIG_DIR / style_fname
8187
if stfile.is_file():
8288
found = True
8389
style = str(stfile)
8490
if not found:
8591
# try local version
86-
stfile = pathlib.Path(__file__).parent / (style + '.mplstyle')
87-
if stfile.is_file():
88-
style = str(stfile)
92+
if imlr.is_resource(_styles, style_fname):
93+
with imlr.path(_styles, style_fname) as stfile:
94+
mpls.use(str(stfile))
95+
continue
8996
try:
9097
mpls.use(style)
9198
except OSError:

stagpy/_styles/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)