@@ -386,27 +386,35 @@ def ge(self, level):
386
386
verbose = Verbose ()
387
387
388
388
389
- def _wrap (fmt , func , level = logging . DEBUG , always = True ):
389
+ def _logged_cached (fmt , func = None ):
390
390
"""
391
- return a callable function that wraps func and reports its
392
- output through logger
391
+ Decorator that logs a function's return value, and memoizes that value.
393
392
394
- if always is True, the report will occur on every function
395
- call; otherwise only on the first time the function is called
393
+ After ::
394
+
395
+ @_logged_cached(fmt)
396
+ def func(): ...
397
+
398
+ the first call to *func* will log its return value at the DEBUG level using
399
+ %-format string *fmt*, and memoize it; later calls to *func* will directly
400
+ return that value.
396
401
"""
397
- assert callable (func )
398
402
399
- def wrapper (* args , ** kwargs ):
400
- ret = func (* args , ** kwargs )
403
+ if func is None :
404
+ return functools .partial (_logged_cached , fmt )
405
+
406
+ called = False
407
+ ret = None
401
408
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
409
+ @functools .wraps (func )
410
+ def wrapper ():
411
+ nonlocal called , ret
412
+ if not called :
413
+ ret = func ()
414
+ called = True
415
+ _log .debug (fmt , ret )
407
416
return ret
408
- wrapper ._spoke = False
409
- wrapper .__doc__ = func .__doc__
417
+
410
418
return wrapper
411
419
412
420
@@ -544,7 +552,8 @@ def checkdep_usetex(s):
544
552
return flag
545
553
546
554
547
- def _get_home ():
555
+ @_logged_cached ('$HOME=%s' )
556
+ def get_home ():
548
557
"""
549
558
Return the user's home directory.
550
559
@@ -555,8 +564,6 @@ def _get_home():
555
564
except Exception :
556
565
return None
557
566
558
- get_home = _wrap ('$HOME=%s' , _get_home , always = False )
559
-
560
567
561
568
def _create_tmp_config_dir ():
562
569
"""
@@ -575,7 +582,7 @@ def _get_xdg_config_dir():
575
582
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
576
583
"""
577
584
return (os .environ .get ('XDG_CONFIG_HOME' )
578
- or (Path (get_home (), ".config" )
585
+ or (str ( Path (get_home (), ".config" ) )
579
586
if get_home ()
580
587
else None ))
581
588
@@ -587,21 +594,21 @@ def _get_xdg_cache_dir():
587
594
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
588
595
"""
589
596
return (os .environ .get ('XDG_CACHE_HOME' )
590
- or (Path (get_home (), ".cache" )
597
+ or (str ( Path (get_home (), ".cache" ) )
591
598
if get_home ()
592
599
else None ))
593
600
594
601
595
602
def _get_config_or_cache_dir (xdg_base ):
596
603
configdir = os .environ .get ('MPLCONFIGDIR' )
597
- configdir = (
598
- Path (configdir ).resolve ()
599
- if configdir
600
- else Path (xdg_base , "matplotlib" )
601
- if sys . platform . startswith (( 'linux' , 'freebsd' )) and xdg_base
602
- else Path (get_home (), ".matplotlib" )
603
- if get_home ()
604
- else None )
604
+ if configdir :
605
+ configdir = Path (configdir ).resolve ()
606
+ elif sys . platform . startswith (( 'linux' , 'freebsd' )) and xdg_base :
607
+ configdir = Path (xdg_base , "matplotlib" )
608
+ elif get_home ():
609
+ configdir = Path (get_home (), ".matplotlib" )
610
+ else :
611
+ configdir = None
605
612
606
613
if configdir :
607
614
try :
@@ -615,7 +622,9 @@ def _get_config_or_cache_dir(xdg_base):
615
622
return _create_tmp_config_dir ()
616
623
617
624
618
- def _get_configdir ():
625
+
626
+ @_logged_cached ('CONFIGDIR=%s' )
627
+ def get_configdir ():
619
628
"""
620
629
Return the string representing the configuration directory.
621
630
@@ -633,10 +642,9 @@ def _get_configdir():
633
642
"""
634
643
return _get_config_or_cache_dir (_get_xdg_config_dir ())
635
644
636
- get_configdir = _wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
637
-
638
645
639
- def _get_cachedir ():
646
+ @_logged_cached ('CACHEDIR=%s' )
647
+ def get_cachedir ():
640
648
"""
641
649
Return the location of the cache directory.
642
650
@@ -645,8 +653,6 @@ def _get_cachedir():
645
653
"""
646
654
return _get_config_or_cache_dir (_get_xdg_cache_dir ())
647
655
648
- get_cachedir = _wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
649
-
650
656
651
657
def _decode_filesystem_path (path ):
652
658
if not isinstance (path , str ):
@@ -698,14 +704,12 @@ def _get_data_path():
698
704
raise RuntimeError ('Could not find the matplotlib data files' )
699
705
700
706
701
- def _get_data_path_cached ():
707
+ @_logged_cached ('matplotlib data path: %s' )
708
+ def get_data_path ():
702
709
if defaultParams ['datapath' ][0 ] is None :
703
710
defaultParams ['datapath' ][0 ] = _get_data_path ()
704
711
return defaultParams ['datapath' ][0 ]
705
712
706
- get_data_path = _wrap ('matplotlib data path %s' , _get_data_path_cached ,
707
- always = False )
708
-
709
713
710
714
def get_py2exe_datafiles ():
711
715
data_path = Path (get_data_path ())
@@ -756,7 +760,7 @@ def gen_candidates():
756
760
else :
757
761
yield matplotlibrc
758
762
yield os .path .join (matplotlibrc , 'matplotlibrc' )
759
- yield os .path .join (_get_configdir (), 'matplotlibrc' )
763
+ yield os .path .join (get_configdir (), 'matplotlibrc' )
760
764
yield os .path .join (get_data_path (), 'matplotlibrc' )
761
765
762
766
for fname in gen_candidates ():
0 commit comments