Convert the named file to png; return the name of the created file. If *cache* is True, the result of the conversion is cached in `matplotlib.get_cachedir() + '/test_cache/'`. The caching is based on a hash of the exact contents of the input file. Old cache entries are automa
(filename, cache)
| 280 | |
| 281 | |
| 282 | def convert(filename, cache): |
| 283 | """ |
| 284 | Convert the named file to png; return the name of the created file. |
| 285 | |
| 286 | If *cache* is True, the result of the conversion is cached in |
| 287 | `matplotlib.get_cachedir() + '/test_cache/'`. The caching is based on a |
| 288 | hash of the exact contents of the input file. Old cache entries are |
| 289 | automatically deleted as needed to keep the size of the cache capped to |
| 290 | twice the size of all baseline images. |
| 291 | """ |
| 292 | path = Path(filename) |
| 293 | if not path.exists(): |
| 294 | raise OSError(f"{path} does not exist") |
| 295 | if path.suffix[1:] not in converter: |
| 296 | import pytest |
| 297 | pytest.skip(f"Don't know how to convert {path.suffix} files to png") |
| 298 | newpath = path.parent / f"{path.stem}_{path.suffix[1:]}.png" |
| 299 | |
| 300 | # Only convert the file if the destination doesn't already exist or |
| 301 | # is out of date. |
| 302 | if not newpath.exists() or newpath.stat().st_mtime < path.stat().st_mtime: |
| 303 | cache_dir = _get_cache_path() if cache else None |
| 304 | |
| 305 | if cache_dir is not None: |
| 306 | _register_conversion_cache_cleaner_once() |
| 307 | hash_value = get_file_hash(path) |
| 308 | cached_path = cache_dir / (hash_value + newpath.suffix) |
| 309 | if cached_path.exists(): |
| 310 | _log.debug("For %s: reusing cached conversion.", filename) |
| 311 | shutil.copyfile(cached_path, newpath) |
| 312 | return str(newpath) |
| 313 | |
| 314 | _log.debug("For %s: converting to png.", filename) |
| 315 | convert = converter[path.suffix[1:]] |
| 316 | if path.suffix == ".svg": |
| 317 | contents = path.read_text(encoding="utf-8") |
| 318 | # NOTE: This check should be kept in sync with font styling in |
| 319 | # `lib/matplotlib/backends/backend_svg.py`. If it changes, then be sure to |
| 320 | # re-generate any SVG test files using this mode, or else such tests will |
| 321 | # fail to use the converter for the expected images (but will for the |
| 322 | # results), and the tests will fail strangely. |
| 323 | if re.search( |
| 324 | # searches for attributes : |
| 325 | # style=[font|font-size|font-weight| |
| 326 | # font-family|font-variant|font-style] |
| 327 | # taking care of the possibility of multiple style attributes |
| 328 | # before the font styling (i.e. opacity) |
| 329 | r'style="[^"]*font(|-size|-weight|-family|-variant|-style):', |
| 330 | contents # raw contents of the svg file |
| 331 | ): |
| 332 | # for svg.fonttype = none, we explicitly patch the font search |
| 333 | # path so that fonts shipped by Matplotlib are found. |
| 334 | convert = _svg_with_matplotlib_fonts_converter |
| 335 | convert(path, newpath) |
| 336 | |
| 337 | if cache_dir is not None: |
| 338 | _log.debug("For %s: caching conversion result.", filename) |
| 339 | shutil.copyfile(newpath, cached_path) |
no test coverage detected
searching dependent graphs…