Skip to content

Commit

Permalink
refactor: improve docstrings and typing
Browse files Browse the repository at this point in the history
  • Loading branch information
MountainGod2 committed Oct 21, 2024
1 parent 3a14747 commit a5b8482
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/chaturbate_poller/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def __init__(self, env_file: str = ".env") -> None:
"""Initialize the ConfigManager by loading environment variables.
Args:
env_file (str): Path to a .env file containing environment variables.
Defaults to ".env".
env_file (str): Path to a .env file containing environment variables. Defaults to
".env".
"""
load_dotenv(env_file)
self.config: dict[str, str | bool | None] = {}
Expand Down
29 changes: 24 additions & 5 deletions src/chaturbate_poller/event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,30 @@ class EventHandler(ABC): # pylint: disable=too-few-public-methods

@abstractmethod
async def handle_event(self, event: Event) -> None:
"""Handle an event."""
"""Handle an event.
Args:
event (Event): The event to be handled.
"""


class DatabaseEventHandler(EventHandler): # pylint: disable=too-few-public-methods
"""Event handler for writing events to a database."""

def __init__(self, influxdb_handler: InfluxDBHandler) -> None:
"""Initialize the database event handler."""
"""Initialize the database event handler.
Args:
influxdb_handler (InfluxDBHandler): The handler to interact with InfluxDB.
"""
self.influxdb_handler = influxdb_handler

async def handle_event(self, event: Event) -> None:
"""Handle an event."""
"""Handle an event by writing it to the database.
Args:
event (Event): The event to be written to the database.
"""
logger.debug("Handling event for database: %s", event.method)
self.influxdb_handler.write_event("chaturbate_events", event.model_dump())

Expand All @@ -39,7 +51,11 @@ def __init__(self) -> None:
"""Initialize the logging event handler."""

async def handle_event(self, event: Event) -> None:
"""Handle an event."""
"""Handle an event by logging it.
Args:
event (Event): The event to be logged.
"""
logger.debug("Handling event for logging: %s", event.method)
message = await format_message(event)
logger.info(message)
Expand All @@ -52,7 +68,10 @@ def create_event_handler(handler_type: str) -> EventHandler:
handler_type (str): The type of event handler to create.
Returns:
EventHandler: The event handler.
EventHandler: The appropriate event handler based on the type.
Raises:
ValueError: If an unknown handler type is passed.
"""
if handler_type == "database":
influxdb_handler = InfluxDBHandler()
Expand Down
4 changes: 2 additions & 2 deletions src/chaturbate_poller/influxdb_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InfluxDBHandler:
"""Class to handle InfluxDB operations."""

def __init__(self) -> None:
"""Initialize the InfluxDB handler."""
"""Initialize the InfluxDB handler by setting up the client and configuration."""
config_manager = ConfigManager()

self.url = config_manager.get("INFLUXDB_URL", "")
Expand Down Expand Up @@ -58,7 +58,7 @@ def write_event(self, measurement: str, data: dict[str, Any]) -> None:
Args:
measurement (str): The measurement name.
data (dict[str, Any]): The event data.
data (dict[str, Any]): The event data to write.
"""
try:
flattened_data = self.flatten_dict(data)
Expand Down

0 comments on commit a5b8482

Please sign in to comment.