diff options
author | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2010-10-19 12:42:56 +0000 |
---|---|---|
committer | rfkelly0 <rfkelly0@67cdc799-7952-0410-af00-57a81ceafa0f> | 2010-10-19 12:42:56 +0000 |
commit | e78439de6551e8366afce253760ffe2e0631c0bb (patch) | |
tree | 6a401a9ce0b8c689a6cec9f5bb1cb880848bd135 /fs/utils.py | |
parent | 78c7dd809836894b6b5d5898584b52ac4e138574 (diff) | |
download | pyfilesystem-git-e78439de6551e8366afce253760ffe2e0631c0bb.tar.gz |
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.
Diffstat (limited to 'fs/utils.py')
-rw-r--r-- | fs/utils.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/fs/utils.py b/fs/utils.py index 0166659..fdca3e7 100644 --- a/fs/utils.py +++ b/fs/utils.py @@ -15,6 +15,7 @@ __all__ = ['copyfile', import shutil import os import sys +import stat from fs.mountfs import MountFS from fs.path import pathjoin, pathsplit from fs.errors import DestinationExistsError @@ -180,6 +181,38 @@ def countbytes(fs): 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, compare_paths=None, quick=False, |