Skip to main content

Smithy document manipulation for Python

Project description

pylaag-smithy

Smithy document manipulation for Python.

Overview

pylaag-smithy provides comprehensive support for working with Smithy 2.0 specifications, including:

  • Document parsing and serialization (JSON)
  • Shape management (structures, operations, services)
  • Trait management
  • Operation management
  • Document validation

Installation

pip install pylaag-smithy

This will automatically install pylaag-core as a dependency.

Quick Start

from pylaag_smithy import SmithyDocument

# Parse a Smithy document
doc = SmithyDocument.from_json(smithy_json)
doc.validate()

# Access document properties
print(doc.smithy_version)
print(doc.shapes)

# Serialize back to JSON
json_output = doc.to_json()

Features

Document Management

from pylaag_smithy import SmithyDocument

# Create a new document
doc = SmithyDocument()

# Parse from JSON
doc = SmithyDocument.from_json(json_string)

# Validate document
doc.validate()  # Raises ValidationError if invalid

# Access properties
print(doc.smithy_version)
print(doc.shapes)

Shape Management

from pylaag_smithy import SmithyDocument, ShapeManager

doc = SmithyDocument()
shape_mgr = ShapeManager(doc)

# Add a structure shape
shape_mgr.add_shape('com.example#User', 'structure', {
    'members': {
        'id': {'target': 'smithy.api#Integer'},
        'name': {'target': 'smithy.api#String'},
        'email': {'target': 'smithy.api#String'}
    }
})

# Add a list shape
shape_mgr.add_shape('com.example#UserList', 'list', {
    'member': {'target': 'com.example#User'}
})

# Get a shape
user_shape = shape_mgr.get_shape('com.example#User')

# Resolve a target
resolved = shape_mgr.resolve_target('com.example#User')

# Remove a shape
shape_mgr.remove_shape('com.example#User')

Trait Management

from pylaag_smithy import SmithyDocument, TraitManager

doc = SmithyDocument()
trait_mgr = TraitManager(doc)

# Add a trait to a shape
trait_mgr.add_trait_to_shape('com.example#ListUsers', 'smithy.api#http', {
    'method': 'GET',
    'uri': '/users'
})

# Add documentation trait
trait_mgr.add_trait_to_shape('com.example#User', 'smithy.api#documentation', 
    'Represents a user in the system')

# Get a trait
http_trait = trait_mgr.get_trait('com.example#ListUsers', 'smithy.api#http')

# Remove a trait
trait_mgr.remove_trait_from_shape('com.example#ListUsers', 'smithy.api#http')

Operation Management

from pylaag_smithy import SmithyDocument, OperationManager

doc = SmithyDocument()
op_mgr = OperationManager(doc)

# Add an operation
op_mgr.add_operation(
    'com.example#ListUsers',
    input_shape='com.example#ListUsersInput',
    output_shape='com.example#ListUsersOutput',
    errors=['com.example#ValidationError']
)

# Get an operation
operation = op_mgr.get_operation('com.example#ListUsers')

# Remove an operation
op_mgr.remove_operation('com.example#ListUsers')

Extension Properties

from pylaag_smithy import SmithyDocument

doc = SmithyDocument()

# Set extension property
doc.set_extension('x-api-id', 'my-api-123')

# Get extension property
api_id = doc.get_extension('x-api-id')

# Remove extension property
doc.remove_extension('x-api-id')

API Reference

Classes

SmithyDocument

Main class for working with Smithy documents.

Class Methods:

  • from_json(json_str: str) -> SmithyDocument: Parse from JSON string

Methods:

  • validate() -> None: Validate the document structure
  • to_json(indent: int = 2) -> str: Serialize to JSON string

Properties:

  • smithy_version: str: The Smithy version
  • shapes: Dict[str, Any]: The shapes in the document

ShapeManager

Manages shapes in a Smithy document.

Methods:

  • add_shape(shape_id: str, shape_type: str, definition: Dict) -> None
  • remove_shape(shape_id: str) -> bool
  • get_shape(shape_id: str) -> Optional[Dict]
  • resolve_target(target: str) -> Optional[Dict]

TraitManager

Manages traits applied to shapes in a Smithy document.

Methods:

  • add_trait_to_shape(shape_id: str, trait_id: str, trait_value: Any) -> None
  • remove_trait_from_shape(shape_id: str, trait_id: str) -> bool
  • get_trait(shape_id: str, trait_id: str) -> Optional[Any]

OperationManager

Manages operations in a Smithy document.

Methods:

  • add_operation(operation_id: str, input_shape: Optional[str] = None, output_shape: Optional[str] = None, errors: Optional[List[str]] = None) -> None
  • remove_operation(operation_id: str) -> bool
  • get_operation(operation_id: str) -> Optional[Dict]

Requirements

  • Python 3.10 or higher
  • pylaag-core >= 0.1.0

License

MIT License - see LICENSE file for details

Related Packages

Contributing

Contributions are welcome! Please see the main repository for guidelines.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pylaag_smithy-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pylaag_smithy-0.1.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file pylaag_smithy-0.1.0.tar.gz.

File metadata

  • Download URL: pylaag_smithy-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pylaag_smithy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6476e76ba01cb670191db6b5155274c9d0cdb218b4095f0bc0a0c22874e36224
MD5 3f80aebd2571a7a39d8ad10882841b4f
BLAKE2b-256 3fba696df1f65f4b5e4bf2acd33e17e9f526f77ee6ee867e32c235822074f816

See more details on using hashes here.

File details

Details for the file pylaag_smithy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pylaag_smithy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pylaag_smithy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9edb92c8dc3e132bb90c95f4cfb91b498288bd189391ab0f147b8f47fae74c03
MD5 52dbc75fa7b730031d44cc238be89763
BLAKE2b-256 ce95d0a95dbb998dfdddbbda771056bbd5d376dfcbc5be6297d6ba08e75e6738

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page