Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/util/walk.py @ 123:4a0c3c9eead7
Use the output of "scandir()" directly if it has a "close" method.
Use a "nullcontext" otherwise.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 02 Jan 2025 12:43:40 +0100 |
| parents | 2dc26a2f3d1c |
| children | c7df81fb84b7 |
| 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 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
112 if not w._is_dir: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
113 # Do not supress errors here and (consistently) follow symlinks |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
114 w._stat_result = entry.stat(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
|
115 return w |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
116 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
117 @staticmethod |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
118 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
|
119 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
|
120 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
121 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
122 if scandir: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
123 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
124 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
|
125 """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
|
126 original: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
127 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
128 - 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
|
129 - 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
|
130 - 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
|
131 - 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
|
132 individual path segments |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
133 - all other yielded lists contain WalkDirEntry elements instead of |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
134 strings |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
135 - 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
|
136 - 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
|
137 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
138 .. 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 `topdown`). |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
144 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
145 """ |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
146 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
|
147 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
|
148 |
|
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 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
|
151 """: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
|
152 if top: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
153 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
|
154 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
155 path = root |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
156 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
157 dirs, nondirs, walk_dirs = [], [], [] |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
158 |
|
123
4a0c3c9eead7
Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents:
121
diff
changeset
|
159 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
|
160 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
|
161 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
|
162 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
|
163 while True: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
164 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
165 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
|
166 except StopIteration: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
167 break |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
168 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
|
169 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
|
170 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
171 nondirs.append(entry) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
172 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
173 # 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
|
174 # 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
|
175 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
176 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
|
177 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
|
178 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
|
179 else: |
|
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 = 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
|
181 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
|
182 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
|
183 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
184 # 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
|
185 walk_dirs.sort(key=WalkDirEntry.sort_key) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
186 dirs.sort(key=WalkDirEntry.sort_key) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
187 nondirs.sort(key=WalkDirEntry.sort_key) |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
188 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
189 # 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
|
190 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
|
191 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
|
192 # Yield after recursion if going bottom up |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
193 yield top, dirs, nondirs |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
194 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
195 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
196 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
197 raise ImportError("no `scandir()' module available") |
