1717"""
1818
1919from __future__ import annotations
20+ import importlib .resources as imlr
2021import os
21- import pathlib
2222import shutil
2323import signal
2424import sys
2727from setuptools_scm import get_version
2828from loam .manager import ConfigurationManager
2929
30- from . import config
30+ from . import config , _styles
3131
3232if typing .TYPE_CHECKING :
33- from typing import NoReturn , Any
33+ from typing import NoReturn , Any , Iterator
3434
3535
3636def _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+
5662def _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
7378def 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 :
0 commit comments