Skip to content

Privatize TexManager.texcache #26056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/deprecations/26056-OG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
``TexManager.texcache``
~~~~~~~~~~~~~~~~~~~~~~~

... is considered private and deprecated. The location of the cache directory is
clarified in the doc-string.
2 changes: 1 addition & 1 deletion lib/matplotlib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def get_cachedir():
Return the string path of the cache directory.

The procedure used to find the directory is the same as for
_get_config_dir, except using ``$XDG_CACHE_HOME``/``$HOME/.cache`` instead.
`get_configdir`, except using ``$XDG_CACHE_HOME``/``$HOME/.cache`` instead.
"""
return _get_config_or_cache_dir(_get_xdg_cache_dir)

Expand Down
16 changes: 10 additions & 6 deletions lib/matplotlib/texmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import numpy as np

import matplotlib as mpl
from matplotlib import cbook, dviread
from matplotlib import _api, cbook, dviread

_log = logging.getLogger(__name__)

Expand All @@ -57,10 +57,14 @@ class TexManager:
"""
Convert strings to dvi files using TeX, caching the results to a directory.

The cache directory is called ``tex.cache`` and is located in the directory
returned by `.get_cachedir`.

Repeated calls to this constructor always return the same instance.
"""

texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
texcache = _api.deprecate_privatize_attribute("3.8")
_texcache = os.path.join(mpl.get_cachedir(), 'tex.cache')
_grey_arrayd = {}

_font_families = ('serif', 'sans-serif', 'cursive', 'monospace')
Expand Down Expand Up @@ -102,7 +106,7 @@ class TexManager:

@functools.lru_cache # Always return the same instance.
def __new__(cls):
Path(cls.texcache).mkdir(parents=True, exist_ok=True)
Path(cls._texcache).mkdir(parents=True, exist_ok=True)
return object.__new__(cls)

@classmethod
Expand Down Expand Up @@ -168,7 +172,7 @@ def get_basefile(cls, tex, fontsize, dpi=None):
"""
src = cls._get_tex_source(tex, fontsize) + str(dpi)
filehash = hashlib.md5(src.encode('utf-8')).hexdigest()
filepath = Path(cls.texcache)
filepath = Path(cls._texcache)

num_letters, num_levels = 2, 2
for i in range(0, num_letters*num_levels, num_letters):
Expand Down Expand Up @@ -244,7 +248,7 @@ def _run_checked_subprocess(cls, command, tex, *, cwd=None):
_log.debug(cbook._pformat_subprocess(command))
try:
report = subprocess.check_output(
command, cwd=cwd if cwd is not None else cls.texcache,
command, cwd=cwd if cwd is not None else cls._texcache,
stderr=subprocess.STDOUT)
except FileNotFoundError as exc:
raise RuntimeError(
Expand Down Expand Up @@ -330,7 +334,7 @@ def get_grey(cls, tex, fontsize=None, dpi=None):
alpha = cls._grey_arrayd.get(key)
if alpha is None:
pngfile = cls.make_png(tex, fontsize, dpi)
rgba = mpl.image.imread(os.path.join(cls.texcache, pngfile))
rgba = mpl.image.imread(os.path.join(cls._texcache, pngfile))
cls._grey_arrayd[key] = alpha = rgba[:, :, -1]
return alpha

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy