146146 sys .argv = ['modpython' ]
147147
148148
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-
162149import sys , os , tempfile
163150
164151if sys .version_info [0 ] >= 3 :
@@ -525,50 +512,104 @@ def _create_tmp_config_dir():
525512
526513get_home = verbose .wrap ('$HOME=%s' , _get_home , always = False )
527514
528- def _get_configdir ():
515+ def _get_xdg_config_dir ():
529516 """
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' ))
531522
532- The directory is chosen as follows:
533523
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>`_.
542529 """
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 ):
543534 from matplotlib .cbook import mkdirs
544535
545536 configdir = os .environ .get ('MPLCONFIGDIR' )
546537 if configdir is not None :
547538 if not os .path .exists (configdir ):
548539 from matplotlib .cbook import mkdirs
549540 mkdirs (configdir )
541+
550542 if not _is_writable_dir (configdir ):
551543 return _create_tmp_config_dir ()
552544 return configdir
553545
554546 h = get_home ()
555- if h is not None :
556- p = os .path .join (h , '. matplotlib' )
547+ if sys . platform . startswith ( 'linux' ) :
548+ xdg_path = os .path .join (xdg_base , 'matplotlib' )
557549
550+ p = os .path .join (h , '.matplotlib' )
558551 if os .path .exists (p ):
559- if not _is_writable_dir (p ):
560- return _create_tmp_config_dir ()
552+ warnings .warn (
553+ "Found matplotlib configuration in ~/.matplotlib. "
554+ "To conform with the XDG base directory standard, "
555+ "this configuration directory has been deprecated "
556+ "on Linux, and the new location is now %r. Please "
557+ "move your configuration there to ensure that "
558+ "matplotlib will continue to find it in the future." %
559+ xdg_path )
561560 else :
562- if not _is_writable_dir (h ):
563- return _create_tmp_config_dir ()
564- mkdirs (p )
561+ p = xdg_path
562+ else :
563+ p = os .path .join (h , '.matplotlib' )
564+
565+ if os .path .exists (p ):
566+ if not _is_writable_dir (p ):
567+ return _create_tmp_config_dir ()
568+ else :
569+ if not _is_writable_dir (h ):
570+ return _create_tmp_config_dir ()
571+ mkdirs (p )
572+
573+ return p
574+
575+
576+ def _get_configdir ():
577+ """
578+ Return the string representing the configuration directory.
579+
580+ The directory is chosen as follows:
565581
566- return p
582+ 1. If the MPLCONFIGDIR environment variable is supplied, choose that.
583+
584+ 2a. On Linux, if `$HOME/.matplotlib` exists, choose that, but warn that
585+ that is the old location. Barring that, follow the XDG specification
586+ and look first in `$XDG_CONFIG_HOME`, if defined, or `$HOME/.config`.
587+
588+ 2b. On other platforms, choose `$HOME/.matplotlib`.
589+
590+ 3. If the chosen directory exists and is writable, use that as the
591+ configuration directory.
592+ 4. If possible, create a temporary directory, and use it as the
593+ configuration directory.
594+ 5. A writable directory could not be found or created; return None.
595+ """
596+ return _get_config_or_cache_dir (_get_xdg_config_dir ())
567597
568- return _create_tmp_config_dir ()
569598get_configdir = verbose .wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
570599
571600
601+ def _get_cachedir ():
602+ """
603+ Return the location of the cache directory.
604+
605+ The procedure used to find the directory is the same as for
606+ _get_config_dir, except using `$XDG_CONFIG_HOME`/`~/.cache` instead.
607+ """
608+ return _get_config_or_cache_dir (_get_xdg_cache_dir ())
609+
610+ get_cachedir = verbose .wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
611+
612+
572613def _get_data_path ():
573614 'get the path to matplotlib data'
574615
@@ -643,50 +684,36 @@ def get_py2exe_datafiles():
643684
644685def matplotlib_fname ():
645686 """
646- Return the path to the rc file used by matplotlib .
687+ Get the location of the config file.
647688
648- Search order:
689+ The file location is determined in the following order
649690
650- * current working dir
651- * environ var MATPLOTLIBRC
652- * HOME/.matplotlib/matplotlibrc
653- * MATPLOTLIBDATA/matplotlibrc
691+ - `$PWD/matplotlibrc`
654692
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 )
693+ - environment variable `MATPLOTLIBRC`
694+
695+ - `$MPLCONFIGDIR/matplotlib`
696+
697+ - On Linux,
687698
699+ - `$HOME/.matplotlib/matplotlibrc`, if it exists
700+
701+ - or `$XDG_CONFIG_HOME/matplotlib/matplotlibrc` (if
702+ $XDG_CONFIG_HOME is defined)
703+
704+ - or `$HOME/.config/matplotlib/matplotlibrc` (if
705+ $XDG_CONFIG_HOME is not defined)
706+
707+ - On other platforms,
708+
709+ - `$HOME/.matplotlib/matplotlibrc` if `$HOME` is defined.
710+
711+ - Lastly, it looks in `$MATPLOTLIBDATA/matplotlibrc` for a
712+ system-defined copy.
713+ """
688714 fname = os .path .join (os .getcwd (), 'matplotlibrc' )
689- if os .path .exists (fname ): return fname
715+ if os .path .exists (fname ):
716+ return fname
690717
691718 if 'MATPLOTLIBRC' in os .environ :
692719 path = os .environ ['MATPLOTLIBRC' ]
@@ -695,15 +722,17 @@ def matplotlib_fname():
695722 if os .path .exists (fname ):
696723 return fname
697724
725+ configdir = _get_configdir ()
698726 if configdir is not None :
699727 fname = os .path .join (configdir , 'matplotlibrc' )
700728 if os .path .exists (fname ):
701729 return fname
702730
703- path = get_data_path () # guaranteed to exist or raise
731+ path = get_data_path () # guaranteed to exist or raise
704732 fname = os .path .join (path , 'matplotlibrc' )
705733 if not os .path .exists (fname ):
706734 warnings .warn ('Could not find matplotlibrc; using defaults' )
735+
707736 return fname
708737
709738
0 commit comments