Metadata-Version: 2.4
Name: eventwire
Version: 0.1.0
Summary: Lightweight, zero-dependency async & sync in-process event bus and decoupled messaging toolkit for Python.
Author-email: Hamidreza <h4midrezam@gmail.com>
License-Expression: MIT
Keywords: event-bus,events,pubsub,asyncio,messaging,decoupled,middleware,dead-letter-queue,signals,dispatcher
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Distributed Computing
Classifier: Topic :: System :: Networking
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=8.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
Dynamic: license-file

# ⚡ EventWire

> **Zero-dependency async & sync in-process event bus and decoupled messaging engine for Python.**

[![PyPI version](https://img.shields.io/pypi/v/eventwire.svg)](https://pypi.org/project/eventwire/)
[![Python versions](https://img.shields.io/pypi/pyversions/eventwire.svg)](https://pypi.org/project/eventwire/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Tests](https://img.shields.io/badge/tests-passing-brightgreen.svg)]()
[![Typing](https://img.shields.io/badge/typing-typed-blue.svg)]()

---

## 💡 Why EventWire?

In modern Python backends (FastAPI, Django, asynchronous microservices, workflow agents), modules frequently need to communicate without tightly coupling themselves to one another.

Instead of calling 5 different services directly inside an HTTP route or having to spin up heavyweight distributed brokers (Celery, Kafka, RabbitMQ) just for in-process communication, **EventWire** provides a high-performance, strictly typed in-memory event bus.

---

## ✨ Features

- **⚡ Sync & Async Support**: Emit synchronously (`bus.emit`) or asynchronously (`await bus.emit_async`) with concurrent or sequential execution.
- **🏷️ First-Class Type Safety**: Use dataclasses or class hierarchies as events with full IDE autocompletion (`py.typed`).
- **🎯 Wildcard Subscriptions**: Subscribe to topics with glob patterns (e.g. `order.*`, `user.*`, `*`).
- **🔢 Priority Ordering**: Control execution order with prioritized listeners (e.g. `priority=100` before `priority=0`).
- **🛡️ Dead-Letter Queue (DLQ)**: Fault-tolerant error recording with full stack traces, inspection, and recovery.
- **🔌 Middleware Pipeline**: Intercept events globally for logging, metrics, tracing, or validation.
- **🛑 Propagation Control**: Stop event propagation anytime (`event.stop_propagation()`).
- **📦 Zero Dependencies**: Built purely with Python standard library (`asyncio`, `dataclasses`, `inspect`).

---

## 🚀 Quickstart

### 1. Installation

```bash
pip install eventwire
```

### 2. Basic Synchronous Pub/Sub

```python
from eventwire import EventBus, Event

bus = EventBus()

class UserRegistered(Event):
    def __init__(self, username: str, email: str):
        super().__init__()
        self.username = username
        self.email = email

@bus.on(UserRegistered, priority=10)
def send_welcome_email(event: UserRegistered):
    print(f"📧 Sending welcome email to {event.email}")

@bus.on(UserRegistered, priority=5)
def provision_account(event: UserRegistered):
    print(f"👤 Provisioning workspace for {event.username}")

# Emit event
bus.emit(UserRegistered("alice", "alice@example.com"))
```

---

### 3. Asynchronous Concurrent Dispatch

```python
import asyncio
from eventwire import EventBus, Event

bus = EventBus()

class ImageUploaded(Event):
    def __init__(self, image_id: str):
        super().__init__()
        self.image_id = image_id

@bus.on(ImageUploaded)
async def generate_thumbnail(event: ImageUploaded):
    await asyncio.sleep(0.1)
    print(f"🖼️ Thumbnail created for {event.image_id}")

@bus.on(ImageUploaded)
async def index_metadata(event: ImageUploaded):
    await asyncio.sleep(0.05)
    print(f"🔍 Metadata indexed for {event.image_id}")

# Run both handlers concurrently via asyncio.gather
asyncio.run(bus.emit_async(ImageUploaded("img_123"), concurrent=True))
```

---

### 4. Wildcards and Priority Ordering

```python
bus = EventBus()

@bus.on("auth.*")
def audit_auth_events(event_name):
    print(f"Audit log: {event_name}")

@bus.on("*", priority=-100)
def global_telemetry(event_name):
    print(f"Telemetry record: {event_name}")

bus.emit("auth.login")
bus.emit("auth.password_reset")
```

---

### 5. Dead-Letter Queue (DLQ) & Resilience

```python
bus = EventBus()

@bus.on("payment.charge")
def process_payment(event):
    raise TimeoutError("Stripe gateway unreachable")

# Suppress errors so critical flows aren't interrupted
bus.emit("payment.charge", suppress_errors=True)

# Inspect failed events in DLQ
print(f"Failed events: {bus.dlq.count()}")
for letter in bus.dlq.get_all():
    print(f"Failed handler: {letter.handler_name}")
    print(f"Exception: {letter.error}")
```

---

## 🧪 Testing

```bash
python -m pytest
```

---

## 📄 License

MIT License. Copyright (c) 2026 Hamidreza.
