def dir_list(self, path: Path) -> Iterable[FSObjectPath]: files: List[Path] dirs: List[Path] current_dir: MemoryDirectory try: current_dir = self._get_dir(path) except NotADirectoryError: pass else: for file_ in current_dir.list_files(): yield FSObjectPath(FSObjectType.FILE, path.child(file_)) for dir_ in current_dir.list_dirs(): yield FSObjectPath(FSObjectType.DIR, path.child(dir_))
class TestPath(TestCase): drive: str tail: List[str] sut: Path def setUp(self) -> None: self.drive = mock.Mock(spec=str) self.tail = [mock.Mock(spec=str) for _ in range(random.randint(1, 10))] self.sut = Path(self.drive, *self.tail) def test_drive(self) -> None: assert self.sut.drive == self.drive def test_tail(self) -> None: assert self.sut.tail == tuple(self.tail) def test_basename(self) -> None: assert self.sut.basename == self.tail[-1] def test_parent(self) -> None: assert self.sut.parent == Path(self.drive, *self.tail[:-1]) def test_child(self) -> None: child_name: str = mock.Mock(spec=str) assert self.sut.child(child_name) == Path(self.drive, *self.tail, child_name) def test_repr(self) -> None: assert self.sut.__repr__( ) == f"Path(drive={self.drive}, tail={tuple(self.tail)})"
def dir_list(self, path: Path) -> Iterable[FSObjectPath]: path_str: str = self.path_to_string(path) items: List[str] try: items = os.listdir(path_str) except FileNotFoundError: return for item in items: full_path: str = os.path.join(path_str, item) item_path: Path = path.child(item) if os.path.isfile(full_path): yield FSObjectPath(FSObjectType.FILE, item_path) elif os.path.isdir(full_path): yield FSObjectPath(FSObjectType.DIR, item_path) else: yield FSObjectPath(FSObjectType.OTHER, item_path)