Local hard disk storage
| 39 | |
| 40 | |
| 41 | class LocalStorage(Storage): |
| 42 | """Local hard disk storage""" |
| 43 | |
| 44 | def read(self, filepath: Union[str, Path]) -> bytes: |
| 45 | """Read data from a given ``filepath`` with 'rb' mode. |
| 46 | |
| 47 | Args: |
| 48 | filepath (str or Path): Path to read data. |
| 49 | |
| 50 | Returns: |
| 51 | bytes: Expected bytes object. |
| 52 | """ |
| 53 | with open(filepath, 'rb') as f: |
| 54 | content = f.read() |
| 55 | return content |
| 56 | |
| 57 | def read_text(self, |
| 58 | filepath: Union[str, Path], |
| 59 | encoding: str = 'utf-8') -> str: |
| 60 | """Read data from a given ``filepath`` with 'r' mode. |
| 61 | |
| 62 | Args: |
| 63 | filepath (str or Path): Path to read data. |
| 64 | encoding (str): The encoding format used to open the ``filepath``. |
| 65 | Default: 'utf-8'. |
| 66 | |
| 67 | Returns: |
| 68 | str: Expected text reading from ``filepath``. |
| 69 | """ |
| 70 | with open(filepath, 'r', encoding=encoding) as f: |
| 71 | value_buf = f.read() |
| 72 | return value_buf |
| 73 | |
| 74 | def write(self, obj: bytes, filepath: Union[str, Path]) -> None: |
| 75 | """Write data to a given ``filepath`` with 'wb' mode. |
| 76 | |
| 77 | Note: |
| 78 | ``write`` will create a directory if the directory of ``filepath`` |
| 79 | does not exist. |
| 80 | |
| 81 | Args: |
| 82 | obj (bytes): Data to be written. |
| 83 | filepath (str or Path): Path to write data. |
| 84 | """ |
| 85 | dirname = os.path.dirname(filepath) |
| 86 | if dirname and not os.path.exists(dirname): |
| 87 | os.makedirs(dirname, exist_ok=True) |
| 88 | |
| 89 | with open(filepath, 'wb') as f: |
| 90 | f.write(obj) |
| 91 | |
| 92 | def write_text(self, |
| 93 | obj: str, |
| 94 | filepath: Union[str, Path], |
| 95 | encoding: str = 'utf-8') -> None: |
| 96 | """Write data to a given ``filepath`` with 'w' mode. |
| 97 | |
| 98 | Note: |
no outgoing calls
searching dependent graphs…