MCPcopy Index your code
hub / github.com/ipython/ipython / get_home_dir

Function get_home_dir

IPython/utils/path.py:156–197  ·  view source on GitHub ↗

Return the 'home' directory, as a unicode string. Uses os.path.expanduser('~'), and checks for writability. See stdlib docs for how this is determined. For Python <3.8, $HOME is first priority on *ALL* platforms. For Python >=3.8 on Windows, %HOME% is no longer considered. Par

(require_writable: bool=False)

Source from the content-addressed store, hash-verified

154
155
156def get_home_dir(require_writable: bool=False) -> str:
157 """Return the 'home' directory, as a unicode string.
158
159 Uses os.path.expanduser('~'), and checks for writability.
160
161 See stdlib docs for how this is determined.
162 For Python <3.8, $HOME is first priority on *ALL* platforms.
163 For Python >=3.8 on Windows, %HOME% is no longer considered.
164
165 Parameters
166 ----------
167 require_writable : bool [default: False]
168 if True:
169 guarantees the return value is a writable directory, otherwise
170 raises HomeDirError
171 if False:
172 The path is resolved, but it is not guaranteed to exist or be writable.
173 """
174
175 homedir = os.path.expanduser('~')
176 # Next line will make things work even when /home/ is a symlink to
177 # /usr/home as it is on FreeBSD, for example
178 homedir = os.path.realpath(homedir)
179
180 if not _writable_dir(homedir) and os.name == 'nt':
181 # expanduser failed, use the registry to get the 'My Documents' folder.
182 try:
183 import winreg as wreg
184 with wreg.OpenKey(
185 wreg.HKEY_CURRENT_USER,
186 r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
187 ) as key:
188 homedir = wreg.QueryValueEx(key,'Personal')[0]
189 except:
190 pass
191
192 if (not require_writable) or _writable_dir(homedir):
193 assert isinstance(homedir, str), "Homedir should be unicode not bytes"
194 return homedir
195 else:
196 raise HomeDirError('%s is not a writable dir, '
197 'set $HOME environment variable to override' % homedir)
198
199def get_xdg_dir() -> str | None:
200 """Return the XDG_CONFIG_HOME, if it is defined and exists, else None.

Callers 4

get_ipython_dirFunction · 0.90
init_pushd_popd_magicMethod · 0.90
get_xdg_dirFunction · 0.85
get_xdg_cache_dirFunction · 0.85

Calls 2

_writable_dirFunction · 0.85
HomeDirErrorClass · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…