Mercurial > hgrepos > Python > apps > py-cutils
changeset 154:c7df81fb84b7
Merge dirs and nondirs into one list that is yielded from "util.walk()"
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 06 Jan 2025 13:38:09 +0100 |
| parents | 3505406ef9f3 |
| children | bf74ce3c968d |
| files | cutils/util/walk.py |
| diffstat | 1 files changed, 9 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/cutils/util/walk.py Sat Jan 04 15:41:00 2025 +0100 +++ b/cutils/util/walk.py Mon Jan 06 13:38:09 2025 +0100 @@ -130,8 +130,11 @@ - the `root` is never part of the returned data - the returned directory in "top" is not a string form but a list of individual path segments - - all other yielded lists contain WalkDirEntry elements instead of - strings + - there is only one yields list + + * contains :class:`WalkDirEntry` + * sorted by its fsname + - recurse into sub-directories first ("topdown=False") - sort consistently all yielded lists by the filesystem encoding @@ -154,7 +157,7 @@ else: path = root - dirs, nondirs, walk_dirs = [], [], [] + fsobjects, walk_dirs = [], [] scandir_cm = scandir(path) if not hasattr(scandir_cm, "close"): @@ -165,10 +168,7 @@ entry = WalkDirEntry.from_direntry(next(scandir_it)) except StopIteration: break - if entry.is_dir: - dirs.append(entry) - else: - nondirs.append(entry) + fsobjects.append(entry) # # Always bottom-up: recurse into sub-directories, but exclude # symlinks to directories if follow_symlinks is False @@ -183,14 +183,13 @@ # Sort by low-level filesystem encoding walk_dirs.sort(key=WalkDirEntry.sort_key) - dirs.sort(key=WalkDirEntry.sort_key) - nondirs.sort(key=WalkDirEntry.sort_key) + fsobjects.sort(key=WalkDirEntry.sort_key) # Recurse into sub-directories for wd in walk_dirs: yield from _walk(root, top + (wd.name,), follow_symlinks) # Yield after recursion if going bottom up - yield top, dirs, nondirs + yield top, fsobjects else:
