Skip to main content

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)

Installation

Using pip / pipx

The simplest way to install CodeHem:

pip install codehem

or

pipx install codehem

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
## Usage Example

### Querying Code

handler = CodeHem('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


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.

Metadata

Release files for CodeHem 0.1.6.1

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

Source distribution (sdist)

Source distribution for CodeHem 0.1.6.1
File Size Uploaded
codehem-0.1.6.1.tar.gz 98.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for CodeHem 0.1.6.1
File Interpreter ABI Platform
codehem-0.1.6.1-py3-none-any.whl Python 3 none any Details

Total release size: 253.8 kB

Release files / codehem-0.1.6.1.tar.gz

Download URL codehem-0.1.6.1.tar.gz
Size 98.7 kB
Tags Source
SHA-256 checksum
How to use checksums
dc73d8e0f0b0971f99345e6fa29641e0cd91621aa3431b06f4dbdce80dbd23de
BLAKE2b-256 checksum
How to use checksums
22455045ecf5eac5c86b93abdadb057e899995ff48b1db3d9f325d4f65e826d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / codehem-0.1.6.1-py3-none-any.whl

Download URL codehem-0.1.6.1-py3-none-any.whl
Size 155.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
33183625ad4164c31af97d0b2e2776a328a3f6a8c011e59f661ac9495ceb0e46
BLAKE2b-256 checksum
How to use checksums
40bf505270fb75431d5515db14add4c34e1926e47217ceef73241617d559f848
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.1.6.1 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

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