Skip to content

Commit

Permalink
Add .group method.
Browse files Browse the repository at this point in the history
Ref #214
  • Loading branch information
jaraco committed Apr 9, 2024
1 parent d702e7d commit af481c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
13 changes: 11 additions & 2 deletions path/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ def access(self, *args, **kwargs):
"""
return os.access(self, *args, **kwargs)

def stat(self):
def stat(self, *, follow_symlinks=True):
"""
Perform a ``stat()`` system call on this path.
Expand All @@ -1268,7 +1268,7 @@ def stat(self):
.. seealso:: :meth:`lstat`, :func:`os.stat`
"""
return os.stat(self)
return os.stat(self, follow_symlinks=follow_symlinks)

def lstat(self):
"""
Expand Down Expand Up @@ -1327,6 +1327,15 @@ def __get_owner_not_implemented(self): # pragma: nocover
.. seealso:: :meth:`get_owner`""",
)

if 'grp' in globals(): # pragma: no cover

def group(self, *, follow_symlinks=True):
"""
Return the group name of the file gid.
"""
gid = self.stat(follow_symlinks=follow_symlinks).st_gid
return grp.getgrgid(gid).gr_name

if hasattr(os, 'statvfs'):

def statvfs(self):
Expand Down
5 changes: 5 additions & 0 deletions test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ def test_removedirs_p(self, tmpdir):
# TODO: shouldn't sub get removed?
# assert not (dir / 'sub').is_dir()

@pytest.mark.skipif("not hasattr(Path, 'group')")
def test_group(self, tmpdir):
file = Path(tmpdir).joinpath('file').touch()
assert isinstance(file.group(), str)


class TestReadWriteText:
def test_read_write(self, tmpdir):
Expand Down

0 comments on commit af481c6

Please sign in to comment.