120
120
import tempfile
121
121
import warnings
122
122
123
+ try :
124
+ from functools import lru_cache
125
+ except ImportError :
126
+ from backports .functools_lru_cache import lru_cache
127
+
123
128
# cbook must import matplotlib only within function
124
129
# definitions, so it is safe to import from it here.
125
130
from . import cbook
@@ -385,31 +390,6 @@ def ge(self, level):
385
390
return self .vald [self .level ] >= self .vald [level ]
386
391
387
392
388
- def _wrap (fmt , func , level = 'INFO' , always = True ):
389
- """
390
- return a callable function that wraps func and reports its
391
- output through logger
392
-
393
- if always is True, the report will occur on every function
394
- call; otherwise only on the first time the function is called
395
- """
396
- assert callable (func )
397
-
398
- def wrapper (* args , ** kwargs ):
399
- ret = func (* args , ** kwargs )
400
-
401
- if (always or not wrapper ._spoke ):
402
- lvl = logging .getLevelName (level .upper ())
403
- _log .log (lvl , fmt % ret )
404
- spoke = True
405
- if not wrapper ._spoke :
406
- wrapper ._spoke = spoke
407
- return ret
408
- wrapper ._spoke = False
409
- wrapper .__doc__ = func .__doc__
410
- return wrapper
411
-
412
-
413
393
def checkdep_dvipng ():
414
394
try :
415
395
s = subprocess .Popen ([str ('dvipng' ), '-version' ],
@@ -578,7 +558,27 @@ def checkdep_usetex(s):
578
558
return flag
579
559
580
560
581
- def _get_home ():
561
+ def _log_and_cache_result (fmt , level = "INFO" , func = None ):
562
+ """Decorator that logs & caches the result of a function with no arguments.
563
+
564
+ The first time the decorated *func* is called, its result is logged at
565
+ level *level* using log format *fmt*, and cached. Later calls immediately
566
+ return the cached result without logging.
567
+ """
568
+ if func is None :
569
+ return functools .partial (_log_and_cache_result , fmt , level )
570
+
571
+ @lru_cache (1 ) # Make calls after the 1st return the cached result.
572
+ @functools .wraps (func )
573
+ def wrapper ():
574
+ retval = func ()
575
+ _log .log (logging .getLevelName (level .upper ()), fmt , retval )
576
+ return retval
577
+ return wrapper
578
+
579
+
580
+ @_log_and_cache_result ("HOME=%s" )
581
+ def get_home ():
582
582
"""Find user's home directory if possible.
583
583
Otherwise, returns None.
584
584
@@ -608,9 +608,6 @@ def _create_tmp_config_dir():
608
608
return configdir
609
609
610
610
611
- get_home = _wrap ('$HOME=%s' , _get_home , always = False )
612
-
613
-
614
611
def _get_xdg_config_dir ():
615
612
"""
616
613
Returns the XDG configuration directory, according to the `XDG
@@ -676,7 +673,8 @@ def _get_config_or_cache_dir(xdg_base):
676
673
return _create_tmp_config_dir ()
677
674
678
675
679
- def _get_configdir ():
676
+ @_log_and_cache_result ("CONFIGDIR=%s" )
677
+ def get_configdir ():
680
678
"""
681
679
Return the string representing the configuration directory.
682
680
@@ -697,10 +695,9 @@ def _get_configdir():
697
695
"""
698
696
return _get_config_or_cache_dir (_get_xdg_config_dir ())
699
697
700
- get_configdir = _wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
701
-
702
698
703
- def _get_cachedir ():
699
+ @_log_and_cache_result ("CACHEDIR=%s" )
700
+ def get_cachedir ():
704
701
"""
705
702
Return the location of the cache directory.
706
703
@@ -709,8 +706,6 @@ def _get_cachedir():
709
706
"""
710
707
return _get_config_or_cache_dir (_get_xdg_cache_dir ())
711
708
712
- get_cachedir = _wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
713
-
714
709
715
710
def _decode_filesystem_path (path ):
716
711
if isinstance (path , bytes ):
@@ -762,14 +757,12 @@ def _get_data_path():
762
757
raise RuntimeError ('Could not find the matplotlib data files' )
763
758
764
759
765
- def _get_data_path_cached ():
760
+ @_log_and_cache_result ('rcParams["datapath"]=%s' )
761
+ def get_data_path ():
766
762
if defaultParams ['datapath' ][0 ] is None :
767
763
defaultParams ['datapath' ][0 ] = _get_data_path ()
768
764
return defaultParams ['datapath' ][0 ]
769
765
770
- get_data_path = _wrap ('matplotlib data path %s' , _get_data_path_cached ,
771
- always = False )
772
-
773
766
774
767
def get_py2exe_datafiles ():
775
768
datapath = get_data_path ()
@@ -826,7 +819,7 @@ def gen_candidates():
826
819
else :
827
820
yield matplotlibrc
828
821
yield os .path .join (matplotlibrc , 'matplotlibrc' )
829
- yield os .path .join (_get_configdir (), 'matplotlibrc' )
822
+ yield os .path .join (get_configdir (), 'matplotlibrc' )
830
823
yield os .path .join (get_data_path (), 'matplotlibrc' )
831
824
832
825
for fname in gen_candidates ():
0 commit comments