Spaces:
Running on Zero
Running on Zero
File size: 2,255 Bytes
0122a25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | """Standard backend for local files on a hard drive.
This backends loads data from and saves data to the local hard drive.
"""
import os
from typing import Literal
from .base import DataBackend
class FileBackend(DataBackend):
"""Raw file from hard disk data backend."""
def isfile(self, filepath: str) -> bool:
"""Check if filepath is a file.
Args:
filepath (str): Path to file.
Returns:
bool: True if file exists, False otherwise.
"""
return os.path.isfile(filepath)
def listdir(self, filepath: str) -> list[str]:
"""List all files in the directory.
Args:
filepath (str): Path to file.
Returns:
list[str]: List of all files in the directory.
"""
return sorted(os.listdir(filepath))
def exists(self, filepath: str) -> bool:
"""Check if filepath exists.
Args:
filepath (str): Path to file.
Returns:
bool: True if file exists, False otherwise.
"""
return os.path.exists(filepath)
def set(
self, filepath: str, content: bytes, mode: Literal["w", "a"] = "w"
) -> None:
"""Write the file content to disk.
Args:
filepath (str): Path to file.
content (bytes): Content to write in bytes.
mode (Literal["w", "a"], optional): Overwrite or append mode.
Defaults to "w".
"""
os.makedirs(os.path.dirname(filepath), exist_ok=True)
mode_binary: Literal["wb", "ab"] = "wb" if mode == "w" else "ab"
with open(filepath, mode_binary) as f:
f.write(content)
def get(self, filepath: str) -> bytes:
"""Get file content as bytes.
Args:
filepath (str): Path to file.
Raises:
FileNotFoundError: If filepath does not exist.
Returns:
bytes: File content as bytes.
"""
if not self.exists(filepath):
raise FileNotFoundError(f"File not found:" f" {filepath}")
with open(filepath, "rb") as f:
value_buf = f.read()
return value_buf
def close(self) -> None:
"""No need to close manually."""
|