ReMe/reme_cli/component/base_component.py
2026-04-08 16:49:36 +08:00

32 lines
829 B
Python

"""Base class for components."""
from abc import ABC, abstractmethod
from ..enumeration import ComponentEnum
class BaseComponent(ABC):
"""Base class supporting async start/close and async context management."""
component_type = ComponentEnum.BASE
@abstractmethod
async def start(self) -> None:
"""Start the component asynchronously."""
@abstractmethod
async def close(self) -> None:
"""Close the component asynchronously."""
async def __aenter__(self) -> "BaseComponent":
"""Enter async context manager."""
await self.start()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb) -> bool:
"""Exit async context manager."""
await self.close()
if exc_type is not None:
return True
return False