[Python-checkins] python/nondist/sandbox/path path.py,1.8,1.9
birkenfeld@users.sourceforge.net
birkenfeld at users.sourceforge.net
Tue Aug 2 16:00:01 CEST 2005
Update of /cvsroot/python/python/nondist/sandbox/path
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3627
Modified Files:
path.py
Log Message:
Use self.__class__ instead of hardcoded Path.
Index: path.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/path/path.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- path.py 30 Jul 2005 12:14:14 -0000 1.8
+++ path.py 2 Aug 2005 13:59:58 -0000 1.9
@@ -59,21 +59,21 @@
The argument can be either a string or an existing Path object.
"""
if not args:
- return Path(os.curdir)
+ return typ(os.curdir)
for arg in args:
if not isinstance(arg, basestring):
- raise ValueError("Path() arguments must be Path, str or unicode")
+ raise ValueError("%s() arguments must be Path, str or unicode" % typ.__name__)
if len(args) == 1:
return _base.__new__(typ, *args)
else:
- return Path(os.path.join(*args))
+ return typ(os.path.join(*args))
# Iterating over a string yields its parts
def __iter__(self):
return iter(self.parts())
def __repr__(self):
- return 'Path(%s)' % repr(_base(self))
+ return '%s(%r)' % (self.__class__.__name__, _base(self))
def base(self):
return _base(self)
@@ -82,12 +82,12 @@
# Caution: this is not a join!
def __add__(self, other):
if isinstance(other, basestring):
- return Path(_base(self) + other)
+ return self.__class__(_base(self) + other)
return NotImplemented
def __radd__(self, other):
if isinstance(other, basestring):
- return Path(other + _base(self))
+ return self.__class__(other + _base(self))
return NotImplemented
# The / joins paths
@@ -98,13 +98,13 @@
# Alternative constructor.
- @staticmethod
- def cwd():
+ @classmethod
+ def cwd(cls):
""" Return the current working directory as a path object. """
if os.path.supports_unicode_filenames:
- return Path(os.getcwdu())
+ return cls(os.getcwdu())
else:
- return Path(os.getcwd())
+ return cls(os.getcwd())
# --- Operations which return strings
@@ -113,7 +113,7 @@
os.path.basename, None, None,
""" The name of this file or directory without the full path.
- For example, path('/usr/local/lib/libpython.so').basename == 'libpython.so'
+ For example, Path('/usr/local/lib/libpython.so').basename == 'libpython.so'
""")
def _get_namebase(self):
@@ -132,8 +132,8 @@
_get_namebase, None, None,
""" The same as Path.basename, but with one file extension stripped off.
- For example, path('/home/guido/python.tar.gz').basename == 'python.tar.gz',
- but path('/home/guido/python.tar.gz').namebase == 'python.tar'
+ For example, Path('/home/guido/python.tar.gz').basename == 'python.tar.gz',
+ but Path('/home/guido/python.tar.gz').namebase == 'python.tar'
""")
ext = property(
@@ -149,22 +149,22 @@
# --- Operations which return Path objects
def abspath(self):
- return Path(os.path.abspath(self))
+ return self.__class__(os.path.abspath(self))
def normcase(self):
- return Path(os.path.normcase(self))
+ return self.__class__(os.path.normcase(self))
def normpath(self):
- return Path(os.path.normpath(self))
+ return self.__class__(os.path.normpath(self))
def realpath(self):
- return Path(os.path.realpath(self))
+ return self.__class__(os.path.realpath(self))
def expanduser(self):
- return Path(os.path.expanduser(self))
+ return self.__class__(os.path.expanduser(self))
def expandvars(self):
- return Path(os.path.expandvars(self))
+ return self.__class__(os.path.expandvars(self))
def expand(self):
""" Clean up a filename by calling expandvars(),
@@ -176,7 +176,7 @@
return self.expandvars().expanduser().normpath()
def _get_directory(self):
- return Path(os.path.dirname(self))
+ return self.__class__(os.path.dirname(self))
directory = property(
_get_directory, None, None,
@@ -198,7 +198,7 @@
def splitpath(self):
""" p.splitpath() -> Return (p.directory, p.basename). """
parent, child = os.path.split(self)
- return Path(parent), child
+ return self.__class__(parent), child
def splitdrive(self):
""" p.splitdrive() -> Return (Path(p.drive), <the rest of p>).
@@ -208,7 +208,7 @@
is simply (Path(''), p). This is always the case on Unix.
"""
drive, rel = os.path.splitdrive(self)
- return Path(drive), rel
+ return self.__class__(drive), rel
def splitext(self):
""" p.splitext() -> Return (p.stripext(), p.ext).
@@ -220,16 +220,16 @@
last path segment.
"""
filename, ext = os.path.splitext(self)
- return Path(filename), ext
+ return self.__class__(filename), ext
if hasattr(os.path, 'splitunc'):
def splitunc(self):
unc, rest = os.path.splitunc(self)
- return Path(unc), rest
+ return self.__class__(unc), rest
def _get_uncshare(self):
unc, r = os.path.splitunc(self)
- return Path(unc)
+ return self.__class__(unc)
uncshare = property(
_get_uncshare, None, None,
@@ -241,7 +241,7 @@
character (os.sep) if needed. Returns a new path
object.
"""
- return Path(os.path.join(self, *args))
+ return self.__class__(os.path.join(self, *args))
joinpath = joinwith
@@ -271,7 +271,7 @@
""" Return this path as a relative path,
based from the current working directory.
"""
- return Path.cwd().relpathto(self)
+ return self.__class__.cwd().relpathto(self)
def relpathto(self, dest):
""" Return a relative path from self to dest.
@@ -281,7 +281,7 @@
dest.abspath().
"""
origin = self.abspath()
- dest = Path(dest).abspath()
+ dest = self.__class__(dest).abspath()
orig_list = origin.normcase().parts()
# Don't normcase dest! We want to preserve the case.
@@ -306,15 +306,15 @@
segments += dest_list[i:]
if len(segments) == 0:
# If they happen to be identical, use os.curdir.
- return Path(os.curdir)
+ return self.__class__(os.curdir)
else:
- return Path(os.path.join(*segments))
+ return self.__class__(os.path.join(*segments))
# --- Listing, searching, walking, and matching
def listdir(self):
- return [Path(p) for p in os.listdir(self)]
+ return [self.__class__(p) for p in os.listdir(self)]
def children(self, pattern=None):
""" D.children() -> List of items in this directory,
@@ -422,7 +422,7 @@
For example, path('/users').glob('*/bin/*') returns a list
of all the files users have in their bin directories.
"""
- return map(Path, glob.glob(self / pattern))
+ return map(self.__class__, glob.glob(self / pattern))
# --- Reading or writing an entire file at once.
@@ -799,7 +799,7 @@
The result may be an absolute or a relative path.
"""
- return Path(os.readlink(self))
+ return self.__class__(os.readlink(self))
def readlinkabs(self):
""" Return the path to which this symbolic link points.
@@ -833,4 +833,7 @@
if hasattr(os, 'startfile'):
def startfile(self):
os.startfile(self)
-
+
+ if hasattr(os, 'chdir'):
+ def chdir(self):
+ os.chdir(self)
More information about the Python-checkins
mailing list