77import shutil
88import sys
99from functools import wraps
10+ from pathlib import Path
1011
1112__all__ = ["get_config_dir" , "get_cache_dir" , "set_temp_config" , "set_temp_cache" ]
1213
1314
14- def _find_home ():
15- """Locates and return the home directory (or best approximation) on this
16- system.
17-
18- Raises
19- ------
20- OSError
21- If the home directory cannot be located - usually means you are running
22- Astropy on some obscure platform that doesn't have standard home
23- directories.
24- """
25- try :
26- homedir = os .path .expanduser ("~" )
27- except Exception :
28- # Linux, Unix, AIX, OS X
29- if os .name == "posix" :
30- if "HOME" in os .environ :
31- homedir = os .environ ["HOME" ]
32- else :
33- raise OSError (
34- "Could not find unix home directory to search for "
35- "astropy config dir"
36- )
37- elif os .name == "nt" : # This is for all modern Windows (NT or after)
38- if "MSYSTEM" in os .environ and os .environ .get ("HOME" ):
39- # Likely using an msys shell; use whatever it is using for its
40- # $HOME directory
41- homedir = os .environ ["HOME" ]
42- # See if there's a local home
43- elif "HOMEDRIVE" in os .environ and "HOMEPATH" in os .environ :
44- homedir = os .path .join (os .environ ["HOMEDRIVE" ], os .environ ["HOMEPATH" ])
45- # Maybe a user profile?
46- elif "USERPROFILE" in os .environ :
47- homedir = os .path .join (os .environ ["USERPROFILE" ])
48- else :
49- try :
50- import winreg as wreg
51-
52- shell_folders = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
53- key = wreg .OpenKey (wreg .HKEY_CURRENT_USER , shell_folders )
54-
55- homedir = wreg .QueryValueEx (key , "Personal" )[0 ]
56- key .Close ()
57- except Exception :
58- # As a final possible resort, see if HOME is present
59- if "HOME" in os .environ :
60- homedir = os .environ ["HOME" ]
61- else :
62- raise OSError (
63- "Could not find windows home directory to "
64- "search for astropy config dir"
65- )
66- else :
67- # for other platforms, try HOME, although it probably isn't there
68- if "HOME" in os .environ :
69- homedir = os .environ ["HOME" ]
70- else :
71- raise OSError (
72- "Could not find a home directory to search for "
73- "astropy config dir - are you on an unsupported "
74- "platform?"
75- )
76- return homedir
77-
78-
79- def get_config_dir (rootname = "astropy" ):
15+ def get_config_dir (rootname : str = "astropy" ) -> str :
8016 """
8117 Determines the package configuration directory name and creates the
8218 directory if it doesn't exist.
@@ -105,25 +41,25 @@ def get_config_dir(rootname="astropy"):
10541 # If using set_temp_config, that overrides all
10642 if set_temp_config ._temp_path is not None :
10743 xch = set_temp_config ._temp_path
108- config_path = os .path .join (xch , rootname )
109- if not os .path .exists (config_path ):
110- os .mkdir (config_path )
111- return os .path .abspath (config_path )
44+ config_path = xch / rootname
45+ config_path .mkdir (exist_ok = True )
46+ return str (config_path .resolve ())
11247
11348 # first look for XDG_CONFIG_HOME
114- xch = os .environ .get ("XDG_CONFIG_HOME" )
49+ if (
50+ (xdg_config_home := os .getenv ("XDG_CONFIG_HOME" )) is not None
51+ and (xch := Path (xdg_config_home )).exists ()
52+ and not (xchpth := xch / rootname ).is_symlink ()
53+ ):
54+ if xchpth .exists ():
55+ return str (xchpth .resolve ())
56+ else :
57+ linkto = xchpth
11558
116- if xch is not None and os .path .exists (xch ):
117- xchpth = os .path .join (xch , rootname )
118- if not os .path .islink (xchpth ):
119- if os .path .exists (xchpth ):
120- return os .path .abspath (xchpth )
121- else :
122- linkto = xchpth
123- return os .path .abspath (_find_or_create_root_dir ("config" , linkto , rootname ))
59+ return str (_find_or_create_root_dir ("config" , linkto , rootname ))
12460
12561
126- def get_cache_dir (rootname = "astropy" ):
62+ def get_cache_dir (rootname : str = "astropy" ) -> str :
12763 """
12864 Determines the Astropy cache directory name and creates the directory if it
12965 doesn't exist.
@@ -152,23 +88,23 @@ def get_cache_dir(rootname="astropy"):
15288 # If using set_temp_cache, that overrides all
15389 if set_temp_cache ._temp_path is not None :
15490 xch = set_temp_cache ._temp_path
155- cache_path = os . path . join ( xch , rootname )
156- if not os . path . exists ( cache_path ):
157- os .mkdir (cache_path )
158- return os . path . abspath (cache_path )
91+ cache_path = xch / rootname
92+ if not cache_path . is_file ( ):
93+ cache_path .mkdir (exist_ok = True )
94+ return str (cache_path . resolve () )
15995
16096 # first look for XDG_CACHE_HOME
161- xch = os . environ . get ( "XDG_CACHE_HOME" )
162-
163- if xch is not None and os . path . exists (xch ):
164- xchpth = os . path . join ( xch , rootname )
165- if not os . path . islink ( xchpth ):
166- if os . path . exists (xchpth ):
167- return os . path . abspath (xchpth )
168- else :
169- linkto = xchpth
97+ if (
98+ ( xdg_config_home := os . getenv ( "XDG_CONFIG_HOME" )) is not None
99+ and ( xch := Path ( xdg_config_home )). exists ()
100+ and not ( xchpth := xch / rootname ). is_symlink ( )
101+ ):
102+ if xchpth . exists ():
103+ return str (xchpth . resolve () )
104+ else :
105+ linkto = xchpth
170106
171- return os . path . abspath (_find_or_create_root_dir ("cache" , linkto , rootname ))
107+ return str (_find_or_create_root_dir ("cache" , linkto , rootname ))
172108
173109
174110class _SetTempPath :
@@ -177,7 +113,7 @@ class _SetTempPath:
177113
178114 def __init__ (self , path = None , delete = False ):
179115 if path is not None :
180- path = os . path . abspath ( path )
116+ path = Path ( path ). resolve ( )
181117
182118 self ._path = path
183119 self ._delete = delete
@@ -287,39 +223,30 @@ class set_temp_cache(_SetTempPath):
287223 _default_path_getter = staticmethod (get_cache_dir )
288224
289225
290- def _find_or_create_root_dir (dirnm , linkto , pkgname = "astropy" ):
291- innerdir = os .path .join (_find_home (), f".{ pkgname } " )
292- maindir = os .path .join (_find_home (), f".{ pkgname } " , dirnm )
226+ def _find_or_create_root_dir (
227+ dirnm : str ,
228+ linkto : Path | None ,
229+ pkgname : str = "astropy" ,
230+ ) -> Path :
231+ innerdir = Path .home () / f".{ pkgname } "
232+ maindir = innerdir / dirnm
293233
294- if not os .path .exists (maindir ):
234+ if maindir .is_file ():
235+ raise OSError (
236+ f"Intended { pkgname } { dirnm } directory { maindir } is actually a file."
237+ )
238+ if not maindir .is_dir ():
295239 # first create .astropy dir if needed
296- if not os .path .exists (innerdir ):
297- try :
298- os .mkdir (innerdir )
299- except OSError :
300- if not os .path .isdir (innerdir ):
301- raise
302- elif not os .path .isdir (innerdir ):
240+ if innerdir .is_file ():
303241 raise OSError (
304242 f"Intended { pkgname } { dirnm } directory { maindir } is actually a file."
305243 )
306-
307- try :
308- os .mkdir (maindir )
309- except OSError :
310- if not os .path .isdir (maindir ):
311- raise
312-
244+ maindir .mkdir (parents = True , exist_ok = True )
313245 if (
314246 not sys .platform .startswith ("win" )
315247 and linkto is not None
316- and not os . path . exists (linkto )
248+ and not linkto . exists ()
317249 ):
318250 os .symlink (maindir , linkto )
319251
320- elif not os .path .isdir (maindir ):
321- raise OSError (
322- f"Intended { pkgname } { dirnm } directory { maindir } is actually a file."
323- )
324-
325- return os .path .abspath (maindir )
252+ return maindir .resolve ()
0 commit comments