Mercurial > hgrepos > Python > apps > py-cutils
comparison cutils/util/walk.py @ 175:506d895a8500
Implement cutils.util.walk.Scandir as a wrapper for os.scandir()
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Sat, 11 Jan 2025 12:20:11 +0100 |
| parents | a813094ae4f5 |
| children | 089c40240061 |
comparison
equal
deleted
inserted
replaced
| 174:fc1055878775 | 175:506d895a8500 |
|---|---|
| 218 for wd in walk_dirs: | 218 for wd in walk_dirs: |
| 219 yield from _walk(root, top + (wd.name,), follow_symlinks) | 219 yield from _walk(root, top + (wd.name,), follow_symlinks) |
| 220 # Yield after recursion if going bottom up | 220 # Yield after recursion if going bottom up |
| 221 yield top, fsobjects | 221 yield top, fsobjects |
| 222 | 222 |
| 223 | |
| 224 class ScanDir(object): # noqa: E303 too many blank lines | |
| 225 | |
| 226 """An :func:`os.scandir` wrapper that is always an iterator and | |
| 227 a context manager. | |
| 228 | |
| 229 """ | |
| 230 | |
| 231 __slots__ = ("_scandir_it", ) | |
| 232 | |
| 233 def __init__(self, path): | |
| 234 super(ScanDir, self).__init__() | |
| 235 self._scandir_it = os.scandir(path) | |
| 236 | |
| 237 def __iter__(self): | |
| 238 return self | |
| 239 | |
| 240 def __next__(self): | |
| 241 return WalkDirEntry.from_direntry(next(self._scandir_it)) | |
| 242 | |
| 243 if PY2: | |
| 244 next = __next__ | |
| 245 | |
| 246 def __enter__(self): | |
| 247 return self | |
| 248 | |
| 249 def __exit__(self, *args, **kwds): | |
| 250 if hasattr(self._scandir_it, "close"): | |
| 251 self._scandir_it.close() | |
| 252 | |
| 223 else: | 253 else: |
| 224 | 254 |
| 225 def _walk(root, top, follow_symlinks): | 255 def _walk(root, top, follow_symlinks): |
| 226 """:func:`walk` helper. | 256 """:func:`walk` helper. |
| 227 | 257 |
| 258 # Recurse into sub-directories | 288 # Recurse into sub-directories |
| 259 for wd in walk_dirs: | 289 for wd in walk_dirs: |
| 260 yield from _walk(root, top + (wd.name,), follow_symlinks) | 290 yield from _walk(root, top + (wd.name,), follow_symlinks) |
| 261 # Yield after recursion if going bottom up | 291 # Yield after recursion if going bottom up |
| 262 yield top, fsobjects | 292 yield top, fsobjects |
| 293 | |
| 294 | |
| 295 class ScanDir(object): # noqa: E303 too many blank lines | |
| 296 | |
| 297 """An :func:`os.scandir` wrapper that is always an iterator and | |
| 298 a context manager. | |
| 299 | |
| 300 """ | |
| 301 | |
| 302 __slots__ = ("_listdir_it", "_path") | |
| 303 | |
| 304 def __init__(self, path): | |
| 305 super(ScanDir, self).__init__() | |
| 306 self._listdir_it = iter(os.listdir(path)) | |
| 307 self._path = path | |
| 308 | |
| 309 def __iter__(self): | |
| 310 return self | |
| 311 | |
| 312 def __next__(self): | |
| 313 return WalkDirEntry.from_path_name(self._path, | |
| 314 next(self._listdir_it)) | |
| 315 | |
| 316 if PY2: | |
| 317 next = __next__ | |
| 318 | |
| 319 def __enter__(self): | |
| 320 return self | |
| 321 | |
| 322 def __exit__(self, *args, **kwds): | |
| 323 pass |
