Selfie
Loading...
Searching...
No Matches
FS.py
Go to the documentation of this file.
1from abc import ABC, abstractmethod
2from collections.abc import Iterator
3from pathlib import Path
4from typing import Callable, TypeVar
5
6from selfie_lib.TypedPath import TypedPath
7
8T = TypeVar("T")
9
10
11class FS(ABC):
12 def file_exists(self, typed_path: TypedPath) -> bool:
13 return Path(typed_path.absolute_path).is_file()
14
16 self, typed_path: TypedPath, walk: Callable[[Iterator[TypedPath]], T]
17 ) -> T:
18 def walk_generator(path: TypedPath) -> Iterator[TypedPath]:
19 for file_path in Path(path.absolute_path).rglob("*"):
20 if file_path.is_file():
21 yield TypedPath(file_path.absolute().as_posix())
22
23 return walk(walk_generator(typed_path))
24
25 def file_read(self, typed_path) -> str:
26 return self.file_read_binary(typed_path).decode()
27
28 def file_write(self, typed_path, content: str):
29 self.file_write_binary(typed_path, content.encode())
30
31 def file_read_binary(self, typed_path: TypedPath) -> bytes:
32 return Path(typed_path.absolute_path).read_bytes()
33
34 def file_write_binary(self, typed_path: TypedPath, content: bytes):
35 Path(typed_path.absolute_path).write_bytes(content)
36
37 @abstractmethod
38 def assert_failed(self, message, expected=None, actual=None) -> Exception:
39 pass
bool file_exists(self, TypedPath typed_path)
Definition FS.py:12
file_write(self, typed_path, str content)
Definition FS.py:28
T file_walk(self, TypedPath typed_path, Callable[[Iterator[TypedPath]], T] walk)
Definition FS.py:17
str file_read(self, typed_path)
Definition FS.py:25
Exception assert_failed(self, message, expected=None, actual=None)
Definition FS.py:38
bytes file_read_binary(self, TypedPath typed_path)
Definition FS.py:31
file_write_binary(self, TypedPath typed_path, bytes content)
Definition FS.py:34