|
15 | 15 | importlogging |
16 | 16 | importos |
17 | 17 | frompathlibimportPath |
| 18 | +importsys |
18 | 19 | importwarnings |
19 | 20 |
|
| 21 | +ifsys.version_info>= (3,10): |
| 22 | +importimportlib.resourcesasimportlib_resources |
| 23 | +else: |
| 24 | +# Even though Py3.9 has importlib.resources, it doesn't properly handle |
| 25 | +# modules added in sys.path. |
| 26 | +importimportlib_resources |
| 27 | + |
20 | 28 | importmatplotlibasmpl |
21 | 29 | frommatplotlibimport_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 | ifknotinSTYLE_BLACKLIST} |
128 | 144 | elifstyleinlibrary: |
129 | 145 | style=library[style] |
| 146 | +elif"."instyle: |
| 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)asexc: |
| 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 | ifisinstance(style, (str,Path)): |
131 | 161 | try: |
132 | 162 | style=_rc_params_in_file(style) |
133 | 163 | exceptIOErroraserr: |
134 | 164 | raiseIOError( |
135 | | -f"{style!r} notfound in the style library and input is " |
136 | | -f"not a valid URLorpath; see `style.available` for the " |
137 | | -f"list of available styles")fromerr |
| 165 | +f"{style!r}isnota valid package style, path of style " |
| 166 | +f"file, URL of style file,orlibrarystyle name (library " |
| 167 | +f"styles are listed in `style.available`)")fromerr |
138 | 168 | filtered= {} |
139 | 169 | forkinstyle:# don't trigger RcParams.__getitem__('backend') |
140 | 170 | ifkinSTYLE_BLACKLIST: |
|