From 39fe88308695c97ddc59266e1aa05c62f372e5fa Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 26 Aug 2015 17:25:36 +0100 Subject: Use scandir if available --- CHANGES.txt | 2 ++ fs/base.py | 14 +++++++------- fs/osfs/__init__.py | 30 +++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index e67eac5..9bfe145 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -100,5 +100,7 @@ 0.5.3: + * Implemented scandir in listdir if available + diff --git a/fs/base.py b/fs/base.py index 04440eb..8785bb7 100644 --- a/fs/base.py +++ b/fs/base.py @@ -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/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): -- cgit v1.2.1 From 9d9440e5544abe9742e23e41697f1e0d45f6d86e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Mon, 7 Sep 2015 10:21:18 +0100 Subject: version bump --- CHANGES.txt | 2 +- fs/__init__.py | 2 +- fs/iotools.py | 4 ++-- setup.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 9bfe145..6e68a86 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -101,6 +101,6 @@ 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. 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/setup.py b/setup.py index f9864cc..aafbf67 100644 --- a/setup.py +++ b/setup.py @@ -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', -- cgit v1.2.1