-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hrshdhgd/commenter
Commenter and Explainer
- Loading branch information
Showing
12 changed files
with
407 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,88 @@ | ||
# CoderGPT | ||
# CoderGPT CLI | ||
|
||
A language agnostic code optimizer. | ||
## Description | ||
|
||
CoderGPT CLI is a command line interface for CoderGPT, a state-of-the-art code generation tool. It allows developers to interact with the CoderGPT functionalities directly from the terminal, streamlining their workflow and enhancing productivity. | ||
|
||
## Author | ||
|
||
Harshad Hegde | ||
|
||
## Installation | ||
|
||
To use the CoderGPT CLI, clone the repository and install the required dependencies. | ||
|
||
```shell | ||
pip install codergpt | ||
``` | ||
|
||
## Usage | ||
|
||
Run the CLI using the following syntax: | ||
|
||
```shell | ||
code [OPTIONS] COMMAND [ARGS]... | ||
``` | ||
|
||
### Options | ||
|
||
- `-v, --verbose INTEGER`: Set verbosity level (0, 1, or 2). | ||
- `-q, --quiet`: Enable quiet mode. | ||
- `--version`: Display version information. | ||
|
||
### Commands | ||
|
||
#### inspect | ||
|
||
Inspect a package and display a file-language map. | ||
|
||
```shell | ||
code inspect <path> | ||
``` | ||
|
||
#### explain | ||
|
||
Explain a specific function or class within a package. | ||
|
||
```shell | ||
code explain <path> [--function <function_name>] [--classname <class_name>] | ||
``` | ||
|
||
#### comment | ||
|
||
Add comments to the code in a package. | ||
|
||
```shell | ||
code comment <path> [--overwrite/--no-overwrite] | ||
``` | ||
|
||
## Development | ||
|
||
The CLI is built using Python and the `click` library. Below is an example of how to define a new command: | ||
|
||
```python | ||
import click | ||
from codergpt import CoderGPT | ||
|
||
coder = CoderGPT() | ||
|
||
@click.command() | ||
@click.argument('path', type=click.Path(exists=True)) | ||
def new_command(path): | ||
# Command logic here | ||
pass | ||
``` | ||
|
||
## Contributing | ||
|
||
Contributions are welcome! Please read our contributing guidelines before submitting pull requests. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the LICENSE.md file for details. | ||
|
||
# Acknowledgements | ||
|
||
This [cookiecutter](https://cookiecutter.readthedocs.io/en/stable/README.html) project was developed from the [monarch-project-template](https://github.com/monarch-initiative/monarch-project-template) template and will be kept up-to-date using [cruft](https://cruft.github.io/cruft/). | ||
|
||
For more information on CoderGPT CLI, please visit [the official documentation](). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
"""Commenter module for the package.""" | ||
|
||
from .commenter import CodeCommenter | ||
|
||
__all__ = ["CodeCommenter"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Commenter Module.""" | ||
|
||
import os | ||
from typing import Any, Dict | ||
|
||
from langchain_core.runnables.base import RunnableSerializable | ||
|
||
|
||
class CodeCommenter: | ||
"""Code Explainer class that extracts and explains code from a given file.""" | ||
|
||
def __init__(self, chain: RunnableSerializable[Dict, Any]): | ||
""" | ||
Initialize the CodeExplainer class with a runnable chain. | ||
:param chain: A RunnableSerializable object capable of executing tasks. | ||
""" | ||
self.chain = chain | ||
|
||
def comment(self, code: str, filename: str, overwrite: bool = False): | ||
""" | ||
Comment the contents of the code string by invoking the runnable chain and write it to a new file. | ||
:param code: The string containing the code to be commented. | ||
:param filename: The original filename of the code file. | ||
:param overwrite: A boolean indicating whether to overwrite the original file. Default is False. | ||
""" | ||
response = self.chain.invoke( | ||
{ | ||
"input": f"Rewrite and return this code with\ | ||
comments and docstrings in :param: format: \n{code}\n" | ||
} | ||
) | ||
|
||
# Extract the commented code from the response if necessary | ||
commented_code = response.content | ||
|
||
new_filename = filename | ||
if not overwrite: | ||
# Create a new filename with the _updated suffix | ||
base, ext = os.path.splitext(filename) | ||
new_filename = f"{base}_updated{ext}" | ||
|
||
# Write the commented code to the new file | ||
with open(new_filename, "w") as updated_file: | ||
updated_file.write(commented_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.