Skip to main content

Python bindings for Cymbal code indexing and symbol discovery

Project description

py-cymbal: Python Bindings for Cymbal

Python bindings for the Cymbal code indexing and symbol discovery tool, wrapping the standalone binaries via a clean Python API.

Overview

Cymbal is a Go-based tool that uses tree-sitter for multi-language AST parsing and SQLite for indexed storage, providing fast symbol search, cross-references, impact analysis, and scoped diffs. These Python bindings allow Python developers to programmatically access Cymbal's powerful code analysis capabilities.

Features

  • Repository Indexing: Index code repositories for fast symbol lookup
  • Symbol Search: Search for functions, classes, variables, and other symbols
  • Symbol Investigation: Get detailed information about specific symbols including definitions and references
  • Reference Finding: Find all references to a particular symbol
  • Multi-language Support: Works with all languages supported by Cymbal (via tree-sitter)
  • Clean Python API: Pythonic interface with context managers and convenience functions

Installation

Prerequisites

  1. Bash/Curl/Unzip: Used by the build script to download binaries.
  2. Cymbal: The Go library being wrapped

Building from Source

# Clone the repository
git clone <repository-url>
cd py-cymbal

# Download the Cymbal binaries and create the Python package
./build.sh

# Install in development mode
pip install -e .

Build Script

The build.sh script automates the entire build process:

  1. Downloads the appropriate pre-compiled Cymbal binaries for Linux, macOS, and Windows.
  2. Places the binaries in the python/cymbal/bin/ directory.
  3. Creates a setup.py for pip installation that bundles the correct binary for your platform.

Usage

Basic Example

import cymbal

# Create a Cymbal instance
with cymbal.Cymbal() as c:
    # Index a repository
    stats = c.index("/path/to/your/repository")
    print(f"Indexing result: {stats}")
    
    # Search for symbols
    results = c.search("handleAuth", limit=10)
    for symbol in results:
        print(f"{symbol['name']} ({symbol['kind']}) at {symbol['file']}:{symbol['start_line']}")
    
    # Investigate a specific symbol
    investigation = c.investigate("UserModel")
    print(f"Symbol: {investigation['symbol']['name']}")
    print(f"References: {len(investigation['refs'])}")
    
    # Find references to a symbol
    references = c.find_references("DatabaseConnection", limit=20)
    for ref in references:
        print(f"Reference at {ref['file']}:{ref['line']}")

Convenience Functions

import cymbal

# Index a repository (one-liner)
stats = cymbal.index_repository("/path/to/repo")

# Search with existing database path
results = cymbal.search_symbols("config", limit=15, db_path="/path/to/index.db")

# Investigate a symbol
investigation = cymbal.investigate_symbol("ApiClient", db_path="/path/to/index.db")

Advanced Usage

import cymbal

# Reuse an existing index
try:
    c = cymbal.Cymbal()
    c.db_path = "/path/to/existing/index.db"  # Set path to existing database
    
    # Perform searches
    results = c.search("test", limit=5)
    
    # Process results
    for symbol in results:
        print(f"Found: {symbol['name']} in {symbol['file']}")
        
except Exception as e:
    print(f"Error: {e}")
finally:
    c.close()

API Reference

cymbal.Cymbal Class

__init__(repo_path=None)

Create a new Cymbal instance. Optionally index a repository immediately.

index(repo_path)

Index a repository. Returns statistics about the indexing operation.

search(query, limit=20)

Search for symbols matching the query. Returns a list of symbol results.

investigate(symbol_name)

Investigate a specific symbol. Returns an investigation result with definition and references.

find_references(symbol_name, limit=50)

Find references to a symbol. Returns a list of reference results.

db_path (property)

Get or set the current database path.

close()

Close the Cymbal instance and release resources.

Convenience Functions

index_repository(repo_path)

Convenience function to index a repository.

search_symbols(query, limit=20, db_path=None)

Convenience function to search for symbols.

investigate_symbol(symbol_name, db_path=None)

Convenience function to investigate a symbol.

Architecture

The Python package uses a subprocess-based architecture to wrap the standalone Cymbal binaries:

  1. Pre-compiled Binaries: Downloaded directly from the upstream Cymbal releases.
  2. Subprocess Wrapper: The Python API executes the binary with the appropriate flags (e.g., --json) and parses the output.
  3. Python API Layer (python/cymbal/__init__.py): Pythonic wrapper that provides a clean, intuitive interface.

File Structure

py-cymbal/
├── python/              # Python bindings
│   └── cymbal/          # Python module
│       ├── __init__.py  # Python API layer
│       └── bin/         # Downloaded Cymbal binaries
├── examples/            # Usage examples
│   └── basic_usage.py  # Basic usage demonstration
├── build.sh            # Build automation script
├── setup.py            # pip installation configuration
└── README.md           # This file

Contributing

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

Acknowledgments

  • Cymbal for the excellent code indexing tool
  • gopy for making Go-Python bindings possible
  • tree-sitter for robust parsing of multiple languages

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

py_cymbal-0.1.22-py3-none-win_amd64.whl (18.5 MB view details)

Uploaded Python 3Windows x86-64

py_cymbal-0.1.22-py3-none-manylinux_2_17_x86_64.whl (6.1 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

py_cymbal-0.1.22-py3-none-manylinux_2_17_aarch64.whl (5.9 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

py_cymbal-0.1.22-py3-none-macosx_11_0_arm64.whl (12.2 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

py_cymbal-0.1.22-py3-none-macosx_10_9_x86_64.whl (12.1 MB view details)

Uploaded Python 3macOS 10.9+ x86-64

File details

Details for the file py_cymbal-0.1.22-py3-none-win_amd64.whl.

File metadata

  • Download URL: py_cymbal-0.1.22-py3-none-win_amd64.whl
  • Upload date:
  • Size: 18.5 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for py_cymbal-0.1.22-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 4b114a8fe35fdc6509efa4e78b1ca8cbcacca5a44a12b75b7547c7ce32e795e9
MD5 11f2bf70d6b649c89e66e61c81b87329
BLAKE2b-256 4f470f5e3f665eafd4ae340147a0d1bc5339e7571af1eeaa55f2e5b91dd27b09

See more details on using hashes here.

File details

Details for the file py_cymbal-0.1.22-py3-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for py_cymbal-0.1.22-py3-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 d63af61312cfe0f2f56a5643a8234f5c267d107f1d80095b151a78c595859fd2
MD5 bdb64b0774a41ebb3e82d0ec84864441
BLAKE2b-256 70417bfba266b6f63cba263748afc4ed92e6ada9dcfe83f2aeb2f6d0106e6713

See more details on using hashes here.

File details

Details for the file py_cymbal-0.1.22-py3-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for py_cymbal-0.1.22-py3-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 97ff2e817f6cd056146b7ca2a67002fd26c277765836f0631b0f57f863df7d57
MD5 68e2ba1a18ac2112443a67499f47547b
BLAKE2b-256 331c3c265f693d4839a2530e366b5b1af6f4ce5b2dfe257a8572de82286bb6af

See more details on using hashes here.

File details

Details for the file py_cymbal-0.1.22-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for py_cymbal-0.1.22-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0ae1f60a6d45f2acb709bbf68619265053fb3e0df9eec8778d8abe2c4b123cfc
MD5 3cfa408677e9eb77c80b86c57b1d292a
BLAKE2b-256 c2f05d4e858cce0cd8513a0700e82dd110063c27a0b2f1a5db4e72c5865a5553

See more details on using hashes here.

File details

Details for the file py_cymbal-0.1.22-py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for py_cymbal-0.1.22-py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4b2b0961686d86f563cc2aaa616771e2a0fd8626aef2977e9522e2d1f37cbefc
MD5 4b4fcae925e1e9eab7a9ee9a0f9bfc1e
BLAKE2b-256 facb8874d6cf8529a73c72b07551333517d3b1a05a1a1c4ce3ddb929c9784d4d

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