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