|
15 | 15 | import logging
|
16 | 16 | import os
|
17 | 17 | from pathlib import Path
|
| 18 | +import sys |
18 | 19 | import warnings
|
19 | 20 |
|
| 21 | +if sys.version_info >= (3, 10): |
| 22 | + import importlib.resources as importlib_resources |
| 23 | +else: |
| 24 | + # Even though Py3.9 has importlib.resources, it doesn't properly handle |
| 25 | + # modules added in sys.path. |
| 26 | + import importlib_resources |
| 27 | + |
20 | 28 | import matplotlib as mpl
|
21 | 29 | from matplotlib import _api, _docstring, _rc_params_in_file, rcParamsDefault
|
22 | 30 |
|
@@ -82,20 +90,27 @@ def use(style):
|
82 | 90 | Parameters
|
83 | 91 | ----------
|
84 | 92 | style : str, dict, Path or list
|
85 |
| - A style specification. Valid options are: |
86 | 93 |
|
87 |
| - +------+-------------------------------------------------------------+ |
88 |
| - | str | The name of a style or a path/URL to a style file. For a | |
89 |
| - | | list of available style names, see `.style.available`. | |
90 |
| - +------+-------------------------------------------------------------+ |
91 |
| - | dict | Dictionary with valid key/value pairs for | |
92 |
| - | | `matplotlib.rcParams`. | |
93 |
| - +------+-------------------------------------------------------------+ |
94 |
| - | Path | A path-like object which is a path to a style file. | |
95 |
| - +------+-------------------------------------------------------------+ |
96 |
| - | list | A list of style specifiers (str, Path or dict) applied from | |
97 |
| - | | first to last in the list. | |
98 |
| - +------+-------------------------------------------------------------+ |
| 94 | + A style specification. |
| 95 | +
|
| 96 | + - If a str, this can be one of the style names in `.style.available` |
| 97 | + (a builtin style or a style installed in the user library path). |
| 98 | +
|
| 99 | + This can also be a dotted name of the form "package.name.style_name"; |
| 100 | + in that case, "package.name" should be an importable Python package |
| 101 | + name, e.g. at ``/path/to/package/name/__init__.py``; the loaded style |
| 102 | + file is ``/path/to/package/name/style_name.mplstyle``. |
| 103 | +
|
| 104 | + This can also be the path or URL to a style file, which gets loaded |
| 105 | + by `.rc_params_from_file`. |
| 106 | +
|
| 107 | + - If a dict, this is a mapping of key/value pairs for `.rcParams`. |
| 108 | +
|
| 109 | + - If a Path, this is the path to a style file, which gets loaded by |
| 110 | + `.rc_params_from_file`. |
| 111 | +
|
| 112 | + - If a list, this is a list of style specifiers (str, Path or dict), |
| 113 | + which get applied from first to last in the list. |
99 | 114 |
|
100 | 115 | Notes
|
101 | 116 | -----
|
@@ -127,14 +142,28 @@ def use(style):
|
127 | 142 | if k not in STYLE_BLACKLIST}
|
128 | 143 | elif style in library:
|
129 | 144 | style = library[style]
|
| 145 | + elif "." in style: |
| 146 | + pkg, _, name = style.rpartition(".") |
| 147 | + try: |
| 148 | + path = (importlib_resources.files(pkg) |
| 149 | + / f"{name}.{STYLE_EXTENSION}") |
| 150 | + style = _rc_params_in_file(path) |
| 151 | + except (ModuleNotFoundError, IOError) as exc: |
| 152 | + # There is an ambiguity whether a dotted name refers to a |
| 153 | + # package.style_name or to a dotted file path. Currently, |
| 154 | + # we silently try the first form and then the second one; |
| 155 | + # in the future, we may consider forcing file paths to |
| 156 | + # either use Path objects or be prepended with "./" and use |
| 157 | + # the slash as marker for file paths. |
| 158 | + pass |
130 | 159 | if isinstance(style, (str, Path)):
|
131 | 160 | try:
|
132 | 161 | style = _rc_params_in_file(style)
|
133 | 162 | except IOError as err:
|
134 | 163 | raise IOError(
|
135 |
| - f"{style!r} not found in the style library and input is " |
136 |
| - f"not a valid URL or path; see `style.available` for the " |
137 |
| - f"list of available styles") from err |
| 164 | + f"{style!r} is not a valid package style, path of style " |
| 165 | + f"file, URL of style file, or library style name (library " |
| 166 | + f"styles are listed in `style.avaible`)") from err |
138 | 167 | filtered = {}
|
139 | 168 | for k in style: # don't trigger RcParams.__getitem__('backend')
|
140 | 169 | if k in STYLE_BLACKLIST:
|
|
0 commit comments