22""" This module contains functions to determine where configuration and
33data/cache files used by Astropy should be placed.
44"""
5+ import shutil
6+ import pathlib
57
6- from .. utils . decorators import wraps
8+ import appdirs
79
8- import os
9- import shutil
10- import sys
10+ from astropy .utils .decorators import wraps
1111
1212
1313__all__ = ['get_config_dir' , 'get_cache_dir' , 'set_temp_config' ,
1414 'set_temp_cache' ]
1515
1616
17- def _find_home ():
18- """ Locates and return the home directory (or best approximation) on this
19- system.
20-
21- Raises
22- ------
23- OSError
24- If the home directory cannot be located - usually means you are running
25- Astropy on some obscure platform that doesn't have standard home
26- directories.
27- """
28-
29- # First find the home directory - this is inspired by the scheme ipython
30- # uses to identify "home"
31- if os .name == 'posix' :
32- # Linux, Unix, AIX, OS X
33- if 'HOME' in os .environ :
34- homedir = os .environ ['HOME' ]
35- else :
36- raise OSError ('Could not find unix home directory to search for '
37- 'astropy config dir' )
38- elif os .name == 'nt' : # This is for all modern Windows (NT or after)
39- if 'MSYSTEM' in os .environ and os .environ .get ('HOME' ):
40- # Likely using an msys shell; use whatever it is using for its
41- # $HOME directory
42- homedir = os .environ ['HOME' ]
43- # Next try for a network home
44- elif 'HOMESHARE' in os .environ :
45- homedir = os .environ ['HOMESHARE' ]
46- # See if there's a local home
47- elif 'HOMEDRIVE' in os .environ and 'HOMEPATH' in os .environ :
48- homedir = os .path .join (os .environ ['HOMEDRIVE' ],
49- os .environ ['HOMEPATH' ])
50- # Maybe a user profile?
51- elif 'USERPROFILE' in os .environ :
52- homedir = os .path .join (os .environ ['USERPROFILE' ])
53- else :
54- try :
55- import winreg as wreg
56- shell_folders = r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders'
57- key = wreg .OpenKey (wreg .HKEY_CURRENT_USER , shell_folders )
58-
59- homedir = wreg .QueryValueEx (key , 'Personal' )[0 ]
60- key .Close ()
61- except Exception :
62- # As a final possible resort, see if HOME is present
63- if 'HOME' in os .environ :
64- homedir = os .environ ['HOME' ]
65- else :
66- raise OSError ('Could not find windows home directory to '
67- 'search for astropy config dir' )
68- else :
69- # for other platforms, try HOME, although it probably isn't there
70- if 'HOME' in os .environ :
71- homedir = os .environ ['HOME' ]
72- else :
73- raise OSError ('Could not find a home directory to search for '
74- 'astropy config dir - are you on an unsupported '
75- 'platform?' )
76- return homedir
77-
78-
7917def get_config_dir (rootname = 'astropy' ):
8018 """
8119 Determines the package configuration directory name and creates the
8220 directory if it doesn't exist.
8321
84- This directory is typically ``$HOME/.astropy/config``, but if the
85- XDG_CONFIG_HOME environment variable is set and the
86- ``$XDG_CONFIG_HOME/astropy`` directory exists, it will be that directory.
87- If neither exists, the former will be created and symlinked to the latter.
88-
8922 Parameters
9023 ----------
9124 rootname : str
@@ -99,29 +32,18 @@ def get_config_dir(rootname='astropy'):
9932 The absolute path to the configuration directory.
10033
10134 """
102-
103- # symlink will be set to this if the directory is created
104- linkto = None
105-
10635 # If using set_temp_config, that overrides all
10736 if set_temp_config ._temp_path is not None :
108- xch = set_temp_config ._temp_path
109- config_path = os .path .join (xch , rootname )
110- if not os .path .exists (config_path ):
111- os .mkdir (config_path )
112- return os .path .abspath (config_path )
37+ xch = pathlib .Path (set_temp_config ._temp_path )
38+ config_path = xch / rootname
11339
114- # first look for XDG_CONFIG_HOME
115- xch = os . environ . get ( 'XDG_CONFIG_HOME' )
40+ else :
41+ config_path = pathlib . Path ( appdirs . user_config_dir ( rootname ) )
11642
117- if xch is not None and os .path .exists (xch ):
118- xchpth = os .path .join (xch , rootname )
119- if not os .path .islink (xchpth ):
120- if os .path .exists (xchpth ):
121- return os .path .abspath (xchpth )
122- else :
123- linkto = xchpth
124- return os .path .abspath (_find_or_create_root_dir ('config' , linkto , rootname ))
43+ if not config_path .exists ():
44+ config_path .mkdir ()
45+
46+ return str (config_path .absolute ())
12547
12648
12749def get_cache_dir (rootname = "astropy" ):
@@ -148,29 +70,17 @@ def get_cache_dir(rootname="astropy"):
14870
14971 """
15072
151- # symlink will be set to this if the directory is created
152- linkto = None
153-
15473 # If using set_temp_cache, that overrides all
15574 if set_temp_cache ._temp_path is not None :
156- xch = set_temp_cache ._temp_path
157- cache_path = os .path .join (xch , rootname )
158- if not os .path .exists (cache_path ):
159- os .mkdir (cache_path )
160- return os .path .abspath (cache_path )
161-
162- # first look for XDG_CACHE_HOME
163- xch = os .environ .get ('XDG_CACHE_HOME' )
75+ xch = pathlib .Path (set_temp_cache ._temp_path )
76+ cache_path = xch / rootname
77+ else :
78+ cache_path = pathlib .Path (appdirs .user_cache_dir (rootname ))
16479
165- if xch is not None and os .path .exists (xch ):
166- xchpth = os .path .join (xch , rootname )
167- if not os .path .islink (xchpth ):
168- if os .path .exists (xchpth ):
169- return os .path .abspath (xchpth )
170- else :
171- linkto = xchpth
80+ if not cache_path .exists ():
81+ cache_path .mkdir ()
17282
173- return os . path . abspath ( _find_or_create_root_dir ( 'cache' , linkto , rootname ))
83+ return str ( cache_path . absolute ( ))
17484
17585
17686class _SetTempPath :
@@ -179,7 +89,7 @@ class _SetTempPath:
17989
18090 def __init__ (self , path = None , delete = False ):
18191 if path is not None :
182- path = os . path . abspath (path )
92+ path = pathlib . Path (path )
18393
18494 self ._path = path
18595 self ._delete = delete
@@ -281,37 +191,3 @@ class set_temp_cache(_SetTempPath):
281191 """
282192
283193 _default_path_getter = staticmethod (get_cache_dir )
284-
285-
286- def _find_or_create_root_dir (dirnm , linkto , pkgname = 'astropy' ):
287- innerdir = os .path .join (_find_home (), '.{}' .format (pkgname ))
288- maindir = os .path .join (_find_home (), '.{}' .format (pkgname ), dirnm )
289-
290- if not os .path .exists (maindir ):
291- # first create .astropy dir if needed
292- if not os .path .exists (innerdir ):
293- try :
294- os .mkdir (innerdir )
295- except OSError :
296- if not os .path .isdir (innerdir ):
297- raise
298- elif not os .path .isdir (innerdir ):
299- msg = 'Intended {0} {1} directory {1} is actually a file.'
300- raise OSError (msg .format (pkgname , dirnm , maindir ))
301-
302- try :
303- os .mkdir (maindir )
304- except OSError :
305- if not os .path .isdir (maindir ):
306- raise
307-
308- if (not sys .platform .startswith ('win' ) and
309- linkto is not None and
310- not os .path .exists (linkto )):
311- os .symlink (maindir , linkto )
312-
313- elif not os .path .isdir (maindir ):
314- msg = 'Intended {0} {1} directory {1} is actually a file.'
315- raise OSError (msg .format (pkgname , dirnm , maindir ))
316-
317- return os .path .abspath (maindir )
0 commit comments