Skip to content

Commit 6778e4e

Browse files
committed
Simplify caching of directories retrieval.
1 parent d0dea40 commit 6778e4e

File tree

3 files changed

+31
-45
lines changed

3 files changed

+31
-45
lines changed

lib/matplotlib/__init__.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -386,27 +386,22 @@ def ge(self, level):
386386
verbose = Verbose()
387387

388388

389-
def _wrap(fmt, func, level=logging.DEBUG, always=True):
390-
"""
391-
return a callable function that wraps func and reports its
392-
output through logger
393-
394-
if always is True, the report will occur on every function
395-
call; otherwise only on the first time the function is called
396-
"""
397-
assert callable(func)
398-
399-
def wrapper(*args, **kwargs):
400-
ret = func(*args, **kwargs)
401-
402-
if (always or not wrapper._spoke):
403-
_log.log(level, fmt % ret)
404-
spoke = True
405-
if not wrapper._spoke:
406-
wrapper._spoke = spoke
389+
def _logged_cached(fmt, func=None):
390+
if func is None:
391+
return functools.partial(_logged_cached, fmt)
392+
393+
called = False
394+
ret = None
395+
396+
@functools.wraps(func)
397+
def wrapper():
398+
nonlocal called, ret
399+
if not called:
400+
ret = func()
401+
called = True
402+
_log.debug(fmt, ret)
407403
return ret
408-
wrapper._spoke = False
409-
wrapper.__doc__ = func.__doc__
404+
410405
return wrapper
411406

412407

@@ -544,7 +539,8 @@ def checkdep_usetex(s):
544539
return flag
545540

546541

547-
def _get_home():
542+
@_logged_cached('$HOME=%s')
543+
def get_home():
548544
"""
549545
Return the user's home directory.
550546
@@ -555,8 +551,6 @@ def _get_home():
555551
except Exception:
556552
return None
557553

558-
get_home = _wrap('$HOME=%s', _get_home, always=False)
559-
560554

561555
def _create_tmp_config_dir():
562556
"""
@@ -615,7 +609,8 @@ def _get_config_or_cache_dir(xdg_base):
615609
return _create_tmp_config_dir()
616610

617611

618-
def _get_configdir():
612+
@_logged_cached('CONFIGDIR=%s')
613+
def get_configdir():
619614
"""
620615
Return the string representing the configuration directory.
621616
@@ -633,10 +628,9 @@ def _get_configdir():
633628
"""
634629
return _get_config_or_cache_dir(_get_xdg_config_dir())
635630

636-
get_configdir = _wrap('CONFIGDIR=%s', _get_configdir, always=False)
637-
638631

639-
def _get_cachedir():
632+
@_logged_cached('CACHEDIR=%s')
633+
def get_cachedir():
640634
"""
641635
Return the location of the cache directory.
642636
@@ -645,8 +639,6 @@ def _get_cachedir():
645639
"""
646640
return _get_config_or_cache_dir(_get_xdg_cache_dir())
647641

648-
get_cachedir = _wrap('CACHEDIR=%s', _get_cachedir, always=False)
649-
650642

651643
def _decode_filesystem_path(path):
652644
if not isinstance(path, str):
@@ -698,14 +690,12 @@ def _get_data_path():
698690
raise RuntimeError('Could not find the matplotlib data files')
699691

700692

701-
def _get_data_path_cached():
693+
@_logged_cached('matplotlib data path: %s')
694+
def get_data_path():
702695
if defaultParams['datapath'][0] is None:
703696
defaultParams['datapath'][0] = _get_data_path()
704697
return defaultParams['datapath'][0]
705698

706-
get_data_path = _wrap('matplotlib data path %s', _get_data_path_cached,
707-
always=False)
708-
709699

710700
def get_py2exe_datafiles():
711701
data_path = Path(get_data_path())
@@ -756,7 +746,7 @@ def gen_candidates():
756746
else:
757747
yield matplotlibrc
758748
yield os.path.join(matplotlibrc, 'matplotlibrc')
759-
yield os.path.join(_get_configdir(), 'matplotlibrc')
749+
yield os.path.join(get_configdir(), 'matplotlibrc')
760750
yield os.path.join(get_data_path(), 'matplotlibrc')
761751

762752
for fname in gen_candidates():

lib/matplotlib/style/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
BASE_LIBRARY_PATH = os.path.join(mpl.get_data_path(), 'stylelib')
2727
# Users may want multiple library paths, so store a list of paths.
28-
USER_LIBRARY_PATHS = [os.path.join(mpl._get_configdir(), 'stylelib')]
28+
USER_LIBRARY_PATHS = [os.path.join(mpl.get_configdir(), 'stylelib')]
2929
STYLE_EXTENSION = 'mplstyle'
3030
STYLE_FILE_PATTERN = re.compile(r'([\S]+).%s$' % STYLE_EXTENSION)
3131

lib/matplotlib/testing/compare.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
import matplotlib
2020
from matplotlib.testing.exceptions import ImageComparisonFailure
21-
from matplotlib import _png
22-
from matplotlib import _get_cachedir
21+
from matplotlib import _png, cbook
2322
from matplotlib import cbook
2423

2524
__all__ = ['compare_float', 'compare_images', 'comparable_formats']
@@ -79,7 +78,7 @@ def compare_float(expected, actual, relTol=None, absTol=None):
7978

8079

8180
def get_cache_dir():
82-
cachedir = _get_cachedir()
81+
cachedir = matplotlib.get_cachedir()
8382
if cachedir is None:
8483
raise RuntimeError('Could not find a suitable configuration directory')
8584
cache_dir = os.path.join(cachedir, 'test_cache')
@@ -293,15 +292,12 @@ def comparable_formats():
293292

294293
def convert(filename, cache):
295294
"""
296-
Convert the named file into a png file. Returns the name of the
297-
created file.
295+
Convert the named file to png; return the name of the created file.
298296
299297
If *cache* is True, the result of the conversion is cached in
300-
`matplotlib._get_cachedir() + '/test_cache/'`. The caching is based
301-
on a hash of the exact contents of the input file. The is no limit
302-
on the size of the cache, so it may need to be manually cleared
303-
periodically.
304-
298+
`matplotlib.get_cachedir() + '/test_cache/'`. The caching is based on a
299+
hash of the exact contents of the input file. There is no limit on the
300+
size of the cache, so it may need to be manually cleared periodically.
305301
"""
306302
base, extension = filename.rsplit('.', 1)
307303
if extension not in converter:

0 commit comments

Comments
 (0)