Metadata-Version: 2.5
Name: pycardia
Version: 0.1.0
Summary: Python port of Cordis: spatiotemporal composability via Context / Fiber / Service / Event / Effect
Author: Wanguo Pang
License-Expression: MIT
License-File: LICENSE
Keywords: cordis,dependency-injection,events,framework,plugin
Classifier: Framework :: AsyncIO
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Requires-Python: >=3.11
Requires-Dist: pyyaml>=6.0
Provides-Extra: dev
Requires-Dist: pyright>=1.1.0; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Provides-Extra: schema
Requires-Dist: pydantic>=2.0; extra == 'schema'
Description-Content-Type: text/markdown

# pycardia

A Python port of Cordis' **Spatiotemporal Composability** meta-framework.

Treating **plugins as behavioral units**, **reversible side effects as the time axis**,
and **dependency scopes as the space axis**, it recreates Cordis' core architecture
using Python-native `contextvars` + `asyncio`:
Context / Fiber / Service / Event / Effect.

## Installation

```bash
pip install -e .        # core engine (only depends on PyYAML)
pip install -e ".[dev]" # dev tooling (pyright / pytest)
```

Requires Python 3.11+.

## The two axes

- **SPACE axis** — dependencies & scopes
  - `Context` is the service container; `ctx.<name>` resolves services dynamically (`__getattr__` delegates to `ReflectService`).
  - `inject` declares dependencies, and load order is determined by the dependency graph; `isolate(name)` assigns an isolation key to a service.
- **TIME axis** — side effects & lifecycle
  - Every registration (listeners, services, utilities) is a `ctx.effect()` that returns a disposer.
  - `Fiber` state machine: `PENDING → LOADING → ACTIVE → FAILED → DISPOSED → UNLOADING`.
  - Unloading reclaims in LIFO reverse order; mounting/unmounting can repeat, reload safely, and never leak.

## Quick start

```python
import asyncio
from pycardia import Context

def greeter(ctx, config):
    ctx.provide("greeter", lambda n: f"Hello, {n}!")
    def disposer():
        print("greeter disposed")
    return disposer

def user(ctx, config):
    ctx.on("start", lambda: print(ctx.greeter("Cordis")))
    return lambda: print("user disposed")

async def main():
    app = Context()
    app.plugin(greeter)
    app.plugin(user)
    await app.start_all()
    app.emit("start")          # → Hello, Cordis!
    await app.stop()           # LIFO: user → greeter

asyncio.run(main())
```

## Examples

| File | Description |
|---|---|
| `examples/demo_hello.py` | Minimal loop: provide → inject → emit → LIFO cleanup |
| `examples/demo_mechanisms.py` | Dispatch modes / waterfall / dependency-driven reload / isolate |
| `examples/demo_loader.py` | Declarative YAML config + `!!js` interpolation + EntryTree |

```bash
PYTHONPATH=. python examples/demo_hello.py
PYTHONPATH=. python examples/demo_mechanisms.py
PYTHONPATH=. python examples/demo_loader.py
```

## Layout

```
pycardia/
├── context.py    # Context: container + extend/isolate/intercept + mixin API
├── fiber.py      # Fiber: lifecycle state machine + effect/disposer (time axis)
├── registry.py   # Registry: plugin registry + inject resolution + dependency notifications
├── events.py     # EventsService: emit/parallel/serial/bail/waterfall
├── reflect.py    # ReflectService: provide/get/set + versioned notifications
├── logger.py     # LoggerService: callable logger
├── types.py      # Plugin / Service / Disposable protocols
└── utils.py      # DisposableList (LIFO stack container) + compose_error
pycardia_loader/
├── tree.py       # EntryTree / EntryGroup / Entry
├── include.py    # YAML/JSON loading + !!js expression interpolation
└── schema.py     # optional Pydantic config validation
```

## Runtime invariants

1. Every registration has a disposer (`effect`, or `on`/`provide` etc. handled automatically).
2. Unloading is LIFO (last installed, first released).
3. Load order is determined by the dependency graph; manual boot sequencing is forbidden.
4. Writing a service across fibers raises an error (`set` can only write services provided by its own fiber).
5. Registering an effect on an inactive fiber raises `FiberError`.

## Design notes

- The core engine depends only on the standard library + `PyYAML` (used by the loader).
- `contextvars.ContextVar` carries the "current fiber"; `ctx.on`/`ctx.provide`/`ctx.effect`
  automatically bind to the fiber being executed.
- Dependency version numbers monotonically increase (`ReflectService.version`); config hot-reloads
  trigger dependent `notify → unload → reload`, driven by epoch comparison, no manual orchestration.
