Skip to content

Commit

Permalink
Add faster scandir package for iterating over files/folders when …
Browse files Browse the repository at this point in the history
…platform is Python < 3.5.

Refs dgilland#2
  • Loading branch information
dgilland committed Oct 19, 2015
1 parent d2f182f commit ecb5f56
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ Changelog
=========


v0.6.0 (2015-10-19)
-------------------

- Add faster ``scandir`` package for iterating over files/folders when platform is Python < 3.5. Scandir implementation was added to ``os`` module starting with Python 3.5.


v0.5.0 (2015-07-02)
-------------------

Expand Down
12 changes: 12 additions & 0 deletions hashfs/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@

import sys

try:
# Python >= 3.5.
from os import scandir, walk
except ImportError:
try:
# Back ported scandir package.
from scandir import scandir, walk
except ImportError:
# Back ported package not installed so fallback to baseline.
from os import walk
scandir = None


PY3 = sys.version_info[0] == 3

Expand Down
6 changes: 3 additions & 3 deletions hashfs/hashfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tempfile import NamedTemporaryFile

from .utils import issubdir, shard
from ._compat import to_bytes
from ._compat import to_bytes, walk


class HashFS(object):
Expand Down Expand Up @@ -162,15 +162,15 @@ def files(self):
"""Return generator that yields all files in the :attr:`root`
directory.
"""
for folder, subfolders, files in os.walk(self.root):
for folder, subfolders, files in walk(self.root):
for file in files:
yield os.path.abspath(os.path.join(folder, file))

def folders(self):
"""Return generator that yields all folders in the :attr:`root`
directory that contain files.
"""
for folder, subfolders, files in os.walk(self.root):
for folder, subfolders, files in walk(self.root):
if files:
yield folder

Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def read(fname):
changes = read('CHANGES.rst')


if sys.version_info < (3, 5):
# Install back port of faster os.walk/scandir implementation.
meta['__install_requires__'].append('scandir>=1.1')


class Tox(TestCommand):
user_options = [
('tox-args=', 'a', "Arguments to pass to tox")
Expand Down

0 comments on commit ecb5f56

Please sign in to comment.