Skip to main content

capdag-py

Python implementation of the capdag (Cap SDK) - Core cap URN and definition system for MACHFAB cartridges.

This library provides the fundamental cap URN system used across all MACHFAB cartridges and candidates. It defines the formal structure for cap identifiers with flat tag-based naming, wildcard support, and specificity comparison.

Features

  • MediaUrn: Media type definition using tagged URN format with "media" prefix
  • CapUrn: Cap identifiers with required in and out direction specs
  • Semantic matching with wildcard support
  • Specificity-based selection for finding best matching capabilities
  • Full compatibility with the Rust reference implementation

Installation

# Install in development mode
pip install -e .

# With development dependencies
pip install -e ".[dev]"

Dependencies

  • tagged-urn: Flat tag-based URN system (Python implementation)

Usage

from capdag 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";generate;out="media:record;textable"

# Matching semantics
request = CapUrn.from_string('cap:generate;in=media:textable;out="media:record;textable"')
print(cap.accepts(request))  # True

# Specificity
print(cap.specificity())  # Higher score = more specific

Testing

Tests 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=capdag tests/

Test Coverage

The implementation includes comprehensive tests covering:

CapUrn Tests (TEST001-TEST052) - 52 tests ✅

  • TEST001-TEST039: Core CapUrn functionality, parsing, serialization, matching
  • TEST040-TEST052: Advanced matching semantics and direction handling

MediaUrn Tests (TEST057-TEST078, TEST304-TEST306) - 25 tests ✅

  • TEST057-TEST078: MediaUrn parsing, type checking, matching, specificity
  • TEST304-TEST306: Additional media URN constant validation

Cap Tests (TEST108-TEST116) - 9 tests ✅

  • TEST108-TEST116: Cap creation, arguments, stdin, output, metadata, serialization

Manifest Tests (TEST148-TEST155) - 8 tests ✅

  • TEST148-TEST155: Manifest creation, serialization, multiple caps, optional fields

Response Tests (TEST168-TEST170) - 3 tests ✅

  • TEST168-TEST170: Response wrapper for JSON, primitives, and binary data

Standard Caps Tests (TEST307-TEST312) - 6 tests ✅

  • TEST307-TEST312: Standard URN builders for capabilities

Validation Tests (TEST051-056) - 14 tests ✅

  • TEST051-056: Input validation, argument checking, validation rules
  • RULE1-RULE10: Comprehensive validation rule enforcement

Schema Validation Tests (TEST163-167) - 10 tests ✅

  • TEST163-167: JSON schema validation for arguments and outputs

Caller Tests (TEST156-162, TEST274-283) - 21 tests ✅

  • TEST156-162: StdinSource variants and debugging
  • TEST274-283: CapArgumentValue creation and handling

Registry Tests (TEST135-147) - 17 tests ✅

  • TEST135-147: Registry creation, caching, URL encoding, configuration

Total: 165/308 passing tests (54% complete)

Architecture

MediaUrn

Media URNs use the "media" prefix and describe data types using tags:

media:<markers>[;key=value]*

Examples:

  • media:bytes - Binary data
  • media:textable - Text, single value
  • media:bytes;ext=pdf - PDF document (binary)

CapUrn

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";test;out="media:void" - No-op test capability
  • cap:in="media:bytes;ext=pdf";generate-thumbnail;out="media:bytes;ext=png;image;thumbnail" - PDF thumbnail generator

Matching Semantics

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

Development

The Python implementation follows these principles:

  1. Production code only - No placeholders, TODOs, or stopgaps
  2. Root cause fixes - Issues are fixed at their source, not worked around
  3. Python conventions - Follows Pythonic patterns while maintaining semantic compatibility
  4. Test-driven - Tests expose issues and track completion
  5. Cross-language compatibility - Tests numbered to match Rust reference implementation

License

MIT

Reference

Metadata

Release files for capdag 1.284.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for capdag 1.284.0
File Size Uploaded
capdag-1.284.0.tar.gz 548.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for capdag 1.284.0
File Interpreter ABI Platform
capdag-1.284.0-py3-none-any.whl Python 3 none any Details

Total release size: 894.8 kB

Release files / capdag-1.284.0.tar.gz

Download URL capdag-1.284.0.tar.gz
Size 548.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e6456aa160e193a2472cbfa278a7271ffc987987d1d181b42915443202efa5cc
BLAKE2b-256 checksum
How to use checksums
a0359b7b777475f55d8f89607aca7e803f3f8f081b7dadcb31ee42bf1990b89e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / capdag-1.284.0-py3-none-any.whl

Download URL capdag-1.284.0-py3-none-any.whl
Size 346.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a626e03d300dd103837112d86aad88858f1a65c0d355976db27761d4ad01f08
BLAKE2b-256 checksum
How to use checksums
06d83e2949fd6fa3700d743ff7e040739955ef882985a07369b7f595bb5dd573
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

1.284.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page