Skip to content

Commit

Permalink
Added many more chainable methods per #7
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Nov 14, 2012
1 parent fe95177 commit c8fdf97
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions path.py
Expand Up @@ -911,13 +911,16 @@ def pathconf(self, name):
def utime(self, times):
""" Set the access and modified times of this file. """
os.utime(self, times)
return self

def chmod(self, mode):
os.chmod(self, mode)
return self

if hasattr(os, 'chown'):
def chown(self, uid, gid):
os.chown(self, uid, gid)
return self

def rename(self, new):
os.rename(self, new)
Expand Down Expand Up @@ -956,23 +959,27 @@ def makedirs_p(self, mode=0777):

def rmdir(self):
os.rmdir(self)
return self

def rmdir_p(self):
try:
self.rmdir()
except OSError, e:
if e.errno != errno.ENOTEMPTY and e.errno != errno.EEXIST:
raise
return self

def removedirs(self):
os.removedirs(self)
return self

def removedirs_p(self):
try:
self.removedirs()
except OSError, e:
if e.errno != errno.ENOTEMPTY and e.errno != errno.EEXIST:
raise
return self

# --- Modifying operations on files

Expand All @@ -987,31 +994,37 @@ def touch(self):

def remove(self):
os.remove(self)
return self

def remove_p(self):
try:
self.unlink()
except OSError, e:
if e.errno != errno.ENOENT:
raise
return self

def unlink(self):
os.unlink(self)
return self

def unlink_p(self):
self.remove_p()
return self

# --- Links

if hasattr(os, 'link'):
def link(self, newpath):
""" Create a hard link at 'newpath', pointing to this file. """
os.link(self, newpath)
return self.__class__(newpath)

if hasattr(os, 'symlink'):
def symlink(self, newlink):
""" Create a symbolic link at 'newlink', pointing here. """
os.symlink(self, newlink)
return self.__class__(newlink)

if hasattr(os, 'readlink'):
def readlink(self):
Expand Down Expand Up @@ -1051,6 +1064,7 @@ def rmtree_p(self):
except OSError, e:
if e.errno != errno.ENOENT:
raise
return self

#
# --- Special stuff from os
Expand All @@ -1062,3 +1076,4 @@ def chroot(self):
if hasattr(os, 'startfile'):
def startfile(self):
os.startfile(self)
return self

0 comments on commit c8fdf97

Please sign in to comment.