|
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 |
|
@@ -63,20 +71,27 @@ def use(style):
|
63 | 71 | Parameters
|
64 | 72 | ----------
|
65 | 73 | style : str, dict, Path or list
|
66 |
| - A style specification. Valid options are: |
67 | 74 |
|
68 |
| - +------+-------------------------------------------------------------+ |
69 |
| - | str | The name of a style or a path/URL to a style file. For a | |
70 |
| - | | list of available style names, see `.style.available`. | |
71 |
| - +------+-------------------------------------------------------------+ |
72 |
| - | dict | Dictionary with valid key/value pairs for | |
73 |
| - | | `matplotlib.rcParams`. | |
74 |
| - +------+-------------------------------------------------------------+ |
75 |
| - | Path | A path-like object which is a path to a style file. | |
76 |
| - +------+-------------------------------------------------------------+ |
77 |
| - | list | A list of style specifiers (str, Path or dict) applied from | |
78 |
| - | | first to last in the list. | |
79 |
| - +------+-------------------------------------------------------------+ |
| 75 | + A style specification. |
| 76 | +
|
| 77 | + - If a str, this can be one of the style names in `.style.available` |
| 78 | + (a builtin style or a style installed in the user library path). |
| 79 | +
|
| 80 | + This can also be a dotted name of the form "package.name.style_name"; |
| 81 | + in that case, "package.name" should be an importable Python package |
| 82 | + name, e.g. at ``/path/to/package/name/__init__.py``; the loaded style |
| 83 | + file is ``/path/to/package/name/style_name.mplstyle``. |
| 84 | +
|
| 85 | + This can also be the path or URL to a style file, which gets loaded |
| 86 | + by `.rc_params_from_file`. |
| 87 | +
|
| 88 | + - If a dict, this is a mapping of key/value pairs for `.rcParams`. |
| 89 | +
|
| 90 | + - If a Path, this is the path to a style file, which gets loaded by |
| 91 | + `.rc_params_from_file`. |
| 92 | +
|
| 93 | + - If a list, this is a a list of style specifiers (str, Path or dict), |
| 94 | + which get applied from first to last in the list. |
80 | 95 |
|
81 | 96 | Notes
|
82 | 97 | -----
|
@@ -130,6 +145,20 @@ def use(style):
|
130 | 145 | if k not in STYLE_BLACKLIST}
|
131 | 146 | elif style in library:
|
132 | 147 | style = library[style]
|
| 148 | + elif "." in style: |
| 149 | + pkg, name = style.rsplit(".") |
| 150 | + try: |
| 151 | + path = (importlib_resources.files(pkg) |
| 152 | + / f"{name}.{STYLE_EXTENSION}") |
| 153 | + style = _rc_params_in_file(path) |
| 154 | + except (ModuleNotFoundError, IOError) as exc: |
| 155 | + # There is an ambiguity whether a dotted name refers to a |
| 156 | + # package.style_name or to a dotted file path. Currently, |
| 157 | + # we silently try the first form and then the second one; |
| 158 | + # in the future, we may consider forcing file paths to |
| 159 | + # either use Path objects or be prepended with "./" and use |
| 160 | + # the slash as marker for file paths. |
| 161 | + pass |
133 | 162 | if isinstance(style, (str, Path)):
|
134 | 163 | try:
|
135 | 164 | style = _rc_params_in_file(style)
|
|
0 commit comments