@@ -396,31 +396,6 @@ def ge(self, level):
396
396
return self .vald [self .level ] >= self .vald [level ]
397
397
398
398
399
- def _wrap (fmt , func , level = 'DEBUG' , always = True ):
400
- """
401
- return a callable function that wraps func and reports its
402
- output through logger
403
-
404
- if always is True, the report will occur on every function
405
- call; otherwise only on the first time the function is called
406
- """
407
- assert callable (func )
408
-
409
- def wrapper (* args , ** kwargs ):
410
- ret = func (* args , ** kwargs )
411
-
412
- if (always or not wrapper ._spoke ):
413
- lvl = logging .getLevelName (level .upper ())
414
- _log .log (lvl , fmt % ret )
415
- spoke = True
416
- if not wrapper ._spoke :
417
- wrapper ._spoke = spoke
418
- return ret
419
- wrapper ._spoke = False
420
- wrapper .__doc__ = func .__doc__
421
- return wrapper
422
-
423
-
424
399
def checkdep_dvipng ():
425
400
try :
426
401
s = subprocess .Popen ([str ('dvipng' ), '-version' ],
@@ -589,7 +564,27 @@ def checkdep_usetex(s):
589
564
return flag
590
565
591
566
592
- def _get_home ():
567
+ def _log_and_cache_result (fmt , level = "DEBUG" , func = None ):
568
+ """Decorator that logs & caches the result of a function with no arguments.
569
+
570
+ The first time the decorated *func* is called, its result is logged at
571
+ level *level* using log format *fmt*, and cached. Later calls immediately
572
+ return the cached result without logging.
573
+ """
574
+ if func is None :
575
+ return functools .partial (_log_and_cache_result , fmt , level )
576
+
577
+ @functools .lru_cache (1 ) # Calls after the 1st return the cached result.
578
+ @functools .wraps (func )
579
+ def wrapper ():
580
+ retval = func ()
581
+ _log .log (logging .getLevelName (level .upper ()), fmt , retval )
582
+ return retval
583
+ return wrapper
584
+
585
+
586
+ @_log_and_cache_result ("HOME=%s" )
587
+ def get_home ():
593
588
"""Find user's home directory if possible.
594
589
Otherwise, returns None.
595
590
@@ -620,9 +615,6 @@ def _create_tmp_config_dir():
620
615
return configdir
621
616
622
617
623
- get_home = _wrap ('$HOME=%s' , _get_home , always = False )
624
-
625
-
626
618
def _get_xdg_config_dir ():
627
619
"""
628
620
Returns the XDG configuration directory, according to the `XDG
@@ -684,7 +676,8 @@ def _get_config_or_cache_dir(xdg_base):
684
676
return _create_tmp_config_dir ()
685
677
686
678
687
- def _get_configdir ():
679
+ @_log_and_cache_result ("CONFIGDIR=%s" )
680
+ def get_configdir ():
688
681
"""
689
682
Return the string representing the configuration directory.
690
683
@@ -705,10 +698,9 @@ def _get_configdir():
705
698
"""
706
699
return _get_config_or_cache_dir (_get_xdg_config_dir ())
707
700
708
- get_configdir = _wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
709
-
710
701
711
- def _get_cachedir ():
702
+ @_log_and_cache_result ("CACHEDIR=%s" )
703
+ def get_cachedir ():
712
704
"""
713
705
Return the location of the cache directory.
714
706
@@ -717,8 +709,6 @@ def _get_cachedir():
717
709
"""
718
710
return _get_config_or_cache_dir (_get_xdg_cache_dir ())
719
711
720
- get_cachedir = _wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
721
-
722
712
723
713
def _decode_filesystem_path (path ):
724
714
if not isinstance (path , str ):
@@ -770,14 +760,12 @@ def _get_data_path():
770
760
raise RuntimeError ('Could not find the matplotlib data files' )
771
761
772
762
773
- def _get_data_path_cached ():
763
+ @_log_and_cache_result ('rcParams["datapath"]=%s' )
764
+ def get_data_path ():
774
765
if defaultParams ['datapath' ][0 ] is None :
775
766
defaultParams ['datapath' ][0 ] = _get_data_path ()
776
767
return defaultParams ['datapath' ][0 ]
777
768
778
- get_data_path = _wrap ('matplotlib data path %s' , _get_data_path_cached ,
779
- always = False )
780
-
781
769
782
770
def get_py2exe_datafiles ():
783
771
datapath = get_data_path ()
@@ -835,7 +823,7 @@ def gen_candidates():
835
823
else :
836
824
yield matplotlibrc
837
825
yield os .path .join (matplotlibrc , 'matplotlibrc' )
838
- yield os .path .join (_get_configdir (), 'matplotlibrc' )
826
+ yield os .path .join (get_configdir (), 'matplotlibrc' )
839
827
yield os .path .join (get_data_path (), 'matplotlibrc' )
840
828
841
829
for fname in gen_candidates ():
0 commit comments