146
146
sys .argv = ['modpython' ]
147
147
148
148
149
- """
150
- Manage user customizations through a rc file.
151
-
152
- The default file location is given in the following order
153
-
154
- - environment variable MATPLOTLIBRC
155
-
156
- - HOME/.matplotlib/matplotlibrc if HOME is defined
157
-
158
- - PATH/matplotlibrc where PATH is the return value of
159
- get_data_path()
160
- """
161
-
162
149
import sys , os , tempfile
163
150
164
151
if sys .version_info [0 ] >= 3 :
@@ -525,50 +512,91 @@ def _create_tmp_config_dir():
525
512
526
513
get_home = verbose .wrap ('$HOME=%s' , _get_home , always = False )
527
514
528
- def _get_configdir ():
515
+ def _get_xdg_config_dir ():
529
516
"""
530
- Return the string representing the configuration directory.
517
+ Returns the XDG configuration directory, according to the `XDG
518
+ base directory spec
519
+ <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
520
+ """
521
+ return os .environ .get ('XDG_CONFIG_HOME' , os .path .join (get_home (), '.config' ))
531
522
532
- The directory is chosen as follows:
533
523
534
- 1. If the MPLCONFIGDIR environment variable is supplied, choose that. Else,
535
- choose the '.matplotlib' subdirectory of the user's home directory (and
536
- create it if necessary).
537
- 2. If the chosen directory exists and is writable, use that as the
538
- configuration directory.
539
- 3. If possible, create a temporary directory, and use it as the
540
- configuration directory.
541
- 4. A writable directory could not be found or created; return None.
524
+ def _get_xdg_cache_dir ():
525
+ """
526
+ Returns the XDG cache directory, according to the `XDG
527
+ base directory spec
528
+ <http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
542
529
"""
530
+ return os .environ .get ('XDG_CACHE_HOME' , os .path .join (get_home (), '.cache' ))
531
+
532
+
533
+ def _get_config_or_cache_dir (xdg_base ):
543
534
from matplotlib .cbook import mkdirs
544
535
545
536
configdir = os .environ .get ('MPLCONFIGDIR' )
546
537
if configdir is not None :
547
538
if not os .path .exists (configdir ):
548
539
from matplotlib .cbook import mkdirs
549
540
mkdirs (configdir )
541
+
550
542
if not _is_writable_dir (configdir ):
551
543
return _create_tmp_config_dir ()
552
544
return configdir
553
545
554
546
h = get_home ()
555
- if h is not None :
556
- p = os .path .join (h , '.matplotlib' )
547
+ p = os .path .join (h , '.matplotlib' )
548
+ if (sys .platform .startswith ('linux' ) and
549
+ not os .path .exists (p )):
550
+ p = _get_xdg_config_dir ()
557
551
558
- if os .path .exists (p ):
559
- if not _is_writable_dir (p ):
560
- return _create_tmp_config_dir ()
561
- else :
562
- if not _is_writable_dir (h ):
563
- return _create_tmp_config_dir ()
564
- mkdirs (p )
552
+ if os .path .exists (p ):
553
+ if not _is_writable_dir (p ):
554
+ return _create_tmp_config_dir ()
555
+ else :
556
+ if not _is_writable_dir (h ):
557
+ return _create_tmp_config_dir ()
558
+ mkdirs (p )
559
+
560
+ return p
561
+
562
+
563
+ def _get_configdir ():
564
+ """
565
+ Return the string representing the configuration directory.
565
566
566
- return p
567
+ The directory is chosen as follows:
568
+
569
+ 1. If the MPLCONFIGDIR environment variable is supplied, choose that.
570
+
571
+ 2a. On Linux, if `$HOME/.matplotlib` exists, choose that, but warn that
572
+ that is the old location. Barring that, follow the XDG specification
573
+ and look first in `$XDG_CONFIG_HOME`, if defined, or `$HOME/.config`.
574
+
575
+ 2b. On other platforms, choose `$HOME/.matplotlib`.
576
+
577
+ 3. If the chosen directory exists and is writable, use that as the
578
+ configuration directory.
579
+ 4. If possible, create a temporary directory, and use it as the
580
+ configuration directory.
581
+ 5. A writable directory could not be found or created; return None.
582
+ """
583
+ return _get_config_or_cache_dir (_get_xdg_config_dir ())
567
584
568
- return _create_tmp_config_dir ()
569
585
get_configdir = verbose .wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
570
586
571
587
588
+ def _get_cachedir ():
589
+ """
590
+ Return the location of the cache directory.
591
+
592
+ The procedure used to find the directory is the same as for
593
+ _get_config_dir, except using `$XDG_CONFIG_HOME`/`~/.cache` instead.
594
+ """
595
+ return _get_config_or_cache_dir (_get_xdg_cache_dir ())
596
+
597
+ get_cachedir = verbose .wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
598
+
599
+
572
600
def _get_data_path ():
573
601
'get the path to matplotlib data'
574
602
@@ -643,50 +671,36 @@ def get_py2exe_datafiles():
643
671
644
672
def matplotlib_fname ():
645
673
"""
646
- Return the path to the rc file used by matplotlib .
674
+ Get the location of the config file.
647
675
648
- Search order:
676
+ The file location is determined in the following order
649
677
650
- * current working dir
651
- * environ var MATPLOTLIBRC
652
- * HOME/.matplotlib/matplotlibrc
653
- * MATPLOTLIBDATA/matplotlibrc
678
+ - `$PWD/matplotlibrc`
654
679
655
- """
656
- oldname = os .path .join (os .getcwd (), '.matplotlibrc' )
657
- if os .path .exists (oldname ):
658
- try :
659
- shutil .move ('.matplotlibrc' , 'matplotlibrc' )
660
- except IOError as e :
661
- warnings .warn ('File could not be renamed: %s' % e )
662
- else :
663
- warnings .warn ("""\
664
- Old rc filename ".matplotlibrc" found in working dir and and renamed to new
665
- default rc file name "matplotlibrc" (no leading ".").""" )
666
-
667
- home = get_home ()
668
- configdir = get_configdir ()
669
- if home :
670
- oldname = os .path .join (home , '.matplotlibrc' )
671
- if os .path .exists (oldname ):
672
- if configdir is not None :
673
- newname = os .path .join (configdir , 'matplotlibrc' )
674
-
675
- try :
676
- shutil .move (oldname , newname )
677
- except IOError as e :
678
- warnings .warn ('File could not be renamed: %s' % e )
679
- else :
680
- warnings .warn ("""\
681
- Old rc filename "%s" found and renamed to new default rc file name "%s"."""
682
- % (oldname , newname ))
683
- else :
684
- warnings .warn ("""\
685
- Could not rename old rc file "%s": a suitable configuration directory could not
686
- be found.""" % oldname )
680
+ - environment variable `MATPLOTLIBRC`
681
+
682
+ - `$MPLCONFIGDIR/matplotlib`
683
+
684
+ - On Linux,
685
+
686
+ - `$HOME/.matplotlib/matplotlibrc`, if it exists
687
687
688
+ - or `$XDG_CONFIG_HOME/matplotlib/matplotlibrc` (if
689
+ $XDG_CONFIG_HOME is defined)
690
+
691
+ - or `$HOME/.config/matplotlib/matplotlibrc` (if
692
+ $XDG_CONFIG_HOME is not defined)
693
+
694
+ - On other platforms,
695
+
696
+ - `$HOME/.matplotlib/matplotlibrc` if `$HOME` is defined.
697
+
698
+ - Lastly, it looks in `$MATPLOTLIBDATA/matplotlibrc` for a
699
+ system-defined copy.
700
+ """
688
701
fname = os .path .join (os .getcwd (), 'matplotlibrc' )
689
- if os .path .exists (fname ): return fname
702
+ if os .path .exists (fname ):
703
+ return fname
690
704
691
705
if 'MATPLOTLIBRC' in os .environ :
692
706
path = os .environ ['MATPLOTLIBRC' ]
@@ -695,15 +709,28 @@ def matplotlib_fname():
695
709
if os .path .exists (fname ):
696
710
return fname
697
711
712
+ configdir = _get_configdir ()
698
713
if configdir is not None :
699
714
fname = os .path .join (configdir , 'matplotlibrc' )
700
715
if os .path .exists (fname ):
716
+ if (sys .platform .startswith ('linux' ) and
717
+ fname == os .path .join (
718
+ get_home (), '.matplotlib' , 'matplotlibrc' )):
719
+ warnings .warn (
720
+ "Found matplotlib configuration in ~/.matplotlib. "
721
+ "To conform with the XDG base directory standard, "
722
+ "this configuration location has been deprecated "
723
+ "on Linux, and the new location is now %r. Please "
724
+ "move your configuration there to ensure that "
725
+ "matplotlib will continue to find it in the future." %
726
+ _get_xdg_config_dir ())
701
727
return fname
702
728
703
- path = get_data_path () # guaranteed to exist or raise
729
+ path = get_data_path () # guaranteed to exist or raise
704
730
fname = os .path .join (path , 'matplotlibrc' )
705
731
if not os .path .exists (fname ):
706
732
warnings .warn ('Could not find matplotlibrc; using defaults' )
733
+
707
734
return fname
708
735
709
736
0 commit comments