base.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from abc import ABC, abstractmethod
  2. class DataReader(ABC):
  3. def read(self, path: str) -> bytes:
  4. """Read the file.
  5. Args:
  6. path (str): file path to read
  7. Returns:
  8. bytes: the content of the file
  9. """
  10. return self.read_at(path)
  11. @abstractmethod
  12. def read_at(self, path: str, offset: int = 0, limit: int = -1) -> bytes:
  13. """Read the file at offset and limit.
  14. Args:
  15. path (str): the file path
  16. offset (int, optional): the number of bytes skipped. Defaults to 0.
  17. limit (int, optional): the length of bytes want to read. Defaults to -1.
  18. Returns:
  19. bytes: the content of the file
  20. """
  21. pass
  22. class DataWriter(ABC):
  23. @abstractmethod
  24. def write(self, path: str, data: bytes) -> None:
  25. """Write the data to the file.
  26. Args:
  27. path (str): the target file where to write
  28. data (bytes): the data want to write
  29. """
  30. pass
  31. def write_string(self, path: str, data: str) -> None:
  32. """Write the data to file, the data will be encoded to bytes.
  33. Args:
  34. path (str): the target file where to write
  35. data (str): the data want to write
  36. """
  37. self.write(path, data.encode())