17
17
from typing import Optional , Protocol , runtime_checkable
18
18
19
19
20
- def _explode_path (path ):
20
+ def _explode_path (path , split ):
21
21
"""
22
22
Split the path into a 2-tuple (anchor, parts), where *anchor* is the
23
23
uppermost parent of the path (equivalent to path.parents[-1]), and
24
24
*parts* is a reversed list of parts following the anchor.
25
25
"""
26
- split = path .parser .split
27
- path = str (path )
28
26
parent , name = split (path )
29
27
names = []
30
28
while path != parent :
@@ -95,7 +93,7 @@ def __str__(self):
95
93
@property
96
94
def anchor (self ):
97
95
"""The concatenation of the drive and root, or ''."""
98
- return _explode_path (self )[0 ]
96
+ return _explode_path (str ( self ), self . parser . split )[0 ]
99
97
100
98
@property
101
99
def name (self ):
@@ -169,7 +167,7 @@ def with_suffix(self, suffix):
169
167
def parts (self ):
170
168
"""An object providing sequence-like access to the
171
169
components in the filesystem path."""
172
- anchor , parts = _explode_path (self )
170
+ anchor , parts = _explode_path (str ( self ), self . parser . split )
173
171
if anchor :
174
172
parts .append (anchor )
175
173
return tuple (reversed (parts ))
@@ -221,11 +219,9 @@ def full_match(self, pattern):
221
219
Return True if this path matches the given glob-style pattern. The
222
220
pattern is matched against the entire path.
223
221
"""
224
- if not hasattr (pattern , 'with_segments' ):
225
- pattern = self .with_segments (pattern )
226
222
case_sensitive = self .parser .normcase ('Aa' ) == 'Aa'
227
- globber = _PathGlobber (pattern .parser .sep , case_sensitive , recursive = True )
228
- match = globber .compile (str ( pattern ) , altsep = pattern .parser .altsep )
223
+ globber = _PathGlobber (self .parser .sep , case_sensitive , recursive = True )
224
+ match = globber .compile (pattern , altsep = self .parser .altsep )
229
225
return match (str (self )) is not None
230
226
231
227
@@ -282,9 +278,7 @@ def glob(self, pattern, *, recurse_symlinks=True):
282
278
"""Iterate over this subtree and yield all existing files (of any
283
279
kind, including directories) matching the given relative pattern.
284
280
"""
285
- if not hasattr (pattern , 'with_segments' ):
286
- pattern = self .with_segments (pattern )
287
- anchor , parts = _explode_path (pattern )
281
+ anchor , parts = _explode_path (pattern , self .parser .split )
288
282
if anchor :
289
283
raise NotImplementedError ("Non-relative patterns are unsupported" )
290
284
elif not parts :
@@ -338,14 +332,8 @@ def copy(self, target, **kwargs):
338
332
"""
339
333
Recursively copy this file or directory tree to the given destination.
340
334
"""
341
- if not hasattr (target , 'with_segments' ):
342
- target = self .with_segments (target )
343
335
ensure_distinct_paths (self , target )
344
- try :
345
- copy_to_target = target ._copy_from
346
- except AttributeError :
347
- raise TypeError (f"Target path is not writable: { target !r} " ) from None
348
- copy_to_target (self , ** kwargs )
336
+ target ._copy_from (self , ** kwargs )
349
337
return target .joinpath () # Empty join to ensure fresh metadata.
350
338
351
339
def copy_into (self , target_dir , ** kwargs ):
@@ -355,11 +343,7 @@ def copy_into(self, target_dir, **kwargs):
355
343
name = self .name
356
344
if not name :
357
345
raise ValueError (f"{ self !r} has an empty name" )
358
- elif hasattr (target_dir , 'with_segments' ):
359
- target = target_dir / name
360
- else :
361
- target = self .with_segments (target_dir , name )
362
- return self .copy (target , ** kwargs )
346
+ return self .copy (target_dir / name , ** kwargs )
363
347
364
348
365
349
class _WritablePath (_JoinablePath ):
0 commit comments