1. Antoine Pitrou
  2. pathlib

Commits

Antoine Pitrou  committed eee53f5

Add PurePath.anchor

  • Participants
  • Parent commits 1edd3f8
  • Branches default

Comments (0)

Files changed (4)

File README.txt

View file
 Version 0.8
 ^^^^^^^^^^^
 
-- Add PurePath.name
+- Add PurePath.name and PurePath.anchor
 - Add Path.owner and Path.group
 
 Version 0.7

File docs/index.rst

View file
       >>> PureNTPath('//some/share').root
       '\\'
 
+.. data:: PurePath.anchor
+
+   The concatenation of the drive and root::
+
+      >>> PureNTPath('c:/Program Files/').anchor
+      'c:\\'
+      >>> PureNTPath('c:Program Files/').anchor
+      'c:'
+      >>> PurePosixPath('/etc').anchor
+      '/'
+      >>> PureNTPath('//some/share').anchor
+      '\\\\some\\share\\'
+
 .. data:: PurePath.name
 
    A string representing the final path component, excluding the drive and

File pathlib.py

View file
                     doc="""The root of the path, if any""")
 
     @property
+    def anchor(self):
+        """The concatenation of the drive and root, or ''."""
+        anchor = self._drv + self._root
+        return anchor
+
+    @property
     def name(self):
         """The final path component, if any."""
         parts = self._parts

File test_pathlib.py

View file
         self.assertEqual(P('/').root, sep)
         self.assertEqual(P('/a/b').root, sep)
 
+    def test_anchor_common(self):
+        P = self.cls
+        sep = self.sep
+        self.assertEqual(P('').anchor, '')
+        self.assertEqual(P('a/b').anchor, '')
+        self.assertEqual(P('/').anchor, sep)
+        self.assertEqual(P('/a/b').anchor, sep)
+
     def test_name_common(self):
         P = self.cls
         self.assertEqual(P('').name, '')
         self.assertEqual(P('//a/b/').root, '\\')
         self.assertEqual(P('//a/b/c/d').root, '\\')
 
+    def test_anchor(self):
+        P = self.cls
+        self.assertEqual(P('c:').anchor, 'c:')
+        self.assertEqual(P('c:a/b').anchor, 'c:')
+        self.assertEqual(P('c:/').anchor, 'c:\\')
+        self.assertEqual(P('c:/a/b/').anchor, 'c:\\')
+        self.assertEqual(P('//a/b').anchor, '\\\\a\\b\\')
+        self.assertEqual(P('//a/b/').anchor, '\\\\a\\b\\')
+        self.assertEqual(P('//a/b/c/d').anchor, '\\\\a\\b\\')
+
     def test_name(self):
         P = self.cls
         self.assertEqual(P('c:').name, '')