From 431e4a2290de10f5c238e11f3254b811df34f226 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Mon, 8 Apr 2024 20:18:31 -0400 Subject: [PATCH] Add .group method. Ref #214 --- path/__init__.py | 13 +++++++++++-- test_path.py | 5 +++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/path/__init__.py b/path/__init__.py index cf00808..4520aca 100644 --- a/path/__init__.py +++ b/path/__init__.py @@ -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. @@ -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): """ @@ -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): diff --git a/test_path.py b/test_path.py index 1c5813f..b0caacd 100644 --- a/test_path.py +++ b/test_path.py @@ -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):