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,21 @@ def checkdep_usetex(s):
578
558
return flag
579
559
580
560
581
- def _get_home ():
561
+ def _log_result (fmt , level = "INFO" , func = None ):
562
+ if func is None :
563
+ return functools .partial (_log_result , fmt , level )
564
+
565
+ @functools .wraps (func )
566
+ def wrapper (* args , ** kwargs ):
567
+ retval = func (* args , ** kwargs )
568
+ _log .log (logging .getLevelName (level .upper ()), fmt , retval )
569
+ return retval
570
+ return wrapper
571
+
572
+
573
+ @lru_cache (1 )
574
+ @_log_result ("HOME=%s" )
575
+ def get_home ():
582
576
"""Find user's home directory if possible.
583
577
Otherwise, returns None.
584
578
@@ -608,9 +602,6 @@ def _create_tmp_config_dir():
608
602
return configdir
609
603
610
604
611
- get_home = _wrap ('$HOME=%s' , _get_home , always = False )
612
-
613
-
614
605
def _get_xdg_config_dir ():
615
606
"""
616
607
Returns the XDG configuration directory, according to the `XDG
@@ -676,7 +667,9 @@ def _get_config_or_cache_dir(xdg_base):
676
667
return _create_tmp_config_dir ()
677
668
678
669
679
- def _get_configdir ():
670
+ @lru_cache (1 )
671
+ @_log_result ("CONFIGDIR=%s" )
672
+ def get_configdir ():
680
673
"""
681
674
Return the string representing the configuration directory.
682
675
@@ -697,10 +690,10 @@ def _get_configdir():
697
690
"""
698
691
return _get_config_or_cache_dir (_get_xdg_config_dir ())
699
692
700
- get_configdir = _wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
701
-
702
693
703
- def _get_cachedir ():
694
+ @lru_cache (1 )
695
+ @_log_result ("CACHEDIR=%s" )
696
+ def get_cachedir ():
704
697
"""
705
698
Return the location of the cache directory.
706
699
@@ -709,8 +702,6 @@ def _get_cachedir():
709
702
"""
710
703
return _get_config_or_cache_dir (_get_xdg_cache_dir ())
711
704
712
- get_cachedir = _wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
713
-
714
705
715
706
def _decode_filesystem_path (path ):
716
707
if isinstance (path , bytes ):
@@ -762,14 +753,13 @@ def _get_data_path():
762
753
raise RuntimeError ('Could not find the matplotlib data files' )
763
754
764
755
765
- def _get_data_path_cached ():
756
+ @lru_cache (1 )
757
+ @_log_result ('rcParams["datapath"]=%s' )
758
+ def get_data_path ():
766
759
if defaultParams ['datapath' ][0 ] is None :
767
760
defaultParams ['datapath' ][0 ] = _get_data_path ()
768
761
return defaultParams ['datapath' ][0 ]
769
762
770
- get_data_path = _wrap ('matplotlib data path %s' , _get_data_path_cached ,
771
- always = False )
772
-
773
763
774
764
def get_py2exe_datafiles ():
775
765
datapath = get_data_path ()
@@ -826,7 +816,7 @@ def gen_candidates():
826
816
else :
827
817
yield matplotlibrc
828
818
yield os .path .join (matplotlibrc , 'matplotlibrc' )
829
- yield os .path .join (_get_configdir (), 'matplotlibrc' )
819
+ yield os .path .join (get_configdir (), 'matplotlibrc' )
830
820
yield os .path .join (get_data_path (), 'matplotlibrc' )
831
821
832
822
for fname in gen_candidates ():
0 commit comments