Skip to content

Commit 431e4a2

Browse files
committed
Add .group method.
Ref #214
1 parent c25980d commit 431e4a2

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

path/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ def access(self, *args, **kwargs):
12591259
"""
12601260
return os.access(self, *args, **kwargs)
12611261

1262-
def stat(self):
1262+
def stat(self, *, follow_symlinks=True):
12631263
"""
12641264
Perform a ``stat()`` system call on this path.
12651265
@@ -1268,7 +1268,7 @@ def stat(self):
12681268
12691269
.. seealso:: :meth:`lstat`, :func:`os.stat`
12701270
"""
1271-
return os.stat(self)
1271+
return os.stat(self, follow_symlinks=follow_symlinks)
12721272

12731273
def lstat(self):
12741274
"""
@@ -1327,6 +1327,15 @@ def __get_owner_not_implemented(self): # pragma: nocover
13271327
.. seealso:: :meth:`get_owner`""",
13281328
)
13291329

1330+
if 'grp' in globals(): # pragma: no cover
1331+
1332+
def group(self, *, follow_symlinks=True):
1333+
"""
1334+
Return the group name of the file gid.
1335+
"""
1336+
gid = self.stat(follow_symlinks=follow_symlinks).st_gid
1337+
return grp.getgrgid(gid).gr_name
1338+
13301339
if hasattr(os, 'statvfs'):
13311340

13321341
def statvfs(self):

test_path.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ def test_removedirs_p(self, tmpdir):
300300
# TODO: shouldn't sub get removed?
301301
# assert not (dir / 'sub').is_dir()
302302

303+
@pytest.mark.skipif("not hasattr(Path, 'group')")
304+
def test_group(self, tmpdir):
305+
file = Path(tmpdir).joinpath('file').touch()
306+
assert isinstance(file.group(), str)
307+
303308

304309
class TestReadWriteText:
305310
def test_read_write(self, tmpdir):

0 commit comments

Comments
 (0)