@@ -216,13 +216,6 @@ def compare_versions(a, b):
216
216
sys .argv = [str ('modpython' )]
217
217
218
218
219
- def _is_writable_dir (p ):
220
- """
221
- p is a string pointing to a putative writable dir -- return True p
222
- is such a string, else False
223
- """
224
- return os .access (p , os .W_OK ) and os .path .isdir (p )
225
-
226
219
_verbose_msg = """\
227
220
matplotlib.verbose is deprecated;
228
221
Command line argument --verbose-LEVEL is deprecated.
@@ -552,48 +545,39 @@ def checkdep_usetex(s):
552
545
553
546
554
547
def _get_home ():
555
- """Find user's home directory if possible.
556
- Otherwise, returns None .
548
+ """
549
+ Return the user's home directory .
557
550
558
- :see:
559
- http://mail.python.org/pipermail/python-list/2005-February/325395.html
551
+ If the user's home directory cannot be found, return None.
560
552
"""
561
- path = os .path .expanduser ("~" )
562
- if os .path .isdir (path ):
563
- return path
564
- for evar in ('HOME' , 'USERPROFILE' , 'TMP' ):
565
- path = os .environ .get (evar )
566
- if path is not None and os .path .isdir (path ):
567
- return path
568
- return None
553
+ try :
554
+ return str (Path .home ())
555
+ except Exception :
556
+ return None
557
+
558
+ get_home = _wrap ('$HOME=%s' , _get_home , always = False )
569
559
570
560
571
561
def _create_tmp_config_dir ():
572
562
"""
573
- If the config directory can not be created, create a temporary
574
- directory.
563
+ If the config directory can not be created, create a temporary directory.
575
564
"""
576
565
configdir = os .environ ['MPLCONFIGDIR' ] = (
577
566
tempfile .mkdtemp (prefix = 'matplotlib-' ))
578
567
atexit .register (shutil .rmtree , configdir )
579
568
return configdir
580
569
581
570
582
- get_home = _wrap ('$HOME=%s' , _get_home , always = False )
583
-
584
-
585
571
def _get_xdg_config_dir ():
586
572
"""
587
573
Returns the XDG configuration directory, according to the `XDG
588
574
base directory spec
589
575
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
590
576
"""
591
- path = os .environ .get ('XDG_CONFIG_HOME' )
592
- if path is None :
593
- path = get_home ()
594
- if path is not None :
595
- path = os .path .join (path , '.config' )
596
- return path
577
+ return (os .environ .get ('XDG_CONFIG_HOME' )
578
+ or (Path (get_home (), ".config" )
579
+ if get_home ()
580
+ else None ))
597
581
598
582
599
583
def _get_xdg_cache_dir ():
@@ -602,43 +586,31 @@ def _get_xdg_cache_dir():
602
586
base directory spec
603
587
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
604
588
"""
605
- path = os .environ .get ('XDG_CACHE_HOME' )
606
- if path is None :
607
- path = get_home ()
608
- if path is not None :
609
- path = os .path .join (path , '.cache' )
610
- return path
589
+ return (os .environ .get ('XDG_CACHE_HOME' )
590
+ or (Path (get_home (), ".cache" )
591
+ if get_home ()
592
+ else None ))
611
593
612
594
613
595
def _get_config_or_cache_dir (xdg_base ):
614
596
configdir = os .environ .get ('MPLCONFIGDIR' )
615
- if configdir is not None :
616
- configdir = os .path .abspath (configdir )
617
- Path (configdir ).mkdir (parents = True , exist_ok = True )
618
- if not _is_writable_dir (configdir ):
619
- return _create_tmp_config_dir ()
620
- return configdir
621
-
622
- p = None
623
- h = get_home ()
624
- if h is not None :
625
- p = os .path .join (h , '.matplotlib' )
626
- if sys .platform .startswith (('linux' , 'freebsd' )):
627
- p = None
628
- if xdg_base is not None :
629
- p = os .path .join (xdg_base , 'matplotlib' )
630
-
631
- if p is not None :
632
- if os .path .exists (p ):
633
- if _is_writable_dir (p ):
634
- return p
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 )
605
+
606
+ if configdir :
607
+ try :
608
+ configdir .mkdir (parents = True , exist_ok = True )
609
+ except OSError :
610
+ pass
635
611
else :
636
- try :
637
- Path (p ).mkdir (parents = True , exist_ok = True )
638
- except OSError :
639
- pass
640
- else :
641
- return p
612
+ if os .access (str (configdir ), os .W_OK ) and configdir .is_dir ():
613
+ return str (configdir )
642
614
643
615
return _create_tmp_config_dir ()
644
616
@@ -650,12 +622,9 @@ def _get_configdir():
650
622
The directory is chosen as follows:
651
623
652
624
1. If the MPLCONFIGDIR environment variable is supplied, choose that.
653
-
654
625
2a. On Linux, follow the XDG specification and look first in
655
626
`$XDG_CONFIG_HOME`, if defined, or `$HOME/.config`.
656
-
657
627
2b. On other platforms, choose `$HOME/.matplotlib`.
658
-
659
628
3. If the chosen directory exists and is writable, use that as the
660
629
configuration directory.
661
630
4. If possible, create a temporary directory, and use it as the
0 commit comments