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.2.0.tar.gz (81.2 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.2.0-py3-none-any.whl (31.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: shotgrid_query-0.2.0.tar.gz
  • Upload date:
  • Size: 81.2 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.2.0.tar.gz
Algorithm Hash digest
SHA256 1dcaf5104704d99c54f07ff259171f4af9ee92bc451e3a0c9425243d7075cf9e
MD5 290572c295a02d14c94b0efa0113d73a
BLAKE2b-256 73cb168aaf62a39b83b4f57e26585bf6483e0b597ea7d96f6c924940da7f4d15

See more details on using hashes here.

Provenance

The following attestation bundles were made for shotgrid_query-0.2.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: shotgrid_query-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 31.6 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4187798a98f17ba0b691aa328d45e976b09f81340fb86e85df158a3f98f6087c
MD5 1911956b7fb732292355235ae9468119
BLAKE2b-256 cf5a5e5c564802bd9ea466bdc4e052c3000a247a3b935baec38ce49f56d81797

See more details on using hashes here.

Provenance

The following attestation bundles were made for shotgrid_query-0.2.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