Skip to content

Commit d0dea40

Browse files
committed
Simplify _get_config_or_cache_dir logic.
1. In get_home, we shouldn't check for $TMP, but instead let this case fall through to _create_config_or_cache_dir. 2. When checking for $XDG_{CONFIG,CACHE}_HOME and $MPLCONFIGDIR, consider empty strings as equivalent to unset (which is standard behavior, e.g. one could write `XDG_CONFIG_HOME= python ...` and expect things to behave as if `XDG_CONFIG_HOME` was indeed unset). 3. The logic in _get_config_or_cache_dir can greatly simplified: try to create a candidate, generating it on the way if necessary; if it cannot be created or is not writable, fallback to a temporary directory.
1 parent 6665bc7 commit d0dea40

File tree

1 file changed

+34
-65
lines changed

1 file changed

+34
-65
lines changed

lib/matplotlib/__init__.py

Lines changed: 34 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,6 @@ def compare_versions(a, b):
216216
sys.argv = [str('modpython')]
217217

218218

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-
226219
_verbose_msg = """\
227220
matplotlib.verbose is deprecated;
228221
Command line argument --verbose-LEVEL is deprecated.
@@ -552,48 +545,39 @@ def checkdep_usetex(s):
552545

553546

554547
def _get_home():
555-
"""Find user's home directory if possible.
556-
Otherwise, returns None.
548+
"""
549+
Return the user's home directory.
557550
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.
560552
"""
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)
569559

570560

571561
def _create_tmp_config_dir():
572562
"""
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.
575564
"""
576565
configdir = os.environ['MPLCONFIGDIR'] = (
577566
tempfile.mkdtemp(prefix='matplotlib-'))
578567
atexit.register(shutil.rmtree, configdir)
579568
return configdir
580569

581570

582-
get_home = _wrap('$HOME=%s', _get_home, always=False)
583-
584-
585571
def _get_xdg_config_dir():
586572
"""
587573
Returns the XDG configuration directory, according to the `XDG
588574
base directory spec
589575
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
590576
"""
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))
597581

598582

599583
def _get_xdg_cache_dir():
@@ -602,43 +586,31 @@ def _get_xdg_cache_dir():
602586
base directory spec
603587
<http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html>`_.
604588
"""
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))
611593

612594

613595
def _get_config_or_cache_dir(xdg_base):
614596
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
635611
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)
642614

643615
return _create_tmp_config_dir()
644616

@@ -650,12 +622,9 @@ def _get_configdir():
650622
The directory is chosen as follows:
651623
652624
1. If the MPLCONFIGDIR environment variable is supplied, choose that.
653-
654625
2a. On Linux, follow the XDG specification and look first in
655626
`$XDG_CONFIG_HOME`, if defined, or `$HOME/.config`.
656-
657627
2b. On other platforms, choose `$HOME/.matplotlib`.
658-
659628
3. If the chosen directory exists and is writable, use that as the
660629
configuration directory.
661630
4. If possible, create a temporary directory, and use it as the

0 commit comments

Comments
 (0)