mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
Issue #19776: Add a expanduser() method on Path objects.
Patch by Serhiy.
This commit is contained in:
parent
864d57c244
commit
8477ed6048
4 changed files with 180 additions and 3 deletions
|
@ -221,6 +221,36 @@ class _WindowsFlavour(_Flavour):
|
|||
# It's a path on a network drive => 'file://host/share/a/b'
|
||||
return 'file:' + urlquote_from_bytes(path.as_posix().encode('utf-8'))
|
||||
|
||||
def gethomedir(self, username):
|
||||
if 'HOME' in os.environ:
|
||||
userhome = os.environ['HOME']
|
||||
elif 'USERPROFILE' in os.environ:
|
||||
userhome = os.environ['USERPROFILE']
|
||||
elif 'HOMEPATH' in os.environ:
|
||||
try:
|
||||
drv = os.environ['HOMEDRIVE']
|
||||
except KeyError:
|
||||
drv = ''
|
||||
userhome = drv + os.environ['HOMEPATH']
|
||||
else:
|
||||
raise RuntimeError("Can't determine home directory")
|
||||
|
||||
if username:
|
||||
# Try to guess user home directory. By default all users
|
||||
# directories are located in the same place and are named by
|
||||
# corresponding usernames. If current user home directory points
|
||||
# to nonstandard place, this guess is likely wrong.
|
||||
if os.environ['USERNAME'] != username:
|
||||
drv, root, parts = self.parse_parts((userhome,))
|
||||
if parts[-1] != os.environ['USERNAME']:
|
||||
raise RuntimeError("Can't determine home directory "
|
||||
"for %r" % username)
|
||||
parts[-1] = username
|
||||
if drv or root:
|
||||
userhome = drv + root + self.join(parts[1:])
|
||||
else:
|
||||
userhome = self.join(parts)
|
||||
return userhome
|
||||
|
||||
class _PosixFlavour(_Flavour):
|
||||
sep = '/'
|
||||
|
@ -304,6 +334,21 @@ class _PosixFlavour(_Flavour):
|
|||
bpath = bytes(path)
|
||||
return 'file://' + urlquote_from_bytes(bpath)
|
||||
|
||||
def gethomedir(self, username):
|
||||
if not username:
|
||||
try:
|
||||
return os.environ['HOME']
|
||||
except KeyError:
|
||||
import pwd
|
||||
return pwd.getpwuid(os.getuid()).pw_dir
|
||||
else:
|
||||
import pwd
|
||||
try:
|
||||
return pwd.getpwnam(username).pw_dir
|
||||
except KeyError:
|
||||
raise RuntimeError("Can't determine home directory "
|
||||
"for %r" % username)
|
||||
|
||||
|
||||
_windows_flavour = _WindowsFlavour()
|
||||
_posix_flavour = _PosixFlavour()
|
||||
|
@ -1333,6 +1378,17 @@ class Path(PurePath):
|
|||
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
|
||||
return False
|
||||
|
||||
def expanduser(self):
|
||||
""" Return a new path with expanded ~ and ~user constructs
|
||||
(as returned by os.path.expanduser)
|
||||
"""
|
||||
if (not (self._drv or self._root) and
|
||||
self._parts and self._parts[0][:1] == '~'):
|
||||
homedir = self._flavour.gethomedir(self._parts[0][1:])
|
||||
return self._from_parts([homedir] + self._parts[1:])
|
||||
|
||||
return self
|
||||
|
||||
|
||||
class PosixPath(Path, PurePosixPath):
|
||||
__slots__ = ()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue