Skip to main content

A language-agnostic library for code querying and manipulation

Project description

CodeHem

CodeHem is a language-agnostic library designed for sophisticated querying and manipulation of source code. It provides a high-level interface to effortlessly navigate, analyze, and modify code elements such as functions, classes, methods, and properties across multiple programming languages, including Python, JavaScript, and TypeScript.

Key Features

  • Advanced Code Querying: Easily locate functions, classes, methods, properties, imports, and more within your source code, using a uniform, intuitive API.
  • Powerful Code Manipulation: Replace, add, or remove functions, methods, classes, properties, or entire code sections with minimal effort.
  • Syntax-aware Operations: Ensures accurate manipulation preserving syntax integrity through the tree-sitter parser.
  • Language Detection: Automatically identifies the programming language based on file extensions or code analysis.
  • Extensible Architecture: Easily add support for new programming languages through the strategy pattern.

Supported Languages

  • Python
  • JavaScript / TypeScript (including TSX)

Project Structure

CodeHem/
├── ast_handler.py            # Unified interface for AST operations
├── caching/                  # Performance optimization through caching
│   ├── __init__.py
│   └── cache_manager.py
│
├── finder/                   # Code element location
│   ├── base.py               # Abstract base class for querying code elements
│   ├── factory.py            # Factory for creating code finders
│   └── lang/
│       ├── python_code_finder.py
│       └── typescript_code_finder.py
│
├── formatting/               # Code formatting system
│   ├── __init__.py
│   ├── formatter.py          # Base formatter class
│   ├── python_formatter.py   # Python-specific formatter
│   └── typescript_formatter.py # TypeScript-specific formatter
│
├── language_handler.py       # High-level language handling interface (LangHem)
├── languages.py              # Language definitions and parsers
│
├── manipulator/              # Code manipulation
│   ├── abstract.py           # Abstract interface for code manipulators
│   ├── base.py               # Base implementation
│   ├── factory.py            # Factory for manipulators
│   └── lang/
│       ├── python_manipulator.py
│       └── typescript_manipulator.py
│
├── query_builder.py          # Unified query construction
│
├── strategies/               # Strategy pattern for language-specific operations
│   ├── __init__.py
│   ├── language_strategy.py  # Abstract strategy interface
│   ├── python_strategy.py    # Python-specific strategy
│   └── typescript_strategy.py # TypeScript-specific strategy
│
├── templates/                # Templates for adding new languages
│   └── new_language_template.py
│
└── utils/
    └── logs.py               # Logging utilities
## Installation

Ensure Python 3.7 or later is installed, then:

Dependencies include tree-sitter and language-specific parsers.

Usage Example

Querying Code

Create a handler for Python code

handler = LangHem('python')

code = ''' class Example: def greet(self): print("Hello") '''

Find method location

start, end = handler.finder.find_method(code, 'Example', 'greet') print(f'Method found from line {start} to {end}')


### Manipulating Code


handler = LangHem('python')

original_code = '''
def greet():
    print("Hello")
'''

new_function = '''
def greet():
    print("Hello, World!")
'''

modified_code = handler.manipulator.replace_function(original_code, 'greet', new_function)

Contributing

We warmly welcome contributions, whether it's through reporting issues, suggesting enhancements, or submitting pull requests. Feel free to participate!

License

This project is licensed under the MIT license. See LICENSE for details.

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

codehem-0.1.5.tar.gz (98.7 kB view details)

Uploaded Source

Built Distribution

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

codehem-0.1.5-py3-none-any.whl (154.9 kB view details)

Uploaded Python 3

File details

Details for the file codehem-0.1.5.tar.gz.

File metadata

  • Download URL: codehem-0.1.5.tar.gz
  • Upload date:
  • Size: 98.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for codehem-0.1.5.tar.gz
Algorithm Hash digest
SHA256 c6c116ae9160e8b8253eab5a92c851025e25950f47a4485a117125d6aad4e3b1
MD5 087f816a114aaa6aecb2d9931c8112a5
BLAKE2b-256 b6ae6163dc23d7245eb147ffcc17ded94c0b35b9ec35fd1e95eaebc7a0d2e056

See more details on using hashes here.

File details

Details for the file codehem-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: codehem-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 154.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for codehem-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4bbd55ae314def96520329f58864330184d1d9072ff6d8f9f00012b381f35d0d
MD5 e0472f9f8d72a282cc0b0f17b371cdcb
BLAKE2b-256 1718f2b9ed599e439271fc544a7738bd9a3b1bf64398ed679d9a9f4910a59b90

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