Skip to main content

Extract source file skeletons using tree-sitter queries

Project description

Loppers

Simplified library for extracting source file skeletons using tree-sitter queries.

Removes function implementations while preserving structure and signatures.

Requires tree-sitter >= 0.25

Features

  • Functions & Methods - All regular and async functions
  • Constructors - Python __init__, Java/TS constructor()
  • Arrow Functions - JavaScript/TypeScript arrow functions
  • Class Methods - Instance, static, class methods
  • Docstrings - Python docstrings preserved
  • Decorators - Preserved (Python, Java)
  • Type Hints - Fully typed library
  • 11 Languages - Python, JS/TS, Java, Go, Rust, C/C++, C#, Ruby, PHP

See HANDLED_CASES.md for comprehensive list of supported cases.

Installation

With uv (recommended)

# Install from source
uv pip install -e .

# Or sync with pyproject.toml
uv sync

# Install with dev dependencies
uv sync --extra dev

With pip

pip install tree-sitter>=0.25.0 tree-sitter-language-pack

Usage

from loppers import extract

source: str = '''
def calculate(x: int, y: int) -> int:
    """Calculate sum."""
    result: int = x + y
    return result
'''

skeleton: str = extract(source, "python")
print(skeleton)

Output:

def calculate(x: int, y: int) -> int:
    """Calculate sum."""

Supported Languages

  • Python (with docstring preservation)
  • JavaScript / TypeScript (including arrow functions)
  • Java (including constructors)
  • Go
  • Rust
  • C / C++
  • C#
  • Ruby
  • PHP

Development

Setup

# Create environment and install dev dependencies
uv sync --extra dev

Testing with pytest

# Run all tests
uv run pytest

# Run with verbose output
uv run pytest -v

# Run with coverage
uv run pytest --cov=loppers --cov-report=html

# Run specific test
uv run pytest test_loppers.py::TestSkeletonExtractor::test_python_extraction

# Run with markers
uv run pytest -m "not slow"

Code Quality with ruff

# Check code style
uv run ruff check .

# Fix issues automatically
uv run ruff check . --fix

# Format code (included in check --fix)
uv run ruff format .

# Run all checks
uv run ruff check . && uv run ruff format .

How It Works

Uses tree-sitter queries to find and remove function/method body nodes while keeping:

  • Function signatures
  • Class definitions
  • Import statements
  • Python docstrings
  • Comments

API

extract(source_code: str, language: str) -> str

Extract skeleton from source code.

Args:

  • source_code: Source code to process
  • language: Programming language

Returns: Skeleton with implementations removed

Raises:

  • ValueError: If language not supported

SkeletonExtractor(language: str)

Create extractor for a specific language.

Methods:

  • extract(source_code: str) -> str: Extract skeleton

Raises:

  • ValueError: If language not supported

Adding Languages

To add a new language, extend LANGUAGE_CONFIGS with a tree-sitter query:

LANGUAGE_CONFIGS["newlang"] = LanguageConfig(
    name="newlang",
    body_query="(function_definition body: (block) @body)",
)

Find the correct query for your language by exploring the tree-sitter grammar.

Type Hints

This project uses comprehensive type hints throughout. All functions are fully typed.

# Example of type hints in use
def extract(source_code: str, language: str) -> str:
    """Extract skeleton from source code."""
    extractor: SkeletonExtractor = SkeletonExtractor(language)
    return extractor.extract(source_code)

Configuration Files

  • pyproject.toml: Project metadata, dependencies, pytest, and ruff configuration
  • HANDLED_CASES.md: Comprehensive list of supported cases
  • .gitignore: Git ignore rules
  • UV_GUIDE.md: uv package manager quick reference
  • DEVELOPMENT.md: Detailed development guide
.
├── loppers.py              # Core library (typed)
├── examples.py             # Usage examples (typed)
├── test_loppers.py         # Unit tests (typed)
├── pyproject.toml          # uv/pip + pytest + ruff config
├── .gitignore              # Git ignore rules
├── README.md               # This file
├── UV_GUIDE.md             # uv quick reference
└── requirements.txt        # Legacy requirements

tree-sitter API Changes (>= 0.25)

Key changes in the code for tree-sitter >= 0.25:

  • Language wrapping: Language(tree_sitter_language_pack.get_language(...))
  • Parser language assignment: parser.language = lang (property instead of set_language())

References

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

loppers-0.1.0.tar.gz (44.7 kB view details)

Uploaded Source

Built Distribution

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

loppers-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file loppers-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for loppers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc3ac33daaa72d2f3c06a68308387986b557ed65a4671df8104bbfcdbe2f9074
MD5 f3e1ebc8e2c551d889000c9a9918d4e9
BLAKE2b-256 0bbbc25d23b9aa5989972096c6225f563c9098a48c42a19d57b2603f932aa0df

See more details on using hashes here.

Provenance

The following attestation bundles were made for loppers-0.1.0.tar.gz:

Publisher: publish.yml on undo76/loppers

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

File details

Details for the file loppers-0.1.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for loppers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44acd190665290981dad001286e4aaa8a6e16944e14cc143aca39b1f48532ee2
MD5 33e0c9e78f8d66d5ed33522ff26ced51
BLAKE2b-256 b08d13f2d3589fa992ae6327e3ddede44be8b532df6e8b3ec73b4a145990da1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for loppers-0.1.0-py3-none-any.whl:

Publisher: publish.yml on undo76/loppers

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