|
| 1 | +""" |
| 2 | +Abstract base class for storage backends. |
| 3 | +""" |
| 4 | +from abc import ABC, abstractmethod |
| 5 | +from typing import Any, AsyncIterator, Dict, List, Optional, Union |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +class StorageBackend(ABC): |
| 10 | + """Abstract base class for storage backends.""" |
| 11 | + |
| 12 | + def __init__(self, config: Dict[str, Any]): |
| 13 | + """ |
| 14 | + Initialize storage backend. |
| 15 | +
|
| 16 | + Args: |
| 17 | + config: Backend configuration dictionary |
| 18 | + """ |
| 19 | + self.config = config |
| 20 | + self.name = config.get("name", "unknown") |
| 21 | + |
| 22 | + @abstractmethod |
| 23 | + async def exists(self, path: str) -> bool: |
| 24 | + """ |
| 25 | + Check if a file exists. |
| 26 | +
|
| 27 | + Args: |
| 28 | + path: File path relative to backend root |
| 29 | +
|
| 30 | + Returns: |
| 31 | + True if file exists, False otherwise |
| 32 | + """ |
| 33 | + pass |
| 34 | + |
| 35 | + @abstractmethod |
| 36 | + async def read(self, path: str) -> AsyncIterator[bytes]: |
| 37 | + """ |
| 38 | + Read file contents as an async iterator of chunks. |
| 39 | +
|
| 40 | + Args: |
| 41 | + path: File path relative to backend root |
| 42 | +
|
| 43 | + Yields: |
| 44 | + File content chunks as bytes |
| 45 | + """ |
| 46 | + pass |
| 47 | + |
| 48 | + @abstractmethod |
| 49 | + async def write(self, path: str, data: Union[bytes, AsyncIterator[bytes]]) -> int: |
| 50 | + """ |
| 51 | + Write data to a file. |
| 52 | +
|
| 53 | + Args: |
| 54 | + path: File path relative to backend root |
| 55 | + data: File content as bytes or async iterator of chunks |
| 56 | +
|
| 57 | + Returns: |
| 58 | + Number of bytes written |
| 59 | + """ |
| 60 | + pass |
| 61 | + |
| 62 | + @abstractmethod |
| 63 | + async def delete(self, path: str) -> bool: |
| 64 | + """ |
| 65 | + Delete a file. |
| 66 | +
|
| 67 | + Args: |
| 68 | + path: File path relative to backend root |
| 69 | +
|
| 70 | + Returns: |
| 71 | + True if deleted, False if not found |
| 72 | + """ |
| 73 | + pass |
| 74 | + |
| 75 | + @abstractmethod |
| 76 | + async def list(self, path: str = "", recursive: bool = False) -> List[str]: |
| 77 | + """ |
| 78 | + List files in a directory. |
| 79 | +
|
| 80 | + Args: |
| 81 | + path: Directory path relative to backend root |
| 82 | + recursive: Whether to list recursively |
| 83 | +
|
| 84 | + Returns: |
| 85 | + List of file paths |
| 86 | + """ |
| 87 | + pass |
| 88 | + |
| 89 | + @abstractmethod |
| 90 | + async def ensure_dir(self, path: str) -> None: |
| 91 | + """ |
| 92 | + Ensure a directory exists, creating it if necessary. |
| 93 | +
|
| 94 | + Args: |
| 95 | + path: Directory path relative to backend root |
| 96 | + """ |
| 97 | + pass |
| 98 | + |
| 99 | + async def get_file_info(self, path: str) -> Optional[Dict[str, Any]]: |
| 100 | + """ |
| 101 | + Get file metadata. |
| 102 | +
|
| 103 | + Args: |
| 104 | + path: File path relative to backend root |
| 105 | +
|
| 106 | + Returns: |
| 107 | + Dictionary with file info or None if not found |
| 108 | + """ |
| 109 | + if not await self.exists(path): |
| 110 | + return None |
| 111 | + return { |
| 112 | + "path": path, |
| 113 | + "exists": True, |
| 114 | + } |
| 115 | + |
| 116 | + async def get_size(self, path: str) -> int: |
| 117 | + """ |
| 118 | + Get file size in bytes. |
| 119 | +
|
| 120 | + Args: |
| 121 | + path: File path relative to backend root |
| 122 | +
|
| 123 | + Returns: |
| 124 | + File size in bytes |
| 125 | + """ |
| 126 | + info = await self.get_file_info(path) |
| 127 | + return info.get("size", 0) if info else 0 |
| 128 | + |
| 129 | + async def get_status(self) -> Dict[str, Any]: |
| 130 | + """ |
| 131 | + Get backend status. |
| 132 | +
|
| 133 | + Returns: |
| 134 | + Dictionary with backend status information |
| 135 | + """ |
| 136 | + return { |
| 137 | + "name": self.name, |
| 138 | + "type": self.__class__.__name__, |
| 139 | + "available": True, |
| 140 | + } |
| 141 | + |
| 142 | + async def cleanup(self) -> None: |
| 143 | + """Clean up backend resources.""" |
| 144 | + pass |
| 145 | + |
| 146 | + def __repr__(self) -> str: |
| 147 | + return f"<{self.__class__.__name__} name={self.name}>" |
0 commit comments