diff options
author | Dmitry Paramonov <asmatic075@gmail.com> | 2015-10-05 13:08:54 +0300 |
---|---|---|
committer | Dmitry Paramonov <asmatic075@gmail.com> | 2015-10-05 13:08:54 +0300 |
commit | 8a3d69c8eac1b3d63e91a9b50cdab5957c2dd01d (patch) | |
tree | afbc401c46cb4c3d8d89f9f8589628e64c490516 | |
parent | ab5fb6080d164ba5418b014145175b7c5b569542 (diff) | |
parent | 9d9440e5544abe9742e23e41697f1e0d45f6d86e (diff) | |
download | pyfilesystem-git-8a3d69c8eac1b3d63e91a9b50cdab5957c2dd01d.tar.gz |
Merge github.com:PyFilesystem/pyfilesystem
-rw-r--r-- | CHANGES.txt | 2 | ||||
-rw-r--r-- | fs/__init__.py | 2 | ||||
-rw-r--r-- | fs/base.py | 14 | ||||
-rw-r--r-- | fs/iotools.py | 4 | ||||
-rw-r--r-- | fs/osfs/__init__.py | 30 | ||||
-rw-r--r-- | setup.py | 2 |
6 files changed, 40 insertions, 14 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index e67eac5..6e68a86 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -100,5 +100,7 @@ 0.5.3: + * Implemented scandir in listdir if available + * Fix for issue where local.getpreferredencoding returns empty string diff --git a/fs/__init__.py b/fs/__init__.py index c250eac..2ddb779 100644 --- a/fs/__init__.py +++ b/fs/__init__.py @@ -15,7 +15,7 @@ implementations of this interface such as: """ -__version__ = "0.5.3a0" +__version__ = "0.5.4a1" __author__ = "Will McGugan (will@willmcgugan.com)" # provide these by default so people can use 'fs.path.basename' etc. @@ -1008,14 +1008,14 @@ class FS(object): paths = [] paths_append = paths.append try: - for filename in listdir(current_path): + for filename in listdir(current_path, dirs_only=True): path = pathcombine(current_path, filename) - if isdir(path): - if dir_wildcard(path): - dirs_append(path) - else: - if wildcard(filename): - paths_append(filename) + if dir_wildcard(path): + dirs_append(path) + for filename in listdir(current_path, files_only=True): + path = pathcombine(current_path, filename) + if wildcard(filename): + paths_append(filename) except ResourceNotFoundError: # Could happen if another thread / process deletes something whilst we are walking pass diff --git a/fs/iotools.py b/fs/iotools.py index bf91ec3..75197ec 100644 --- a/fs/iotools.py +++ b/fs/iotools.py @@ -160,7 +160,7 @@ def make_stream(name, if not binary: io_object = io.TextIOWrapper(io_object, - encoding=encoding, + encoding=encoding or 'utf-8', errors=errors, newline=newline, line_buffering=line_buffering,) @@ -170,7 +170,7 @@ def make_stream(name, def decode_binary(data, encoding=None, errors=None, newline=None): """Decode bytes as though read from a text file""" - return io.TextIOWrapper(io.BytesIO(data), encoding=encoding, errors=errors, newline=newline).read() + return io.TextIOWrapper(io.BytesIO(data), encoding=encoding or 'utf-8', errors=errors, newline=newline).read() def make_bytes_io(data, encoding=None, errors=None): diff --git a/fs/osfs/__init__.py b/fs/osfs/__init__.py index 4153f08..446ef93 100644 --- a/fs/osfs/__init__.py +++ b/fs/osfs/__init__.py @@ -23,6 +23,15 @@ import platform import io import shutil +scandir = None +try: + scandir = os.scandir +except AttributeError: + try: + from scandir import scandir + except ImportError: + pass + from fs.base import * from fs.path import * from fs.errors import * @@ -248,9 +257,24 @@ class OSFS(OSFSXAttrMixin, OSFSWatchMixin, FS): def listdir(self, path="./", wildcard=None, full=False, absolute=False, dirs_only=False, files_only=False): _decode_path = self._decode_path sys_path = self.getsyspath(path) - listing = os.listdir(sys_path) - paths = [_decode_path(p) for p in listing] - return self._listdir_helper(path, paths, wildcard, full, absolute, dirs_only, files_only) + + if scandir is None: + listing = os.listdir(sys_path) + paths = [_decode_path(p) for p in listing] + return self._listdir_helper(path, paths, wildcard, full, absolute, dirs_only, files_only) + else: + if dirs_only and files_only: + raise ValueError("dirs_only and files_only can not both be True") + # Use optimized scandir if present + scan = scandir(sys_path) + if dirs_only: + paths = [_decode_path(dir_entry.name) for dir_entry in scan if dir_entry.is_dir()] + elif files_only: + paths = [_decode_path(dir_entry.name) for dir_entry in scan if dir_entry.is_file()] + else: + paths = [_decode_path(dir_entry.name) for dir_entry in scan] + + return self._listdir_helper(path, paths, wildcard, full, absolute, False, False) @convert_os_errors def makedir(self, path, recursive=False, allow_recreate=False): @@ -4,7 +4,7 @@ from setuptools import setup import sys PY3 = sys.version_info >= (3,) -VERSION = "0.5.3a0" +VERSION = "0.5.4a1" COMMANDS = ['fscat', 'fsinfo', |