AutoCommenter is a Python script designed to automatically generate comments for code files in various programming languages. It leverages OpenAI's GPT model to analyze code files, generate meaningful comments, and insert them into the code. Commenting guidelines are specified through a GPT prompt, and can be customized to suit your needs.
- Analyzes code files to understand functionality and logic.
- Generates comments that provide context, explain purposes, and avoid redundancy.
- Uses XML documentation for public classes, methods, and properties.
- Processes multiple directories and files automatically.
- Allows processing of a single file via command-line argument.
- Supports multiple programming languages by specifying file extensions and prompt files.
- Python 3.7 or higher
openai
librarypython-dotenv
library
-
Clone the repository:
git clone https://github.com/yourusername/AutoCommenter.git cd AutoCommenter
-
Install the required Python packages:
pip install openai python-dotenv
-
Set your OpenAI API key:
- Preferred: Set the
OPENAI_API_KEY
as a system environment variable. - Fallback: Create a
.env
file in the root directory and add your OpenAI API key:OPENAI_API_KEY=your_openai_api_key
- Preferred: Set the
-
Create a
config.json
file in the root directory and add your configuration:{ "root_dir": "/path/to/your/root/dir", "sub_dirs": ["Services", "Ui", "Models", "Pages", "ViewModels"], "dst_root_dir": "/path/to/your/destination/dir", "extensions": [".cs", ".py", ".js"], "prompt": "prompt-netmaui.txt" }
-
Create the default prompt file (e.g.,
prompt-netmaui.txt
) and any other language-specific prompt files (e.g.,prompt-python.txt
) in the root directory and add your prompt text.
-
Prepare the script for execution:
-
Ensure your directory structure is set up as expected. The script will process files in the
root_dir
directory by default, including subdirectoriesServices
,Ui
,Models
,Pages
, andViewModels
. -
Modify the
config.json
file if your directory structure or desired file extensions are different.
-
-
Run the script:
python autocomment.py
-
Specify the file and language on the command line:
python autocomment.py -f /path/to/your/file.cs -l python
-
Script Behavior:
- The script reads each specified file in the configured directories, or the single file specified via the command line.
- It sends the file content to the OpenAI model configured with guidelines for commenting.
- It extracts the commented code and writes it back to the corresponding file.
You can modify the commenting guidelines in the prompt file specified in the config.json
file. The autocommenter.configure
method sets the rules and examples that the AI model will follow when generating comments.
To process specific files or additional directories, adjust the root_dir
, sub_dirs
, extensions
, and dst_root_dir
in the config.json
file.
Here’s a quick example of how you can set up and run the script:
# Clone the repository
git clone https://github.com/jurgenskrause/AutoCommenter.git
cd AutoCommenter
# Install dependencies
pip install openai python-dotenv
# Set your OpenAI API key in the environment
export OPENAI_API_KEY=your_openai_api_key
# Alternatively, add your OpenAI API key in the .env file
echo "OPENAI_API_KEY=your_openai_api_key" > .env
# Create config.json file
echo '{
"root_dir": "/path/to/your/root/dir",
"sub_dirs": ["Services", "Ui", "Models", "Pages", "ViewModels"],
"dst_root_dir": "/path/to/your/destination/dir",
"extensions": [".cs", ".py", ".js"],
"prompt": "prompt-netmaui.txt"
}' > config.json
# Create the specified prompt file and add your prompt text
# Run the script to process all files
python autocomment.py
# Run the script to process a single file
python autocomment.py -f /path/to/your/file.cs -l python