Skip to content

Commit e338d86

Browse files
committed
redesign of walker objects
1 parent cdd9571 commit e338d86

File tree

6 files changed

+248
-198
lines changed

6 files changed

+248
-198
lines changed

docs/source/reference/walk.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Walk a directory structure.
55

66
The ``Walker`` class in this module does the work of *walking* a filesystem. In other words, listing each resource in a directory, and any sub-directories.
77

8-
To walk a filesystem (or directory) you can construct a :class::`fs.walk.Walker` object and use its methods to do the walking. Here's an example that prints the path to every Python file in your projects directory::
8+
To walk a filesystem (or directory) you can construct a :class:`fs.walk.Walker` object and use its methods to do the walking. Here's an example that prints the path to every Python file in your projects directory::
99

1010
>>> from fs import open_fs
1111
>>> from fs.walk import Walker
@@ -32,4 +32,4 @@ There are two general algorithms for searching a directory tree. The first metho
3232
Generally speaking, you will only need the a *depth* search if you will be deleting resources as you walk through them. The default *breadth* search is a generally more efficient way of looking through a filesystem. You can specify which method you want with the ``search`` parameter on most ``Walker`` methods.
3333

3434
.. automodule:: fs.walk
35-
:members:
35+
:members:

fs/_repr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from __future__ import unicode_literals
2+
3+
def make_repr(class_name, args):
4+
"""Generate a repr string."""
5+
arguments = [
6+
"{!s}={!r}".format(name, value)
7+
for name, value, default in args
8+
if value != default
9+
]
10+
return "{}({})".format(class_name, ', '.join(arguments))

fs/base.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,12 +834,8 @@ def removetree(self, dir_path):
834834

835835
_dir_path = abspath(normpath(dir_path))
836836
with self._lock:
837-
walker = walk.Walker()
838-
gen_info = walker.walk_info(
839-
self,
840-
_dir_path,
841-
search="depth"
842-
)
837+
walker = walk.Walker(search="depth")
838+
gen_info = walker.walk_info(self, _dir_path)
843839
for _path, info in gen_info:
844840
if info.is_dir:
845841
self.removedir(_path)

fs/copy.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from __future__ import print_function
22
from __future__ import unicode_literals
33

4-
from . import walk
4+
from .walk import Walker
55
from .opener import manage_fs
66
from .path import abspath
77
from .path import combine
88
from .path import frombase
99
from .path import normpath
1010

1111

12-
def copy_fs(src_fs, dst_fs):
12+
def copy_fs(src_fs, dst_fs, walker=None):
1313
"""
1414
Copy the contents of one filesystem to another.
1515
@@ -21,7 +21,7 @@ def copy_fs(src_fs, dst_fs):
2121
:type dst_fs: FS URL or instance
2222
2323
"""
24-
copy_dir(src_fs, '/', dst_fs, '/')
24+
copy_dir(src_fs, '/', dst_fs, '/', walker=walker)
2525

2626

2727
def copy_file(src_fs, src_path, dst_fs, dst_path):
@@ -51,7 +51,7 @@ def copy_file(src_fs, src_path, dst_fs, dst_path):
5151
dst_fs.setbin(dst_path, read_file)
5252

5353

54-
def copy_structure(src_fs, dst_fs):
54+
def copy_structure(src_fs, dst_fs, walker=None):
5555
"""
5656
Copy directories (but not files) from ``src_fs`` to ``dst_fs``.
5757
@@ -61,14 +61,15 @@ def copy_structure(src_fs, dst_fs):
6161
:type dst_fs: FS URL or instance
6262
6363
"""
64+
walker = walker or Walker()
6465
with manage_fs(src_fs, writeable=False) as src_fs:
6566
with manage_fs(dst_fs, create=True) as dst_fs:
6667
with src_fs.lock(), dst_fs.lock():
67-
for dir_path in walk.walk_dirs(src_fs):
68+
for dir_path in walker.walk_dirs(src_fs):
6869
dst_fs.makedir(dir_path, recreate=True)
6970

7071

71-
def copy_dir(src_fs, src_path, dst_fs, dst_path):
72+
def copy_dir(src_fs, src_path, dst_fs, dst_path, walker=None):
7273
"""
7374
Copy a directory from one filesystem to another.
7475
@@ -82,14 +83,15 @@ def copy_dir(src_fs, src_path, dst_fs, dst_path):
8283
8384
"""
8485

86+
walker = walker or Walker()
8587
_src_path = abspath(normpath(src_path))
8688
_dst_path = abspath(normpath(dst_path))
8789

8890
with manage_fs(src_fs, writeable=False) as src_fs:
8991
with manage_fs(dst_fs, create=True) as dst_fs:
9092
with src_fs.lock(), dst_fs.lock():
9193
dst_fs.makedir(_dst_path, recreate=True)
92-
for dir_path, dirs, files in walk.walk(src_fs, _src_path):
94+
for dir_path, dirs, files in walker.walk(src_fs, _src_path):
9395
copy_path = combine(
9496
_dst_path,
9597
frombase(_src_path, dir_path)

0 commit comments

Comments
 (0)