Commits

Antoine Pitrou committed 1edd3f8

Add Path.owner and Path.group

Comments (0)

Files changed (4)

 Version 0.8
 ^^^^^^^^^^^
 
-- Add PurePath.name property.
+- Add PurePath.name
+- Add Path.owner and Path.group
 
 Version 0.7
 ^^^^^^^^^^^
 Concrete paths provide the following attributes:
 
 .. data::
+      Path.owner
+      Path.group
+
+   The owner name and group name of the file, respectively.  :exc:`KeyError`
+   is thrown if the file's uid or gid isn't found in the system database.
+
+
+.. data::
       Path.st_mode
       Path.st_ino
       Path.st_dev
     def __getattr__(self, name):
         if name.startswith('st_'):
             return getattr(self._stat, name)
-        return super(Path, self).__getattribute__(name)
+        return self.__getattribute__(name)
 
     def glob(self, pattern):
         """Iterate over this subtree and yield all existing files (of any
             pass
         return self._stat
 
+    @property
+    def owner(self):
+        """
+        Return the login name of the file owner.
+        """
+        import pwd
+        return pwd.getpwuid(self._stat.st_uid).pw_name
+
+    @property
+    def group(self):
+        """
+        Return the group name of the file gid.
+        """
+        import grp
+        return grp.getgrgid(self._stat.st_gid).gr_name
+
     def raw_open(self, flags, mode=0o777):
         """
         Open the file pointed by this path and return a file descriptor,
     from test import test_support as support
 TESTFN = support.TESTFN
 
+try:
+    import grp, pwd
+except ImportError:
+    grp = pwd = None
+
 
 class _BaseFlavourTest(unittest.TestCase):
 
         with self.assertRaises(AttributeError):
             p.foo
 
+    @unittest.skipUnless(pwd, "the pwd module is needed for this test")
+    def test_owner(self):
+        p = self.cls(BASE)['fileA']
+        uid = p.stat().st_uid
+        name = pwd.getpwuid(uid).pw_name
+        self.assertEqual(name, p.owner)
+
+    @unittest.skipUnless(grp, "the grp module is needed for this test")
+    def test_group(self):
+        p = self.cls(BASE)['fileA']
+        gid = p.stat().st_gid
+        name = grp.getgrgid(gid).gr_name
+        self.assertEqual(name, p.group)
+
     def test_unlink(self):
         p = self.cls(BASE)['fileA']
         p.unlink()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.