Python implementation of the capns (Cap SDK) - Core cap URN and definition system for MACINA plugins.
This library provides the fundamental cap URN system used across all MACINA plugins and providers. It defines the formal structure for cap identifiers with flat tag-based naming, wildcard support, and specificity comparison.
- MediaUrn: Media type specification using tagged URN format with "media" prefix
- CapUrn: Cap identifiers with required
inandoutdirection specs - Semantic matching with wildcard support
- Specificity-based selection for finding best matching capabilities
- Full compatibility with the Rust reference implementation
# Install in development mode
pip install -e .
# With development dependencies
pip install -e ".[dev]"- tagged-urn: Flat tag-based URN system (Python implementation)
from capns import (
MediaUrn,
CapUrn,
CapUrnBuilder,
MEDIA_STRING,
MEDIA_OBJECT,
MEDIA_VOID,
)
# Create media URNs
string_urn = MediaUrn.from_string(MEDIA_STRING)
print(string_urn.is_text()) # True
print(string_urn.is_scalar()) # True
# Create cap URNs
cap = CapUrnBuilder() \
.in_spec(MEDIA_STRING) \
.out_spec(MEDIA_OBJECT) \
.tag("op", "generate") \
.tag("ext", "pdf") \
.build()
print(cap.to_string()) # cap:ext=pdf;in="media:textable;form=scalar";op=generate;out="media:form=map;textable"
# Matching semantics
request = CapUrn.from_string('cap:in="media:textable;form=scalar";out="media:form=map;textable";op=generate')
print(cap.accepts(request)) # True
# Specificity
print(cap.specificity()) # Higher score = more specificTests mirror the Rust implementation with matching TEST### numbers for cross-tracking:
# Run all tests
pytest tests/
# Run specific test files
pytest tests/test_cap_urn.py
pytest tests/test_media_urn.py
# Run with coverage
pytest --cov=capns tests/The implementation includes comprehensive tests covering:
- TEST001-TEST039: Core CapUrn functionality, parsing, serialization, matching
- TEST040-TEST052: Advanced matching semantics and direction handling
- TEST057-TEST078: MediaUrn parsing, type checking, matching, specificity
- TEST304-TEST306: Additional media URN constant validation
- TEST108-TEST116: Cap creation, arguments, stdin, output, metadata, serialization
- TEST148-TEST155: Manifest creation, serialization, multiple caps, optional fields
- TEST168-TEST170: Response wrapper for JSON, primitives, and binary data
- TEST307-TEST312: Standard URN builders for capabilities
- TEST051-056: Input validation, argument checking, validation rules
- RULE1-RULE10: Comprehensive validation rule enforcement
- TEST163-167: JSON schema validation for arguments and outputs
- TEST156-162: StdinSource variants and debugging
- TEST274-283: CapArgumentValue creation and handling
- TEST135-147: Registry creation, caching, URL encoding, configuration
Total: 165/308 passing tests (54% complete)
Media URNs use the "media" prefix and describe data types using tags:
media:<markers>[;key=value]*
Examples:
media:bytes- Binary datamedia:textable;form=scalar- Text, single valuemedia:pdf;bytes- PDF document (binary)
Cap URNs use the "cap" prefix and require in and out direction specs:
cap:in="<media-urn>";out="<media-urn>"[;key=value]*
Examples:
cap:in="media:void";op=test;out="media:void"- No-op test capabilitycap:in="media:pdf;bytes";op=generate_thumbnail;out="media:image;png;bytes;thumbnail"- PDF thumbnail generator
Matching follows tagged URN semantics with direction-aware rules:
- Input matching:
cap_input.accepts(request_input)- Does the cap's input requirement accept the request's data? - Output matching:
cap_output.conforms_to(request_output)- Does the cap's output conform to the request's expectation? - Missing tags are treated as wildcards (less specific, can handle any value)
- Specificity: Direction specs contribute their MediaUrn tag count; more tags = more specific
The Python implementation follows these principles:
- Production code only - No placeholders, TODOs, or stopgaps
- Root cause fixes - Issues are fixed at their source, not worked around
- Python conventions - Follows Pythonic patterns while maintaining semantic compatibility
- Test-driven - Tests expose issues and track completion
- Cross-language compatibility - Tests numbered to match Rust reference implementation
MIT
- Rust reference implementation: capns
- Tagged URN Python: tagged-urn-py
- Tagged URN Rust: tagged-urn-rs