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