|
| 1 | +"""Base classes for DialogChain connectors.""" |
| 2 | + |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from typing import Any, AsyncIterator, Dict, Optional |
| 5 | +from urllib.parse import urlparse |
| 6 | +import logging |
| 7 | + |
| 8 | +logger = logging.getLogger(__name__) |
| 9 | + |
| 10 | +class Connector(ABC): |
| 11 | + """Base class for all connectors.""" |
| 12 | + |
| 13 | + def __init__(self, uri: str): |
| 14 | + """Initialize the connector with a URI. |
| 15 | + |
| 16 | + Args: |
| 17 | + uri: Connection string in the format 'scheme://[user:password@]host[:port][/path][?query]' |
| 18 | + """ |
| 19 | + self.uri = uri |
| 20 | + self.parsed_uri = urlparse(uri) |
| 21 | + self._is_connected = False |
| 22 | + |
| 23 | + @property |
| 24 | + def is_connected(self) -> bool: |
| 25 | + """Check if the connector is connected.""" |
| 26 | + return self._is_connected |
| 27 | + |
| 28 | + async def connect(self): |
| 29 | + """Establish a connection to the resource.""" |
| 30 | + if self._is_connected: |
| 31 | + return self |
| 32 | + |
| 33 | + try: |
| 34 | + await self._connect() |
| 35 | + self._is_connected = True |
| 36 | + logger.debug(f"Connected to {self.__class__.__name__}: {self.uri}") |
| 37 | + return self |
| 38 | + except Exception as e: |
| 39 | + self._is_connected = False |
| 40 | + logger.error(f"Failed to connect to {self.uri}: {e}") |
| 41 | + raise |
| 42 | + |
| 43 | + async def disconnect(self): |
| 44 | + """Close the connection to the resource.""" |
| 45 | + if not self._is_connected: |
| 46 | + return |
| 47 | + |
| 48 | + try: |
| 49 | + await self._disconnect() |
| 50 | + logger.debug(f"Disconnected from {self.__class__.__name__}: {self.uri}") |
| 51 | + except Exception as e: |
| 52 | + logger.error(f"Error disconnecting from {self.uri}: {e}") |
| 53 | + raise |
| 54 | + finally: |
| 55 | + self._is_connected = False |
| 56 | + |
| 57 | + async def __aenter__(self): |
| 58 | + """Async context manager entry.""" |
| 59 | + return await self.connect() |
| 60 | + |
| 61 | + async def __aexit__(self, exc_type, exc_val, exc_tb): |
| 62 | + """Async context manager exit.""" |
| 63 | + await self.disconnect() |
| 64 | + |
| 65 | + @abstractmethod |
| 66 | + async def _connect(self): |
| 67 | + """Implementation-specific connection logic.""" |
| 68 | + pass |
| 69 | + |
| 70 | + @abstractmethod |
| 71 | + async def _disconnect(self): |
| 72 | + """Implementation-specific disconnection logic.""" |
| 73 | + pass |
| 74 | + |
| 75 | + |
| 76 | +class Source(Connector): |
| 77 | + """Base class for all data sources.""" |
| 78 | + |
| 79 | + @abstractmethod |
| 80 | + async def receive(self) -> AsyncIterator[Dict[str, Any]]: |
| 81 | + """Async generator that yields data from the source. |
| 82 | + |
| 83 | + Yields: |
| 84 | + Dictionary containing the received data and metadata |
| 85 | + """ |
| 86 | + pass |
| 87 | + |
| 88 | + @classmethod |
| 89 | + def create(cls, uri: str, **kwargs) -> 'Source': |
| 90 | + """Create a source instance from a URI. |
| 91 | + |
| 92 | + Args: |
| 93 | + uri: Source URI (e.g., 'rtsp://...', 'imap://...') |
| 94 | + **kwargs: Additional arguments for the source |
| 95 | + |
| 96 | + Returns: |
| 97 | + Source: An instance of the appropriate source class |
| 98 | + |
| 99 | + Raises: |
| 100 | + ValueError: If the URI scheme is not supported |
| 101 | + """ |
| 102 | + from . import sources |
| 103 | + |
| 104 | + parsed = urlparse(uri) |
| 105 | + scheme = parsed.scheme.lower() |
| 106 | + |
| 107 | + # Map scheme to source class |
| 108 | + source_classes = { |
| 109 | + 'rtsp': sources.RTSPSource, |
| 110 | + 'imap': sources.IMAPSource, |
| 111 | + 'file': sources.FileSource, |
| 112 | + 'timer': sources.TimerSource, |
| 113 | + } |
| 114 | + |
| 115 | + if scheme not in source_classes: |
| 116 | + raise ValueError(f"Unsupported source scheme: {scheme}") |
| 117 | + |
| 118 | + return source_classes[scheme](uri, **kwargs) |
| 119 | + |
| 120 | + |
| 121 | +class Destination(Connector): |
| 122 | + """Base class for all data destinations.""" |
| 123 | + |
| 124 | + @abstractmethod |
| 125 | + async def send(self, data: Any) -> None: |
| 126 | + """Send data to the destination. |
| 127 | + |
| 128 | + Args: |
| 129 | + data: The data to send |
| 130 | + |
| 131 | + Raises: |
| 132 | + Exception: If sending fails |
| 133 | + """ |
| 134 | + pass |
| 135 | + |
| 136 | + @classmethod |
| 137 | + def create(cls, uri: str, **kwargs) -> 'Destination': |
| 138 | + """Create a destination instance from a URI. |
| 139 | + |
| 140 | + Args: |
| 141 | + uri: Destination URI (e.g., 'http://...', 'smtp://...') |
| 142 | + **kwargs: Additional arguments for the destination |
| 143 | + |
| 144 | + Returns: |
| 145 | + Destination: An instance of the appropriate destination class |
| 146 | + |
| 147 | + Raises: |
| 148 | + ValueError: If the URI scheme is not supported |
| 149 | + """ |
| 150 | + from . import destinations |
| 151 | + |
| 152 | + parsed = urlparse(uri) |
| 153 | + scheme = parsed.scheme.lower() |
| 154 | + |
| 155 | + # Map scheme to destination class |
| 156 | + dest_classes = { |
| 157 | + 'http': destinations.HTTPDestination, |
| 158 | + 'https': destinations.HTTPDestination, |
| 159 | + 'smtp': destinations.EmailDestination, |
| 160 | + 'file': destinations.FileDestination, |
| 161 | + 'log': destinations.LogDestination, |
| 162 | + } |
| 163 | + |
| 164 | + if scheme not in dest_classes: |
| 165 | + raise ValueError(f"Unsupported destination scheme: {scheme}") |
| 166 | + |
| 167 | + return dest_classes[scheme](uri, **kwargs) |
0 commit comments