@@ -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
561555def _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
651643def _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
710700def 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 ():
0 commit comments