Skip to content

Commit

Permalink
Directly implement get_owner
Browse files Browse the repository at this point in the history
The implementations for get_owner need to be hiddent behind sys.platform
in order for mypy to not complain about non-existing functionality on
some platforms.
  • Loading branch information
SethMMorton committed Dec 23, 2024
1 parent 0f700b6 commit d63dc73
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,43 +1188,38 @@ def lstat(self) -> os.stat_result:
"""
return os.lstat(self)

def __get_owner_windows(self) -> str: # pragma: nocover
r"""
Return the name of the owner of this file or directory. Follow
symbolic links.
if sys.platform == "win32":

Return a name of the form ``DOMAIN\User Name``; may be a group.
def get_owner(self) -> str: # pragma: nocover
r"""
Return the name of the owner of this file or directory. Follow
symbolic links.
.. seealso:: :attr:`owner`
"""
desc = win32security.GetFileSecurity(
self, win32security.OWNER_SECURITY_INFORMATION
)
sid = desc.GetSecurityDescriptorOwner()
account, domain, typecode = win32security.LookupAccountSid(None, sid)
return domain + '\\' + account
Return a name of the form ``DOMAIN\User Name``; may be a group.
def __get_owner_unix(self) -> str: # pragma: nocover
"""
Return the name of the owner of this file or directory. Follow
symbolic links.
.. seealso:: :attr:`owner`
"""
st = self.stat()
return pwd.getpwuid(st.st_uid).pw_name
.. seealso:: :attr:`owner`
"""
if "win32security" not in globals():
raise NotImplementedError("Ownership not available on this platform.")

def __get_owner_not_implemented(self) -> Never: # pragma: nocover
raise NotImplementedError("Ownership not available on this platform.")
desc = win32security.GetFileSecurity(
self, win32security.OWNER_SECURITY_INFORMATION
)
sid = desc.GetSecurityDescriptorOwner()
account, domain, typecode = win32security.LookupAccountSid(None, sid)
return domain + '\\' + account

if sys.platform != "win32":
get_owner = __get_owner_unix
else:
get_owner = (
__get_owner_windows
if "win32security" in globals()
else __get_owner_not_implemented
)

def get_owner(self) -> str: # pragma: nocover
"""
Return the name of the owner of this file or directory. Follow
symbolic links.
.. seealso:: :attr:`owner`
"""
st = self.stat()
return pwd.getpwuid(st.st_uid).pw_name

@property
def owner(self) -> str:
Expand Down

0 comments on commit d63dc73

Please sign in to comment.