Skip to main content

A Pythonic query builder and ORM-like abstraction layer for ShotGrid/Flow Production Tracking API

Project description

ShotGrid Query Builder

PyPI version Python Versions Downloads Downloads/Month License: MIT Tests codecov Code style: ruff Type checked: mypy GitHub stars GitHub forks

A Pythonic query builder and ORM-like abstraction layer for ShotGrid/Flow Production Tracking API.

中文文档

🎯 Features

  • Pythonic API: Chainable query builder with intuitive method names
  • Type Safety: Full type hints and Pydantic validation
  • Flexible: Works with both shotgun_api3 and raw filter dictionaries
  • Powerful: Support for complex filters, related fields, and time-based queries
  • Lightweight: Minimal dependencies, can be used independently
  • Well-tested: Comprehensive test suite with >90% coverage

📦 Installation

# Basic installation
pip install shotgrid-query

# With ShotGrid API support
pip install shotgrid-query[shotgrid]

# For development
pip install shotgrid-query[dev]

🚀 Quick Start

Traditional ShotGrid API (Before)

import shotgun_api3

sg = shotgun_api3.Shotgun(url, script_name, api_key)

# Complex filter syntax
filters = [
    ["sg_status_list", "is", "ip"],
    ["project", "is", {"type": "Project", "id": 123}],
    ["created_at", "in_last", [7, "DAY"]]
]
fields = ["code", "description", "project.Project.name"]
result = sg.find("Shot", filters, fields)

With shotgrid-query (After)

from shotgrid_query import Query

# Chainable, readable query builder
query = (
    Query("Shot")
    .filter(sg_status_list="ip")
    .filter(project_id=123)
    .filter(created_at__in_last="7 days")
    .select("code", "description")
    .select_related("project", fields=["name"])
    .order_by("-created_at")
    .limit(100)
)

# Execute with shotgun_api3 connection
result = query.execute(sg)

# Or get raw filters/fields for manual use
filters = query.to_filters()  # Returns list of tuples
fields = query.to_fields()    # Returns list of field names

🔍 Schema Validation

Validate your queries against ShotGrid schema before execution to catch errors early:

from shotgrid_query import Query

# Development mode - auto-validate on every operation
query = Query("Shot", sg=sg, auto_validate=True)
query.filter(code="SHOT_010").select("code", "description")

# Validation happens automatically, raises ValueError if invalid
result = query.execute(sg)

Manual Validation

# Create query with schema
schema = sg.schema_field_read("Shot")
query = Query("Shot", schema=schema)
query.filter(code="SHOT_010").select("invalid_field")

# Manually validate
errors = query.validate()
if errors:
    for error in errors:
        print(f"❌ {error.field}: {error.message}")
        if error.suggestion:
            print(f"💡 {error.suggestion}")
else:
    result = query.execute(sg)

Validation on Demand

# Validate when converting to filters/fields
try:
    filters = query.to_filters(verify=True)
    fields = query.to_fields(verify=True)
except ValueError as e:
    print(f"Validation failed:\n{e}")

Detailed Error Messages

When validation fails, you get helpful error messages:

# Field doesn't exist
 invalid_field: Field does not exist in Shot
💡 Available fields: code, description, id, project, sg_status_list (and 45 more)

# Operator not compatible with field type
 code: Operator 'greater_than' is not valid for field type 'text'
💡 Valid operators for text fields: contains, ends_with, in, is, is_not, not_contains, not_in, starts_with

# Value type mismatch
 id: Value must be a number, got str
💡 Example: query.filter(id=123)

Schema Caching

Schema is automatically cached to avoid repeated API calls:

from shotgrid_query import SchemaCache

# Set custom TTL (default: 1 hour)
SchemaCache.set_ttl(3600)

# Clear cache if needed
SchemaCache.clear()

📚 Documentation

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Development Setup

  1. Clone the repository:
git clone https://github.com/loonghao/shotgrid-query.git
cd shotgrid-query
  1. Install dependencies:
pip install -e ".[dev]"
  1. Install pre-commit hooks:
pre-commit install

This will automatically run code formatting and linting checks before each commit.

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src/shotgrid_query --cov-report=term-missing

# Run specific test file
pytest tests/test_query.py -v

Code Quality

# Format code
ruff format src tests

# Lint code
ruff check src tests

# Type check
mypy src/shotgrid_query

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

This library was extracted from shotgrid-mcp-server to provide a reusable query builder for the ShotGrid community.

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

shotgrid_query-0.3.0.tar.gz (81.7 kB view details)

Uploaded Source

Built Distribution

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

shotgrid_query-0.3.0-py3-none-any.whl (31.9 kB view details)

Uploaded Python 3

File details

Details for the file shotgrid_query-0.3.0.tar.gz.

File metadata

  • Download URL: shotgrid_query-0.3.0.tar.gz
  • Upload date:
  • Size: 81.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shotgrid_query-0.3.0.tar.gz
Algorithm Hash digest
SHA256 b4a5e48700ee66e5c3df53879b30e115015b239c217b055456b70bec8aca980d
MD5 8a0577396fd75ec00986f2bed3110c5f
BLAKE2b-256 56c2d754207a8e015c747280c156ae91c33c42560da93f30009d5a25614759ba

See more details on using hashes here.

Provenance

The following attestation bundles were made for shotgrid_query-0.3.0.tar.gz:

Publisher: publish.yml on loonghao/shotgrid-query

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file shotgrid_query-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: shotgrid_query-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 31.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for shotgrid_query-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a95386509d2a86cc54e0367c0ddd5f7a30ceb6c8ccd24d3caf32d42e713739ca
MD5 2257d0ce88bc7df437b28ad0839f60db
BLAKE2b-256 ee1344bbd57f5e5ad2089fa5d850643f1c2e050fca9ef01722d44eb1233aff9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for shotgrid_query-0.3.0-py3-none-any.whl:

Publisher: publish.yml on loonghao/shotgrid-query

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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