@@ -77,10 +77,27 @@ class path(unicode):
77
77
counterparts in os.path.
78
78
"""
79
79
80
+ # Use an alternate path module (e.g. posixpath)
81
+ module = os .path
82
+ _subclass_for_module = {}
83
+ @classmethod
84
+ def using_module (cls , module , value = None ):
85
+ if module in cls ._subclass_for_module :
86
+ subclass = cls ._subclass_for_module [module ]
87
+ else :
88
+ subclass_name = cls .__name__ + '_' + module .__name__
89
+ bases = (cls ,)
90
+ ns = {'module' : module }
91
+ subclass = type (subclass_name , bases , ns )
92
+ cls ._subclass_for_module [module ] = subclass
93
+ if value is None :
94
+ return subclass
95
+ return subclass (value )
96
+
80
97
# --- Special Python methods.
81
98
82
99
def __repr__ (self ):
83
- return 'path (%s)' % super (path , self ).__repr__ ()
100
+ return '%s (%s)' % ( type ( self ). __name__ , super (path , self ).__repr__ () )
84
101
85
102
# Adding a path and a string yields a path.
86
103
def __add__ (self , more ):
@@ -101,7 +118,7 @@ def __div__(self, rel):
101
118
Join two path components, adding a separator character if
102
119
needed.
103
120
"""
104
- return self .__class__ (os . path .join (self , rel ))
121
+ return self .__class__ (self . module .join (self , rel ))
105
122
106
123
# Make the / operator work even when true division is enabled.
107
124
__truediv__ = __div__
@@ -121,14 +138,14 @@ def getcwd(cls):
121
138
#
122
139
# --- Operations on path strings.
123
140
124
- def abspath (self ): return self .__class__ (os . path .abspath (self ))
125
- def normcase (self ): return self .__class__ (os . path .normcase (self ))
126
- def normpath (self ): return self .__class__ (os . path .normpath (self ))
127
- def realpath (self ): return self .__class__ (os . path .realpath (self ))
128
- def expanduser (self ): return self .__class__ (os . path .expanduser (self ))
129
- def expandvars (self ): return self .__class__ (os . path .expandvars (self ))
130
- def dirname (self ): return self .__class__ (os . path .dirname (self ))
131
- def basename (self ): return self .__class__ (os . path .basename (self ))
141
+ def abspath (self ): return self .__class__ (self . module .abspath (self ))
142
+ def normcase (self ): return self .__class__ (self . module .normcase (self ))
143
+ def normpath (self ): return self .__class__ (self . module .normpath (self ))
144
+ def realpath (self ): return self .__class__ (self . module .realpath (self ))
145
+ def expanduser (self ): return self .__class__ (self . module .expanduser (self ))
146
+ def expandvars (self ): return self .__class__ (self . module .expandvars (self ))
147
+ def dirname (self ): return self .__class__ (self . module .dirname (self ))
148
+ def basename (self ): return self .__class__ (self . module .basename (self ))
132
149
133
150
def expand (self ):
134
151
""" Clean up a filename by calling expandvars(),
@@ -140,15 +157,15 @@ def expand(self):
140
157
return self .expandvars ().expanduser ().normpath ()
141
158
142
159
def _get_namebase (self ):
143
- base , ext = os . path .splitext (self .name )
160
+ base , ext = self . module .splitext (self .name )
144
161
return base
145
162
146
163
def _get_ext (self ):
147
- f , ext = os . path .splitext (self )
164
+ f , ext = self . module .splitext (self )
148
165
return ext
149
166
150
167
def _get_drive (self ):
151
- drive , r = os . path .splitdrive (self )
168
+ drive , r = self . module .splitdrive (self )
152
169
return self .__class__ (drive )
153
170
154
171
parent = property (
@@ -185,7 +202,7 @@ def _get_drive(self):
185
202
186
203
def splitpath (self ):
187
204
""" p.splitpath() -> Return (p.parent, p.name). """
188
- parent , child = os . path .split (self )
205
+ parent , child = self . module .split (self )
189
206
return self .__class__ (parent ), child
190
207
191
208
def splitdrive (self ):
@@ -195,7 +212,7 @@ def splitdrive(self):
195
212
no drive specifier, p.drive is empty, so the return value
196
213
is simply (path(''), p). This is always the case on Unix.
197
214
"""
198
- drive , rel = os . path .splitdrive (self )
215
+ drive , rel = self . module .splitdrive (self )
199
216
return self .__class__ (drive ), rel
200
217
201
218
def splitext (self ):
@@ -208,7 +225,7 @@ def splitext(self):
208
225
last path segment. This has the property that if
209
226
(a, b) == p.splitext(), then a + b == p.
210
227
"""
211
- filename , ext = os . path .splitext (self )
228
+ filename , ext = self . module .splitext (self )
212
229
return self .__class__ (filename ), ext
213
230
214
231
def stripext (self ):
@@ -219,26 +236,25 @@ def stripext(self):
219
236
"""
220
237
return self .splitext ()[0 ]
221
238
222
- if hasattr (os .path , 'splitunc' ):
223
- def splitunc (self ):
224
- unc , rest = os .path .splitunc (self )
225
- return self .__class__ (unc ), rest
239
+ def splitunc (self ):
240
+ unc , rest = self .module .splitunc (self )
241
+ return self .__class__ (unc ), rest
226
242
227
- def _get_uncshare (self ):
228
- unc , r = os . path .splitunc (self )
229
- return self .__class__ (unc )
243
+ def _get_uncshare (self ):
244
+ unc , r = self . module .splitunc (self )
245
+ return self .__class__ (unc )
230
246
231
- uncshare = property (
232
- _get_uncshare , None , None ,
233
- """ The UNC mount point for this path.
234
- This is empty for paths on local drives. """ )
247
+ uncshare = property (
248
+ _get_uncshare , None , None ,
249
+ """ The UNC mount point for this path.
250
+ This is empty for paths on local drives. """ )
235
251
236
252
def joinpath (self , * args ):
237
253
""" Join two or more path components, adding a separator
238
254
character (os.sep) if needed. Returns a new path
239
255
object.
240
256
"""
241
- return self .__class__ (os . path .join (self , * args ))
257
+ return self .__class__ (self . module .join (self , * args ))
242
258
243
259
def splitall (self ):
244
260
r""" Return a list of the path components in this path.
@@ -283,14 +299,14 @@ def relpathto(self, dest):
283
299
# Don't normcase dest! We want to preserve the case.
284
300
dest_list = dest .splitall ()
285
301
286
- if orig_list [0 ] != os . path .normcase (dest_list [0 ]):
302
+ if orig_list [0 ] != self . module .normcase (dest_list [0 ]):
287
303
# Can't get here from there.
288
304
return dest
289
305
290
306
# Find the location where the two paths start to differ.
291
307
i = 0
292
308
for start_seg , dest_seg in zip (orig_list , dest_list ):
293
- if start_seg != os . path .normcase (dest_seg ):
309
+ if start_seg != self . module .normcase (dest_seg ):
294
310
break
295
311
i += 1
296
312
@@ -304,7 +320,7 @@ def relpathto(self, dest):
304
320
# If they happen to be identical, use os.curdir.
305
321
relpath = os .curdir
306
322
else :
307
- relpath = os . path .join (* segments )
323
+ relpath = self . module .join (* segments )
308
324
return self .__class__ (relpath )
309
325
310
326
# --- Listing, searching, walking, and matching
@@ -796,33 +812,31 @@ def read_hexhash(self, hash_name):
796
812
# (e.g. isdir on Windows, Python 3.2.2), and compiled functions don't get
797
813
# bound. Playing it safe and wrapping them all in method calls.
798
814
799
- def isabs (self ): return os . path .isabs (self )
800
- def exists (self ): return os . path .exists (self )
801
- def isdir (self ): return os . path .isdir (self )
802
- def isfile (self ): return os . path .isfile (self )
803
- def islink (self ): return os . path .islink (self )
804
- def ismount (self ): return os . path .ismount (self )
815
+ def isabs (self ): return self . module .isabs (self )
816
+ def exists (self ): return self . module .exists (self )
817
+ def isdir (self ): return self . module .isdir (self )
818
+ def isfile (self ): return self . module .isfile (self )
819
+ def islink (self ): return self . module .islink (self )
820
+ def ismount (self ): return self . module .ismount (self )
805
821
806
- if hasattr (os .path , 'samefile' ):
807
- def samefile (self ): return os .path .samefile (self )
822
+ def samefile (self ): return self .module .samefile (self )
808
823
809
- def getatime (self ): return os . path .getatime (self )
824
+ def getatime (self ): return self . module .getatime (self )
810
825
atime = property (
811
826
getatime , None , None ,
812
827
""" Last access time of the file. """ )
813
828
814
- def getmtime (self ): return os . path .getmtime (self )
829
+ def getmtime (self ): return self . module .getmtime (self )
815
830
mtime = property (
816
831
getmtime , None , None ,
817
832
""" Last-modified time of the file. """ )
818
833
819
- if hasattr (os .path , 'getctime' ):
820
- def getctime (self ): return os .path .getctime (self )
821
- ctime = property (
822
- getctime , None , None ,
823
- """ Creation time of the file. """ )
834
+ def getctime (self ): return self .module .getctime (self )
835
+ ctime = property (
836
+ getctime , None , None ,
837
+ """ Creation time of the file. """ )
824
838
825
- def getsize (self ): return os . path .getsize (self )
839
+ def getsize (self ): return self . module .getsize (self )
826
840
size = property (
827
841
getsize , None , None ,
828
842
""" Size of the file, in bytes. """ )
0 commit comments