Mercurial > hgrepos > Python > apps > py-cutils
annotate cutils/util/walk.py @ 162:29dd5528174c
Implement walk._walk() using os.listdir() also.
Use it if no scandir is available.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Thu, 09 Jan 2025 13:36:41 +0100 |
| parents | 481cc9b26861 |
| children | a813094ae4f5 |
| 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 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
116 @classmethod |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
117 def from_path_name(cls_, path, name): |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
118 w = cls_(name) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
119 w._path = os.path.join(path, name) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
120 try: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
121 w._is_dir = os.path.isdir(w._path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
122 except OSError: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
123 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
124 # If is_dir() raises an OSError, consider that the entry |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
125 # is not a directory, same behaviour than os.path.isdir(). |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
126 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
127 w._is_dir = False |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
128 try: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
129 w._is_symlink = os.path.islink(w._path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
130 except OSError: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
131 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
132 # If is_symlink() raises an OSError, consider that the entry |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
133 # is not a symbolic link, same behaviour than os.path.islink(). |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
134 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
135 w._is_symlink = False |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
136 w._stat_result = os.stat(w._path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
137 return w |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
138 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
139 @staticmethod |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
140 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
|
141 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
|
142 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
143 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
144 def walk(root, follow_symlinks=False): |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
145 """A heyvily customized :func:`os.walk` alike that differs from the |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
146 original: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
147 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
148 - optimized for use in :command:`treesum` |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
149 - most errors are not suppressed |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
150 - the `root` is never part of the returned data |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
151 - the returned directory in "top" is not a string form but a list of |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
152 individual path segments |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
153 - there is only one yielded list |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
154 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
155 * contains :class:`WalkDirEntry` |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
156 * sorted by its fsname |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
157 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
158 The caller can easily get the old dirs and nondirs by filtering |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
159 the yielded list using "entry.is_dir". |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
160 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
161 - recurse into sub-directories first ("topdown=False") |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
162 - sort consistently all yielded lists by the filesystem encoding |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
163 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
164 .. note:: The implementation is based on Python 3.11 and needs a |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
165 functional :func:`os.scandir` or :func:`scandir.scandir` |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
166 implementation. It intentionally follows the logic in |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
167 Python 3.11 while it could be simplified because we are not |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
168 implementing some of the original flags (e.g. like |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
169 `topdown`). |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
170 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
171 """ |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
172 normed_root = os.path.normpath(root) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
173 yield from _walk(normed_root, tuple(), follow_symlinks=follow_symlinks) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
174 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
175 |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
176 if scandir: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
177 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
178 def _walk(root, top, follow_symlinks): |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
179 """:func:`walk` helper. |
|
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
|
180 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
181 Implemented using :func:`os.scandir`. |
|
121
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
182 |
|
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 if top: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
185 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
|
186 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
187 path = root |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
188 |
|
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
|
189 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
|
190 |
|
123
4a0c3c9eead7
Use the output of "scandir()" directly if it has a "close" method.
Franz Glasner <fzglas.hg@dom66.de>
parents:
121
diff
changeset
|
191 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
|
192 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
|
193 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
|
194 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
|
195 while True: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
196 try: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
197 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
|
198 except StopIteration: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
199 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
|
200 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
|
201 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
202 # 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
|
203 # 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
|
204 # |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
205 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
|
206 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
|
207 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
|
208 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
209 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
|
210 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
|
211 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
|
212 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
213 # 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
|
214 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
|
215 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
|
216 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
217 # 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
|
218 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
|
219 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
|
220 # 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
|
221 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
|
222 |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
223 else: |
|
2dc26a2f3d1c
A heavily customized "os.walk()" alike to support the coming treeview implementation
Franz Glasner <fzglas.hg@dom66.de>
parents:
diff
changeset
|
224 |
|
162
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
225 def _walk(root, top, follow_symlinks): |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
226 """:func:`walk` helper. |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
227 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
228 Implemented using :func:`os.listdir`. |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
229 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
230 """ |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
231 if top: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
232 path = os.path.join(root, *top) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
233 else: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
234 path = root |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
235 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
236 fsobjects, walk_dirs = [], [] |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
237 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
238 names = os.listdir(path) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
239 for name in names: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
240 entry = WalkDirEntry.from_path_name(path, name) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
241 fsobjects.append(entry) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
242 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
243 # Always bottom-up: recurse into sub-directories, but exclude |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
244 # symlinks to directories if follow_symlinks is False |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
245 # |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
246 if entry.is_dir: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
247 if follow_symlinks: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
248 walk_into = True |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
249 else: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
250 walk_into = not entry.is_symlink |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
251 if walk_into: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
252 walk_dirs.append(entry) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
253 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
254 # Sort by low-level filesystem encoding |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
255 walk_dirs.sort(key=WalkDirEntry.sort_key) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
256 fsobjects.sort(key=WalkDirEntry.sort_key) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
257 |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
258 # Recurse into sub-directories |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
259 for wd in walk_dirs: |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
260 yield from _walk(root, top + (wd.name,), follow_symlinks) |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
261 # Yield after recursion if going bottom up |
|
29dd5528174c
Implement walk._walk() using os.listdir() also.
Franz Glasner <fzglas.hg@dom66.de>
parents:
156
diff
changeset
|
262 yield top, fsobjects |
