Skip to main content

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

Metadata

Release files for py-cymbal 0.2.1

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

Built distributions (wheels)

Table of built distributions (wheels) for py-cymbal 0.2.1
File
py_cymbal-0.2.1-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details
py_cymbal-0.2.1-py3-none-manylinux_2_17_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details
py_cymbal-0.2.1-py3-none-manylinux_2_17_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details
py_cymbal-0.2.1-py3-none-macosx_11_0_arm64.whl Python 3 none macOS 11.0+ ARM64 Details
py_cymbal-0.2.1-py3-none-macosx_10_9_x86_64.whl Python 3 none macOS 10.9+ x86-64 Details

Total release size: 70.8 MB

Release files / py_cymbal-0.2.1-py3-none-win_amd64.whl

Download URL py_cymbal-0.2.1-py3-none-win_amd64.whl
Size 23.8 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
c6ef25d252fe762fd9cfd2c73f1e1686ef29deb8229fb48c5d1f7e358526f78d
BLAKE2b-256 checksum
How to use checksums
9b3ef87f6f063a37261c609a4935b38315088bce4106732ac78b3849280d4b44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / py_cymbal-0.2.1-py3-none-manylinux_2_17_x86_64.whl

Download URL py_cymbal-0.2.1-py3-none-manylinux_2_17_x86_64.whl
Size 8.1 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
bf5859c1fd3859307848e3ae841ee8c65ecad836a38e4b8b12a453ee112cba80
BLAKE2b-256 checksum
How to use checksums
5fe2bc2af5ab815c61b19f9b5c62de3bb769e8200e160930b7a4000301ac1975
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / py_cymbal-0.2.1-py3-none-manylinux_2_17_aarch64.whl

Download URL py_cymbal-0.2.1-py3-none-manylinux_2_17_aarch64.whl
Size 7.6 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
0d1c85d0c75977df66753fb7c9c3cc3a01601c54547e7c24765a669313f5f219
BLAKE2b-256 checksum
How to use checksums
945479c377b63d94dab328a385740f52ef5942fdf267831cfd39582177f80a8e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / py_cymbal-0.2.1-py3-none-macosx_11_0_arm64.whl

Download URL py_cymbal-0.2.1-py3-none-macosx_11_0_arm64.whl
Size 15.6 MB
Tags Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
fbc24d201c751fbb422c584e6b4a9722492447fb8c67cde972a10ecf8be7b2d8
BLAKE2b-256 checksum
How to use checksums
86524c6b1fdafa0712384a1918ac92a2acf7f0d8f36bcf2d34e9593430c5c469
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / py_cymbal-0.2.1-py3-none-macosx_10_9_x86_64.whl

Download URL py_cymbal-0.2.1-py3-none-macosx_10_9_x86_64.whl
Size 15.6 MB
Tags Python 3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
ea8d55544477130220e3e823967bd80795801d549a0bf6848e9fb0bd5410dcc2
BLAKE2b-256 checksum
How to use checksums
807601819471785eb5b539665aadc9a9934eec7f141fda2556418633c4b68fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.2.1 This release

5 release files

0.2.0

5 release files

0.1.24

5 release files

0.1.23

5 release files

0.1.22

5 release files

0.1.20

5 release files

0.1.19

5 release files

0.1.17

1 release file

0.1.16

1 release file

0.1.15

1 release file

0.1.14

1 release file

0.1.13

1 release file

0.1.11

1 release file

0.1.10

1 release file

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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