Core module for Spakky framework to support DI/IoC, AOP, Plugin system, and more.
Project description
Spakky
Core module for Spakky Framework - a Spring-inspired dependency injection framework for Python.
Installation
pip install spakky
Or install with plugins:
pip install spakky[fastapi]
pip install spakky[fastapi,kafka,security]
Features
- Dependency Injection: Powerful IoC container with constructor injection
- Aspect-Oriented Programming: Cross-cutting concerns with
@Aspect - Plugin System: Extensible architecture via entry points
- Stereotypes: Semantic annotations (
@Controller,@UseCase, etc.) - Scopes: Singleton, Prototype, and Context-scoped beans
- Type-Safe: Built with Python type hints
- Async First: Native async/await support
Quick Start
Define Pods
from spakky.core.pod.annotations.pod import Pod
@Pod()
class UserRepository:
def find_by_id(self, user_id: int) -> User | None:
# Database query logic
pass
@Pod()
class UserService:
def __init__(self, repository: UserRepository) -> None:
self.repository = repository
def get_user(self, user_id: int) -> User | None:
return self.repository.find_by_id(user_id)
Bootstrap Application
from spakky.core.application.application import SpakkyApplication
from spakky.core.application.application_context import ApplicationContext
import my_app
app = (
SpakkyApplication(ApplicationContext())
.load_plugins()
.scan(my_app) # or .scan() to auto-detect caller's package
.start()
)
# Get a service from the container
user_service = app.container.get(UserService)
📘 Auto-scan: When
scan()is called without arguments, it automatically detects the caller's package and scans it. This also works in Docker environments where the application root may not be insys.path- the framework automatically adds the necessary path.
## Pod Scopes
```python
from spakky.core.pod.annotations.pod import Pod
# Singleton (default) - one instance per container
@Pod(scope=Pod.Scope.SINGLETON)
class SingletonService:
pass
# Prototype - new instance on each request
@Pod(scope=Pod.Scope.PROTOTYPE)
class PrototypeService:
pass
# Context - scoped to request/context lifecycle
@Pod(scope=Pod.Scope.CONTEXT)
class ContextScopedService:
pass
Qualifiers
from spakky.core.pod.annotations.pod import Pod
from spakky.core.pod.annotations.primary import Primary
# Named qualifier
@Pod(name="mysql")
class MySQLRepository(IRepository):
pass
@Pod(name="postgres")
class PostgresRepository(IRepository):
pass
# Primary - preferred when multiple implementations exist
@Primary()
@Pod()
class DefaultRepository(IRepository):
pass
Stereotypes
from spakky.core.stereotype.controller import Controller
from spakky.core.stereotype.usecase import UseCase
@Controller()
class UserController:
"""Groups related handlers together."""
pass
@UseCase()
class CreateUserUseCase:
"""Encapsulates business logic."""
pass
Aspect-Oriented Programming
from spakky.core.aop.aspect import Aspect, AsyncAspect
from spakky.core.aop.interfaces.aspect import IAspect, IAsyncAspect
from spakky.core.aop.pointcut import Before, After, Around
from spakky.core.pod.annotations.order import Order
from spakky.core.aspects.logging import Logging
# Create custom aspect
@Order(0)
@Aspect()
class LoggingAspect(IAspect):
@Before(lambda m: Logging.exists(m))
def before_log(self, *args, **kwargs) -> None:
print("Before method execution")
@After(lambda m: Logging.exists(m))
def after_log(self, *args, **kwargs) -> None:
print("After method execution")
# Apply to methods
@Pod()
class MyService:
@Logging()
def my_method(self) -> str:
return "Hello"
Async Aspects
from spakky.core.aop.aspect import AsyncAspect
from spakky.core.aop.interfaces.aspect import IAsyncAspect
from spakky.core.aop.pointcut import Around
@Order(0)
@AsyncAspect()
class TimingAspect(IAsyncAspect):
@Around(lambda m: hasattr(m, "__timed__"))
async def time_execution(self, joinpoint, *args, **kwargs):
start = time.time()
result = await joinpoint(*args, **kwargs)
elapsed = time.time() - start
print(f"Execution time: {elapsed:.2f}s")
return result
Built-in Aspects
from spakky.core.aspects.logging import Logging
@Pod()
class OrderService:
@Logging() # Automatic logging
async def create_order(self, order: Order) -> Order:
return await self.repository.save(order)
Plugin System
Plugins extend framework functionality:
# In pyproject.toml
[project.entry-points."spakky.plugins"]
my-plugin = "my_plugin.main:initialize"
# In my_plugin/main.py
from spakky.core.application.application import SpakkyApplication
def initialize(app: SpakkyApplication) -> None:
# Register plugin components
pass
Available Plugins
| Plugin | Description |
|---|---|
spakky-fastapi |
FastAPI integration |
spakky-kafka |
Apache Kafka event system |
spakky-rabbitmq |
RabbitMQ event system |
spakky-security |
Security utilities |
spakky-typer |
Typer CLI integration |
Core Modules
| Module | Description |
|---|---|
spakky.core.pod |
Dependency injection container and annotations |
spakky.core.aop |
Aspect-oriented programming framework |
spakky.core.application |
Application context and lifecycle |
spakky.core.stereotype |
Semantic stereotype annotations |
spakky.core.aspects |
Built-in aspects (Logging) |
spakky.core.service |
Service layer components |
spakky.core.common |
Core utilities (annotation, types, metadata) |
spakky.core.utils |
Utility functions |
Related Packages
| Package | Description |
|---|---|
spakky-domain |
DDD building blocks (Entity, AggregateRoot, ValueObject, Event) |
spakky-event |
Event handling (@EventHandler stereotype) |
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file spakky-5.0.0.tar.gz.
File metadata
- Download URL: spakky-5.0.0.tar.gz
- Upload date:
- Size: 31.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a37300d116125171fa04602deafb7466f015c5476bc44e9a59075d215069e399
|
|
| MD5 |
16177be3a176266ee5a0717500444d12
|
|
| BLAKE2b-256 |
0bd65690132b6507c1b906de312c9e79af7e6f8e075c592ec33ef21d508b004a
|
Provenance
The following attestation bundles were made for spakky-5.0.0.tar.gz:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky-5.0.0.tar.gz -
Subject digest:
a37300d116125171fa04602deafb7466f015c5476bc44e9a59075d215069e399 - Sigstore transparency entry: 747185253
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@93d2b3f52a27457bceab42b3ed44bc8404018b26 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@93d2b3f52a27457bceab42b3ed44bc8404018b26 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file spakky-5.0.0-py3-none-any.whl.
File metadata
- Download URL: spakky-5.0.0-py3-none-any.whl
- Upload date:
- Size: 53.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
12226ccf6601d0acf3d05ed88f5d3d7a44da704db4759f2fd2a412a562a3fe2b
|
|
| MD5 |
6212c471010975bba235bdabd6cc0758
|
|
| BLAKE2b-256 |
bb47d5fe2f89eb549be60e81f5f553c3f0e6fe9b360f7ee36e6cb29851c939f8
|
Provenance
The following attestation bundles were made for spakky-5.0.0-py3-none-any.whl:
Publisher:
release.yml on E5presso/spakky-framework
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
spakky-5.0.0-py3-none-any.whl -
Subject digest:
12226ccf6601d0acf3d05ed88f5d3d7a44da704db4759f2fd2a412a562a3fe2b - Sigstore transparency entry: 747185254
- Sigstore integration time:
-
Permalink:
E5presso/spakky-framework@93d2b3f52a27457bceab42b3ed44bc8404018b26 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/E5presso
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@93d2b3f52a27457bceab42b3ed44bc8404018b26 -
Trigger Event:
workflow_dispatch
-
Statement type: