Break iterdir out into a method of its own for easy testing

As I work on what's to come (loading DirectoryTree with a worker), I'm going
to want to try and construct slow loads so I can test the effectiveness of
the changes. This means a desire to fake a very slow source of directory
information. So let's drop this into its own method so we can then do silly
things like add a sleep to really show stuff down.
This commit is contained in:
Dave Pearson
2023-05-09 16:41:17 +01:00
parent 8855471125
commit 30a20ac8da

View File

@@ -2,7 +2,7 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import ClassVar, Iterable from typing import ClassVar, Iterable, Iterator
from rich.style import Style from rich.style import Style
from rich.text import Text, TextType from rich.text import Text, TextType
@@ -229,6 +229,19 @@ class DirectoryTree(Tree[DirEntry]):
""" """
return paths return paths
def _directory_content(self, directory: Path) -> Iterator[Path]:
"""Get the entries within a given directory.
Args:
directory: The directory to get the content of.
Returns:
An iterator of `Path` objects.
"""
# TODO: Place this in a loop with a sleep to slow things down for
# testing.
return directory.iterdir()
def _load_directory(self, node: TreeNode[DirEntry]) -> None: def _load_directory(self, node: TreeNode[DirEntry]) -> None:
"""Load the directory contents for a given node. """Load the directory contents for a given node.
@@ -238,7 +251,7 @@ class DirectoryTree(Tree[DirEntry]):
assert node.data is not None assert node.data is not None
node.data.loaded = True node.data.loaded = True
directory = sorted( directory = sorted(
self.filter_paths(node.data.path.iterdir()), self.filter_paths(self._directory_content(node.data.path)),
key=lambda path: (not path.is_dir(), path.name.lower()), key=lambda path: (not path.is_dir(), path.name.lower()),
) )
for path in directory: for path in directory: