PYTHONPython

base

real world projects / file sync / sync / backends

PYTHON
base.py🐍
"""
Abstract base class for storage backends.
"""

from abc import ABC, abstractmethod
from pathlib import Path
from typing import List, Optional
from dataclasses import dataclass
from datetime import datetime


@dataclass
class RemoteFile:
    """Information about a remote file."""
    path: str
    size: int
    modified_time: datetime
    checksum: Optional[str] = None


class BaseBackend(ABC):
    """Abstract base class for storage backends."""
    
    @abstractmethod
    async def connect(self):
        """Initialize connection to the backend."""
        pass
    
    @abstractmethod
    async def disconnect(self):
        """Close connection to the backend."""
        pass
    
    @abstractmethod
    async def upload(self, local_path: Path, remote_path: str) -> bool:
        """
        Upload a file to the backend.
        
        Args:
            local_path: Path to local file
            remote_path: Destination path in backend
        
        Returns:
            True if successful
        """
        pass
    
    @abstractmethod
    async def download(self, remote_path: str, local_path: Path) -> bool:
        """
        Download a file from the backend.
        
        Args:
            remote_path: Path in backend
            local_path: Destination path locally
        
        Returns:
            True if successful
        """
        pass
    
    @abstractmethod
    async def delete(self, remote_path: str) -> bool:
        """
        Delete a file from the backend.
        
        Args:
            remote_path: Path to delete
        
        Returns:
            True if successful
        """
        pass
    
    @abstractmethod
    async def exists(self, remote_path: str) -> bool:
        """
        Check if a file exists in the backend.
        
        Args:
            remote_path: Path to check
        
        Returns:
            True if file exists
        """
        pass
    
    @abstractmethod
    async def list_files(self, prefix: str = "") -> List[RemoteFile]:
        """
        List files in the backend.
        
        Args:
            prefix: Path prefix to filter by
        
        Returns:
            List of RemoteFile objects
        """
        pass
    
    @abstractmethod
    async def get_file_info(self, remote_path: str) -> Optional[RemoteFile]:
        """
        Get information about a remote file.
        
        Args:
            remote_path: Path to file
        
        Returns:
            RemoteFile object or None if not found
        """
        pass
PreviousNext