Metadata-Version: 2.4
Name: domain-aspects
Version: 0.1.0
Summary: Composable aspect decorators for any service: logging, auth, tenancy, throttling, error wrapping, sensitivity masking
Project-URL: Homepage, https://pypi.org/project/domain-aspects/
Project-URL: Repository, https://github.com/jekhator/domain-aspects
Project-URL: Issues, https://github.com/jekhator/domain-aspects/issues
Project-URL: Changelog, https://github.com/jekhator/domain-aspects/blob/main/CHANGELOG.md
Author: James Ekhator
License: Apache-2.0
License-File: LICENSE
Keywords: aspects,authorization,cross-cutting,decorators,error-handling,logging,security,throttling
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: domain-errors>=0.2.0
Provides-Extra: all
Requires-Dist: domain-api-limiter>=0.2.0; extra == 'all'
Requires-Dist: domain-security>=0.2.0; extra == 'all'
Requires-Dist: mixin-logging>=0.6.0; extra == 'all'
Requires-Dist: mixin-sensitivity>=0.4.0; extra == 'all'
Provides-Extra: logging
Requires-Dist: mixin-logging>=0.6.0; extra == 'logging'
Provides-Extra: security
Requires-Dist: domain-security>=0.2.0; extra == 'security'
Provides-Extra: sensitivity
Requires-Dist: mixin-sensitivity>=0.4.0; extra == 'sensitivity'
Provides-Extra: throttle
Requires-Dist: domain-api-limiter>=0.2.0; extra == 'throttle'
Description-Content-Type: text/markdown

Domain Aspects

Composable cross-cutting decorators for logging, authorization, tenancy, throttling, error handling, and sensitive field masking. Declare your service's aspect requirements once; apply them consistently.

Installation

pip install domain-aspects

Optional dependencies for specific aspects:

pip install domain-aspects[logging]
pip install domain-aspects[security]
pip install domain-aspects[throttle]
pip install domain-aspects[sensitivity]
pip install domain-aspects[all]

Example

Define a service class with a class-level frozenset constant of aspects, then apply them to the class:

from dataclasses import dataclass
from mixin_logging import LoggingMixin
from domain_aspects import aspects, Logged, Sensitive

DOCUMENT_SERVICE_ASPECTS = frozenset({
    Logged(event="document.service"),
    Sensitive(),
})

@aspects(DOCUMENT_SERVICE_ASPECTS)
@dataclass(frozen=True, slots=True)
class DocumentService(LoggingMixin):
    repository: object

    def process(self, doc_id: str) -> str:
        return f"Processed {doc_id}"

service = DocumentService(repository=None)
result = service.process("doc-123")
print(f"Result: {result}")

Output:

document.service.start
Result: Processed doc-123
document.service.success

Aspect Kinds

domain-aspects supports six composable aspects:

- LOGGED: Emit entry/exit events via mixin-logging.
- REQUIRES: Check permissions via domain-security.
- TENANT_SCOPED: Enforce tenant isolation via domain-security.
- THROTTLED: Apply rate limiting via domain-api-limiter.
- WRAP_ERRORS: Translate exceptions via domain-errors.
- SENSITIVE: Mask sensitive fields via mixin-sensitivity.

Entry Types

Each aspect is an immutable, hashable entry object:

- Logged(event: str): Lazy-import logging mixin, emit named event.
- Requires(permission: str): Check permission against context.
- TenantScoped(param_name: str = "tenant_id"): Enforce tenant boundary.
- Throttled(scope: str, rate: str, tiers: tuple[tuple[str, str], ...] = ()): Rate-limit by scope.
- WrapErrors(as_: type, catch: tuple[type[BaseException], ...] = (Exception,)): Catch and translate errors.
- Sensitive(): Mask sensitive fields in repr.

Composition

Declare aspects via the `aspects` decorator, which accepts:

- Single entries: `@aspects(Logged(event="test"))`
- frozenset: `@aspects(frozenset({Logged(...), Requires(...)}))`
- Mixed: `@aspects(Logged(...), frozenset({Requires(...)}), Sensitive())`

Aspects are applied in canonical order (innermost-to-outermost):

1. LOGGED (outermost: emits events)
2. REQUIRES (permission check)
3. TENANT_SCOPED (tenant enforcement)
4. THROTTLED (rate limiting)
5. SENSITIVE (field masking)
6. WRAP_ERRORS (innermost: exception translation)

License

Apache-2.0
