Skip to content

Commit

Permalink
Make scandir an optional dependency (#44)
Browse files Browse the repository at this point in the history
* Remove requirement for scandir

* Test both OSFS.scandir methods

* Avoid duplicating OSFS tests

* Refactor OSFS._scandir and cache os.stat calls

* Add a new environment to tox.ini without scandir
  • Loading branch information
althonos authored and willmcgugan committed May 19, 2017
1 parent d8cad45 commit 9990a1e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
91 changes: 64 additions & 27 deletions fs/osfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
try:
from os import scandir
except ImportError:
from scandir import scandir
try:
from scandir import scandir
except ImportError:
scandir = None

from . import errors
from .errors import FileExists
Expand Down Expand Up @@ -363,35 +366,69 @@ def setinfo(self, path, info):
with convert_os_errors('setinfo', path):
os.utime(sys_path, (accessed, modified))

def _scandir(self, path, namespaces=None):
self.check()
namespaces = namespaces or ()
_path = self.validatepath(path)
sys_path = self._to_sys_path(_path)
with convert_os_errors('scandir', path, directory=True):
for dir_entry in scandir(sys_path):
info = {
"basic": {
"name": dir_entry.name,
"is_dir": dir_entry.is_dir()

if scandir:
def _scandir(self, path, namespaces=None):
self.check()
namespaces = namespaces or ()
_path = self.validatepath(path)
sys_path = self._to_sys_path(_path)
with convert_os_errors('scandir', path, directory=True):
for dir_entry in scandir(sys_path):
info = {
"basic": {
"name": dir_entry.name,
"is_dir": dir_entry.is_dir()
}
}
}
if 'details' in namespaces:
stat_result = dir_entry.stat()
info['details'] = \
self._make_details_from_stat(stat_result)
if 'stat' in namespaces:
stat_result = dir_entry.stat()
info['stat'] = {
k: getattr(stat, k)
for k in dir(stat) if k.startswith('st_')
if 'details' in namespaces:
stat_result = dir_entry.stat()
info['details'] = \
self._make_details_from_stat(stat_result)
if 'stat' in namespaces:
stat_result = dir_entry.stat()
info['stat'] = {
k: getattr(stat, k)
for k in dir(stat) if k.startswith('st_')
}
if 'access' in namespaces:
stat_result = dir_entry.stat()
info['access'] =\
self._make_access_from_stat(stat_result)

yield Info(info)

else:

def _scandir(self, path, namespaces=None):
self.check()
namespaces = namespaces or ()
_path = self.validatepath(path)
sys_path = self._to_sys_path(_path)
with convert_os_errors('scandir', path, directory=True):
for entry_name in os.listdir(sys_path):
entry_path = os.path.join(sys_path, entry_name)
stat_result = os.stat(entry_path)
info = {
"basic": {
"name": entry_name,
"is_dir": stat.S_ISDIR(stat_result.st_mode),
}
}
if 'access' in namespaces:
stat_result = dir_entry.stat()
info['access'] =\
self._make_access_from_stat(stat_result)
if 'details' in namespaces:
info['details'] = \
self._make_details_from_stat(stat_result)
if 'stat' in namespaces:
info['stat'] = {
k: getattr(stat, k)
for k in dir(stat) if k.startswith('st_')
}
if 'access' in namespaces:
info['access'] =\
self._make_access_from_stat(stat_result)

yield Info(info)

yield Info(info)

def scandir(self, path, namespaces=None, page=None):
iter_info = self._scandir(path, namespaces=namespaces)
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
appdirs==1.4.0
enum34==1.1.6 ; python_version < '3.4'
pytz
scandir==1.3 ; python_version < '3.5'
setuptools
six==1.10.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
description="Python's filesystem abstraction layer",
install_requires=REQUIREMENTS,
extras_require={
":python_version < '3.5'": ['scandir~=1.5'],
"scandir :python_version < '3.5'": ['scandir~=1.5'],
":python_version < '3.4'": ['enum34~=1.1.6']
},
license="BSD",
Expand Down
8 changes: 8 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ deps = appdirs
commands = nosetests tests -v \
[]

[testenv:pure-python]
deps = appdirs
nose
mock
pyftpdlib
pytz
commands = nosetests tests -v \
[]

0 comments on commit 9990a1e

Please sign in to comment.