Commit e78439de by rfkelly0

Add utils.isdir(fs,path,info) and utils.isfile(fs,path.info).

These helper functions can often tell the type of a path by inspecting the
info dict (e.g. checking flags in st_mode) and can thus avoid an additional
query to the filesystem.  They fall back to calling fs.isdir() or fs.isfile()
if the info dict doesn't give any clues.
parent 78c7dd80
...@@ -56,4 +56,7 @@ ...@@ -56,4 +56,7 @@
* MountFS: added support for mounting at the root directory, and for * MountFS: added support for mounting at the root directory, and for
mounting over an existing mount. mounting over an existing mount.
* Added 'getpathurl' and 'haspathurl' methods * Added 'getpathurl' and 'haspathurl' methods
* Added utils.isdir(fs,path,info) and utils.isfile(fs,path,info); these
can often determine whether a path is a file or directory by inspecting
the info dict and avoid an additional query to the filesystem.
...@@ -15,6 +15,7 @@ __all__ = ['copyfile', ...@@ -15,6 +15,7 @@ __all__ = ['copyfile',
import shutil import shutil
import os import os
import sys import sys
import stat
from fs.mountfs import MountFS from fs.mountfs import MountFS
from fs.path import pathjoin, pathsplit from fs.path import pathjoin, pathsplit
from fs.errors import DestinationExistsError from fs.errors import DestinationExistsError
...@@ -180,6 +181,38 @@ def countbytes(fs): ...@@ -180,6 +181,38 @@ def countbytes(fs):
return total return total
def isdir(fs,path,info=None):
"""Check whether a path within a filesystem is a directory.
If you're able to provide the info dict for the path, this may be possible
without querying the filesystem (e.g. by checking st_mode).
"""
if info is not None:
st_mode = info.get("st_mode")
if st_mode:
if stat.S_ISDIR(st_mode):
return True
if stat.S_ISREG(st_mode):
return False
return fs.isdir(path)
def isfile(fs,path,info=None):
"""Check whether a path within a filesystem is a file.
If you're able to provide the info dict for the path, this may be possible
without querying the filesystem (e.g. by checking st_mode).
"""
if info is not None:
st_mode = info.get("st_mode")
if st_mode:
if stat.S_ISREG(st_mode):
return True
if stat.S_ISDIR(st_mode):
return False
return fs.isfile(path)
def find_duplicates(fs, def find_duplicates(fs,
compare_paths=None, compare_paths=None,
quick=False, quick=False,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment