annotate cutils/util/walk.py @ 156:481cc9b26861

Calculate "stat()" for directories also in a WalkDirEntry
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 06 Jan 2025 14:38:07 +0100
parents bf74ce3c968d
children 29dd5528174c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
2 # :-
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
3 # :Copyright: (c) 2020-2025 Franz Glasner
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
4 # :License: BSD-3-Clause
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
5 # :-
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
6 r"""Utility sub-module to implement a heavily customized :func:`os.walk`.
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
7
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
8 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
9
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
10 __all__ = ["walk"]
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
11
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
12
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
13 import os
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
14 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
15 from os import scandir
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
16 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
17 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
18 from scandir import scandir
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
19 except ImportError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
20 scandir = None
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
21
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
22 from .cm import nullcontext
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
23 from .constants import PY2
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
24
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
25
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
26 class WalkDirEntry(object):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
27
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
28 """A :class:`os.DirEntry` alike to be used in :func:`walk` and for
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
29 its results.
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
30
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
31 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
32
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
33 __slots__ = ("_name", "_fsname", "_path", "_fspath", "_is_symlink",
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
34 "_is_dir", "_stat_result")
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
35
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
36 def __init__(self, name):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
37 self._name = name
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
38 if PY2:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
39 assert isinstance(name, bytes)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
40 self._fsname = name
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
41 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
42 self._fsname = os.fsencode(name)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
43 self._path = None
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
44 self._fspath = None
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
45 self._is_symlink = self._is_dir = self._stat_result = None
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
46
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
47 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
48 def name(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
49 return self._name
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
50
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
51 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
52 def fsname(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
53 return self._fsname
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
54
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
55 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
56 def path(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
57 return self._path
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
58
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
59 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
60 def fspath(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
61 if self._path is not None:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
62 if self._fspath is None:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
63 if PY2:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
64 assert isinstance(self._path, bytes)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
65 self._fspath = self._path
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
66 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
67 self._fspath = os.fsencode(self._path)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
68 return self._fspath
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
69
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
70 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
71 def is_symlink(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
72 return self._is_symlink
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
73
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
74 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
75 def is_dir(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
76 return self._is_dir
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
77
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
78 @property
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
79 def stat(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
80 return self._stat_result
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
81
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
82 def __repr__(self):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
83 tag = ""
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
84 if self._is_symlink:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
85 tag += "l"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
86 if self._is_dir:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
87 tag += "d"
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
88 if tag:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
89 return "<WalkDirEntry %r (%s)>" % (self._name, tag)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
90 return "<WalkDirEntry %r>" % (self._name,)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
91
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
92 @classmethod
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
93 def from_direntry(cls_, entry):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
94 w = cls_(entry.name)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
95 w._path = entry.path
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
96 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
97 w._is_dir = entry.is_dir(follow_symlinks=True)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
98 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
99 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
100 # If is_dir() raises an OSError, consider that the entry
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
101 # is not a directory, same behaviour than os.path.isdir().
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
102 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
103 w._is_dir = False
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
104 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
105 w._is_symlink = entry.is_symlink()
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
106 except OSError:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
107 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
108 # If is_symlink() raises an OSError, consider that the entry
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
109 # is not a symbolic link, same behaviour than os.path.islink().
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
110 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
111 w._is_symlink = False
156
481cc9b26861 Calculate "stat()" for directories also in a WalkDirEntry
Franz Glasner <fzglas.hg@dom66.de>
parents: 155
diff changeset
112 # Do not supress errors here and (consistently) follow symlinks
481cc9b26861 Calculate "stat()" for directories also in a WalkDirEntry
Franz Glasner <fzglas.hg@dom66.de>
parents: 155
diff changeset
113 w._stat_result = entry.stat(follow_symlinks=True)
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
114 return w
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
115
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
116 @staticmethod
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
117 def sort_key(entry):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
118 return entry._fsname
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
119
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
120
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
121 if scandir:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
122
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
123 def walk(root, follow_symlinks=False):
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
124 """A heyvily customized :func:`os.walk` alike that differs from the
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
125 original:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
126
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
127 - optimized for use in :command:`treesum`
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
128 - most errors are not suppressed
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
129 - the `root` is never part of the returned data
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
130 - the returned directory in "top" is not a string form but a list of
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
131 individual path segments
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 154
diff changeset
132 - there is only one yielded list
154
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
133
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
134 * contains :class:`WalkDirEntry`
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
135 * sorted by its fsname
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
136
155
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 154
diff changeset
137 The caller can easily get the old dirs and nondirs by filtering
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 154
diff changeset
138 the yielded list using "entry.is_dir".
bf74ce3c968d When computing digests use the order imposed by names alone.
Franz Glasner <fzglas.hg@dom66.de>
parents: 154
diff changeset
139
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
140 - recurse into sub-directories first ("topdown=False")
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
141 - sort consistently all yielded lists by the filesystem encoding
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
142
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
143 .. note:: The implementation is based on Python 3.11 and needs a
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
144 functional :func:`os.scandir` or :func:`scandir.scandir`
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
145 implementation. It intentionally follows the logic in
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
146 Python 3.11 while it could be simplified because we are not
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
147 implementing some of the original flags (e.g. like
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
148 `topdown`).
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
149
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
150 """
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
151 normed_root = os.path.normpath(root)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
152 yield from _walk(normed_root, tuple(), follow_symlinks=follow_symlinks)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
153
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
154
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
155 def _walk(root, top, follow_symlinks): # noqa: E303 too many empty lines
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
156 """:func:`walk` helper"""
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
157 if top:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
158 path = os.path.join(root, *top)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
159 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
160 path = root
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
161
154
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
162 fsobjects, walk_dirs = [], []
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
163
123
4a0c3c9eead7 Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents: 121
diff changeset
164 scandir_cm = scandir(path)
4a0c3c9eead7 Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents: 121
diff changeset
165 if not hasattr(scandir_cm, "close"):
4a0c3c9eead7 Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents: 121
diff changeset
166 scandir_cm = nullcontext(scandir_cm)
4a0c3c9eead7 Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents: 121
diff changeset
167 with scandir_cm as scandir_it:
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
168 while True:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
169 try:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
170 entry = WalkDirEntry.from_direntry(next(scandir_it))
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
171 except StopIteration:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
172 break
154
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
173 fsobjects.append(entry)
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
174 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
175 # Always bottom-up: recurse into sub-directories, but exclude
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
176 # symlinks to directories if follow_symlinks is False
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
177 #
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
178 if entry.is_dir:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
179 if follow_symlinks:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
180 walk_into = True
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
181 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
182 walk_into = not entry.is_symlink
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
183 if walk_into:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
184 walk_dirs.append(entry)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
185
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
186 # Sort by low-level filesystem encoding
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
187 walk_dirs.sort(key=WalkDirEntry.sort_key)
154
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
188 fsobjects.sort(key=WalkDirEntry.sort_key)
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
189
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
190 # Recurse into sub-directories
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
191 for wd in walk_dirs:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
192 yield from _walk(root, top + (wd.name,), follow_symlinks)
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
193 # Yield after recursion if going bottom up
154
c7df81fb84b7 Merge dirs and nondirs into one list that is yielded from "util.walk()"
Franz Glasner <fzglas.hg@dom66.de>
parents: 123
diff changeset
194 yield top, fsobjects
121
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
195
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
196 else:
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
197
2dc26a2f3d1c A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff changeset
198 raise ImportError("no `scandir()' module available")