Skip to content

Commit

Permalink
walker_class attribute for filesystem (#119)
Browse files Browse the repository at this point in the history
* added walker_class attribute to FS; overrode walk property in WrapFS to use same walker as wrapped fs uses

* used assertion from six module to run tets both on python 2 and 3
  • Loading branch information
fresheed authored and willmcgugan committed Dec 21, 2017
1 parent ee3e335 commit 699d899
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
5 changes: 4 additions & 1 deletion fs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class FS(object):

# This is the "standard" meta namespace.
_meta = {}

# most FS will use default walking algorithms
walker_class=Walker

def __init__(self):
"""Create a filesystem. See help(type(self)) for accurate signature.
Expand All @@ -65,7 +68,7 @@ def __exit__(self, exc_type, exc_value, traceback):
def walk(self):
"""`~fs.walk.BoundWalker`: a walker bound to this filesystem.
"""
return Walker.bind(self)
return self.walker_class.bind(self)

# ---------------------------------------------------------------- #
# Required methods #
Expand Down
5 changes: 5 additions & 0 deletions fs/wrapfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,8 @@ def validatepath(self, path):
_fs.validatepath(_path)
path = abspath(normpath(path))
return path

@property
def walk(self):
return self._wrap_fs.walker_class.bind(self)

45 changes: 45 additions & 0 deletions tests/test_walk.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from fs.errors import FSError
from fs.memoryfs import MemoryFS
from fs import walk
from fs.wrap import read_only
import six


class TestWalkerBase(unittest.TestCase):
Expand Down Expand Up @@ -196,3 +198,46 @@ def broken_scandir(path, namespaces=None):
def test_on_error_invalid(self):
with self.assertRaises(TypeError):
walk.Walker(on_error='nope')

def test_subdir_uses_same_walker(self):
class CustomWalker(walk.Walker):

@classmethod
def bind(cls, fs):
return walk.BoundWalker(fs, walker_class=CustomWalker)

class CustomizedMemoryFS(MemoryFS):
walker_class=CustomWalker

base_fs=CustomizedMemoryFS()
base_fs.settext("a", "a")
base_fs.makedirs("b")
base_fs.settext("b/c", "c")
base_fs.settext("b/d", "d")
base_walker=base_fs.walk
self.assertEqual(base_walker.walker_class, CustomWalker)
six.assertCountEqual(self, ["/a", "/b/c", "/b/d"], base_walker.files())

sub_fs=base_fs.opendir("b")
sub_walker=sub_fs.walk
self.assertEqual(sub_walker.walker_class, CustomWalker)
six.assertCountEqual(self, ["/c", "/d"], sub_walker.files())

def test_readonly_wrapper_uses_same_walker(self):
class CustomWalker(walk.Walker):

@classmethod
def bind(cls, fs):
return walk.BoundWalker(fs, walker_class=CustomWalker)

class CustomizedMemoryFS(MemoryFS):
walker_class=CustomWalker

base_fs=CustomizedMemoryFS()
base_walker=base_fs.walk
self.assertEqual(base_walker.walker_class, CustomWalker)

readonly_fs=read_only(CustomizedMemoryFS())
readonly_walker=readonly_fs.walk
self.assertEqual(readonly_walker.walker_class, CustomWalker)

0 comments on commit 699d899

Please sign in to comment.