|
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,28 @@ 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.style_name"; in |
| 100 | + that case, "package" should be an importable Python package name, |
| 101 | + e.g. at ``/path/to/package/__init__.py``; the loaded style file is |
| 102 | + ``/path/to/package/style_name.mplstyle``. (Style files in |
| 103 | + subpackages are likewise supported.) |
| 104 | +
|
| 105 | + This can also be the path or URL to a style file, which gets loaded |
| 106 | + by `.rc_params_from_file`. |
| 107 | +
|
| 108 | + - If a dict, this is a mapping of key/value pairs for `.rcParams`. |
| 109 | +
|
| 110 | + - If a Path, this is the path to a style file, which gets loaded by |
| 111 | + `.rc_params_from_file`. |
| 112 | +
|
| 113 | + - If a list, this is a list of style specifiers (str, Path or dict), |
| 114 | + which get applied from first to last in the list. |
99 | 115 |
|
100 | 116 | Notes |
101 | 117 | ----- |
@@ -127,14 +143,28 @@ def use(style): |
127 | 143 | if k not in STYLE_BLACKLIST} |
128 | 144 | elif style in library: |
129 | 145 | style = library[style] |
| 146 | + elif "." in style: |
| 147 | + pkg, _, name = style.rpartition(".") |
| 148 | + try: |
| 149 | + path = (importlib_resources.files(pkg) |
| 150 | + / f"{name}.{STYLE_EXTENSION}") |
| 151 | + style = _rc_params_in_file(path) |
| 152 | + except (ModuleNotFoundError, IOError) as exc: |
| 153 | + # There is an ambiguity whether a dotted name refers to a |
| 154 | + # package.style_name or to a dotted file path. Currently, |
| 155 | + # we silently try the first form and then the second one; |
| 156 | + # in the future, we may consider forcing file paths to |
| 157 | + # either use Path objects or be prepended with "./" and use |
| 158 | + # the slash as marker for file paths. |
| 159 | + pass |
130 | 160 | if isinstance(style, (str, Path)): |
131 | 161 | try: |
132 | 162 | style = _rc_params_in_file(style) |
133 | 163 | except IOError as err: |
134 | 164 | 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 |
| 165 | + f"{style!r} is not a valid package style, path of style " |
| 166 | + f"file, URL of style file, or library style name (library " |
| 167 | + f"styles are listed in `style.available`)") from err |
138 | 168 | filtered = {} |
139 | 169 | for k in style: # don't trigger RcParams.__getitem__('backend') |
140 | 170 | if k in STYLE_BLACKLIST: |
|
0 commit comments