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
- Bash/Curl/Unzip: Used by the build script to download binaries.
- 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:
- Downloads the appropriate pre-compiled Cymbal binaries for Linux, macOS, and Windows.
- Places the binaries in the
python/cymbal/bin/directory. - Creates a
setup.pyfor 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:
- Pre-compiled Binaries: Downloaded directly from the upstream Cymbal releases.
- Subprocess Wrapper: The Python API executes the binary with the appropriate flags (e.g.,
--json) and parses the output. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_cymbal-0.1.24-py3-none-win_amd64.whl.
File metadata
- Download URL: py_cymbal-0.1.24-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ffe63ad693247ee548f93f2e1684180ef220c6fd696d7b01522df8edc2db6044
|
|
| MD5 |
0affd985a852e6ce8d5314e913c8ef55
|
|
| BLAKE2b-256 |
13e6006cb87da3f510668cef1e5e1da54bde11b9f070502667fe997ca0bf5ca1
|
File details
Details for the file py_cymbal-0.1.24-py3-none-manylinux_2_17_x86_64.whl.
File metadata
- Download URL: py_cymbal-0.1.24-py3-none-manylinux_2_17_x86_64.whl
- Upload date:
- Size: 6.1 MB
- Tags: Python 3, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4172dc54b1a4d1072bc7ef1f762abd1d639c2d052661df907912a38890ab72f1
|
|
| MD5 |
72b1c9d2c58834c54a2dc6e8b15f3d28
|
|
| BLAKE2b-256 |
7052646d527a501468562110aa10ca8805b01f11cada5b9de87fad428c0b8ab4
|
File details
Details for the file py_cymbal-0.1.24-py3-none-manylinux_2_17_aarch64.whl.
File metadata
- Download URL: py_cymbal-0.1.24-py3-none-manylinux_2_17_aarch64.whl
- Upload date:
- Size: 5.9 MB
- Tags: Python 3, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f15e65b041ce731c1849ea10ae6a142940baf83cb368b820c90e2d039bf8bd05
|
|
| MD5 |
bf914f399221c2c7d92f29f57b2291be
|
|
| BLAKE2b-256 |
b4dba7e829202a3aa16435966db759c5db3ced62f14b873155d4932f470d765c
|
File details
Details for the file py_cymbal-0.1.24-py3-none-macosx_11_0_arm64.whl.
File metadata
- Download URL: py_cymbal-0.1.24-py3-none-macosx_11_0_arm64.whl
- Upload date:
- Size: 12.2 MB
- Tags: Python 3, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d80ee589b9f338375ec3c68ebd96ce7b76b665694596f8518ff9265f1785d354
|
|
| MD5 |
2a4e25d373e6c62e7ea17f20f77b96db
|
|
| BLAKE2b-256 |
d90258e39f04acbd2bd344f2f6dbdd70d191346817e0cade3c8c0311d3eeca95
|
File details
Details for the file py_cymbal-0.1.24-py3-none-macosx_10_9_x86_64.whl.
File metadata
- Download URL: py_cymbal-0.1.24-py3-none-macosx_10_9_x86_64.whl
- Upload date:
- Size: 12.1 MB
- Tags: Python 3, macOS 10.9+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5fd4601fc5dd70bd1b96543069a82098cf86d957ebad36849f1b3061654fda5
|
|
| MD5 |
f51b22e69898f36e05006d9b3e912347
|
|
| BLAKE2b-256 |
c3cc1b6fc9a427ae36fa2d93b8fcc96dbed6fb0b0a102384b344138bfb7cc052
|