diff cutils/util/walk.py @ 178:dac26a2d9de5

Cleanup: remove non used walk-code
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 11 Jan 2025 21:18:53 +0100
parents 089c40240061
children 188de62caac6
line wrap: on
line diff
--- a/cutils/util/walk.py	Sat Jan 11 17:41:28 2025 +0100
+++ b/cutils/util/walk.py	Sat Jan 11 21:18:53 2025 +0100
@@ -7,8 +7,7 @@
 
 """
 
-__all__ = ["walk",
-           "ScanDir"]
+__all__ = ["ScanDir"]
 
 
 import os
@@ -21,7 +20,6 @@
         scandir = None
 
 from . import PY2
-from .cm import nullcontext
 
 
 class WalkDirEntry(object):
@@ -142,87 +140,9 @@
         return entry._fsname
 
 
-def walk(root, follow_symlinks=False):
-    """A heyvily customized :func:`os.walk` alike that differs from the
-    original:
-
-    - optimized for use in :command:`treesum`
-    - most errors are not suppressed
-    - 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
-    - there is only one yielded list
-
-      * contains :class:`WalkDirEntry`
-      * sorted by its fsname
-
-      The caller can easily get the old dirs and nondirs by filtering
-      the yielded list using "entry.is_dir".
-
-    - recurse into sub-directories first ("topdown=False")
-    - sort consistently all yielded lists by the filesystem encoding
-
-    .. note:: The implementation is based on Python 3.11 and needs a
-              functional :func:`os.scandir` or :func:`scandir.scandir`
-              implementation. It intentionally follows the logic in
-              Python 3.11 while it could be simplified because we are not
-              implementing some of the original flags (e.g. like
-              `topdown`).
-
-    """
-    normed_root = os.path.normpath(root)
-    yield from _walk(normed_root, tuple(), follow_symlinks=follow_symlinks)
-
-
 if scandir:
 
-    def _walk(root, top, follow_symlinks):
-        """:func:`walk` helper.
-
-        Implemented using :func:`os.scandir`.
-
-        """
-        if top:
-            path = os.path.join(root, *top)
-        else:
-            path = root
-
-        fsobjects, walk_dirs = [], []
-
-        scandir_cm = scandir(path)
-        if not hasattr(scandir_cm, "close"):
-            scandir_cm = nullcontext(scandir_cm)
-        with scandir_cm as scandir_it:
-            while True:
-                try:
-                    entry = WalkDirEntry.from_direntry(next(scandir_it))
-                except StopIteration:
-                    break
-                fsobjects.append(entry)
-                #
-                # Always bottom-up: recurse into sub-directories, but exclude
-                # symlinks to directories if follow_symlinks is False
-                #
-                if entry.is_dir:
-                    if follow_symlinks:
-                        walk_into = True
-                    else:
-                        walk_into = not entry.is_symlink
-                    if walk_into:
-                        walk_dirs.append(entry)
-
-        # Sort by low-level filesystem encoding
-        walk_dirs.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, fsobjects
-
-
-    class ScanDir(object):    # noqa: E303   too many blank lines
+    class ScanDir(object):
 
         """An :func:`os.scandir` wrapper that is always an iterator and
         a context manager.
@@ -253,47 +173,7 @@
 
 else:
 
-    def _walk(root, top, follow_symlinks):
-        """:func:`walk` helper.
-
-        Implemented using :func:`os.listdir`.
-
-        """
-        if top:
-            path = os.path.join(root, *top)
-        else:
-            path = root
-
-        fsobjects, walk_dirs = [], []
-
-        names = os.listdir(path)
-        for name in names:
-            entry = WalkDirEntry.from_path_name(path, name)
-            fsobjects.append(entry)
-            #
-            # Always bottom-up: recurse into sub-directories, but exclude
-            # symlinks to directories if follow_symlinks is False
-            #
-            if entry.is_dir:
-                if follow_symlinks:
-                    walk_into = True
-                else:
-                    walk_into = not entry.is_symlink
-                if walk_into:
-                    walk_dirs.append(entry)
-
-        # Sort by low-level filesystem encoding
-        walk_dirs.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, fsobjects
-
-
-    class ScanDir(object):    # noqa: E303   too many blank lines
+    class ScanDir(object):
 
         """An :func:`os.scandir` wrapper that is always an iterator and
         a context manager.