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