From 7d923e3088a5de72ca7a4f43826734050c779bb0 Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Thu, 9 Feb 2023 21:59:19 +0100 Subject: [PATCH 1/2] Linting shell script, pre-push hook, minor changes --- pre-push.sh | 41 +++++++++++++++++++++++++++++++++++++++++ setup.py | 4 +--- sgpt.py | 33 +++++++++++++++++++++------------ 3 files changed, 63 insertions(+), 15 deletions(-) create mode 100755 pre-push.sh diff --git a/pre-push.sh b/pre-push.sh new file mode 100755 index 0000000..770043f --- /dev/null +++ b/pre-push.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +chmod +x "$PWD/pre-push.sh" +ln -sf "$PWD/pre-push.sh" .git/hooks/pre-push + +# Check if pylint is installed +if ! command -v pylint > /dev/null; then + echo "pylint is not installed. Install it with 'pip install pylint'" + exit 1 +fi + +# Check if black is installed +if ! command -v black > /dev/null; then + echo "black is not installed. Install it with 'pip install black'" + exit 1 +fi + +echo 'Executing pre-push hook.' + +if [ -f "$PWD/venv/bin/activate" ]; then + source $PWD/venv/bin/activate +fi + +if black --check --target-version py310 -l 120 sgpt.py +then + echo 'Black passed ✅' +else + echo 'Black failed ❌' + echo 'RUN: black --target-version py310 -l 120 sgpt.py' + exit 1 +fi + +if pylint sgpt.py +then + echo 'Pylint passed ✅' +else + echo 'Pylint failed ❌' + exit 1 +fi + +echo 'Well done.' \ No newline at end of file diff --git a/setup.py b/setup.py index 32aeb0d..5ee4109 100644 --- a/setup.py +++ b/setup.py @@ -13,9 +13,7 @@ setup( "rich==13.3.1", ], entry_points={ - 'console_scripts': [ - 'sgpt = sgpt:entry_point' - ], + "console_scripts": ["sgpt = sgpt:entry_point"], }, author="Farkhod Sadykov", author_email="farkhod@sadykov.dev", diff --git a/sgpt.py b/sgpt.py index 2f9dcb5..04a3e7e 100644 --- a/sgpt.py +++ b/sgpt.py @@ -1,3 +1,17 @@ +""" +shell-gpt: An interface to OpenAI's GPT-3 API + +This module provides a simple interface for OpenAI's GPT-3 API using Typer +as the command line interface. It supports different modes of output including +shell commands and code, and allows users to specify the desired OpenAI model +and length of the output. Additionally, it supports executing shell +commands directly from the interface. + +API Key is stored locally for easy use in future runs. +""" + +# pylint: disable=missing-function-docstring,too-many-arguments + import os from time import sleep from pathlib import Path @@ -9,7 +23,7 @@ from rich.progress import Progress, SpinnerColumn, TextColumn API_URL = "https://api.openai.com/v1/completions" -DATA_FOLDER = os.path.expanduser('~/.config') +DATA_FOLDER = os.path.expanduser("~/.config") KEY_FILE = Path(DATA_FOLDER) / "shell-gpt" / "api_key.txt" @@ -27,28 +41,23 @@ def loading_spinner(func): def wrapper(*args, **kwargs): if not kwargs.pop("spinner"): return func(*args, **kwargs) - with Progress( - SpinnerColumn(), - TextColumn("[green]Requesting OpenAI..."), - transient=True - ) as progress: + text = TextColumn("[green]Requesting OpenAI...") + with Progress(SpinnerColumn(), text, transient=True) as progress: progress.add_task("request") return func(*args, **kwargs) + return wrapper @loading_spinner def openai_request(prompt, model, max_tokens, api_key): - headers = { - "Content-Type": "application/json", - "Authorization": f"Bearer {api_key}" - } + headers = {"Content-Type": "application/json", "Authorization": f"Bearer {api_key}"} data = { "prompt": prompt, "model": model, "max_tokens": max_tokens, } - response = requests.post(API_URL, headers=headers, json=data) + response = requests.post(API_URL, headers=headers, json=data, timeout=5) response.raise_for_status() return response.json()["choices"][0]["text"] @@ -72,7 +81,7 @@ def main( model: str = typer.Option("text-davinci-003", help="OpenAI model name."), max_tokens: int = typer.Option(2048, help="Strict length of output (words)."), shell: bool = typer.Option(False, "--shell", "-s", help="Provide shell command as output."), - execute: bool = typer.Option(False, "--execute", "-e", help="Used with --shell, will execute command."), + execute: bool = typer.Option(False, "--execute", "-e", help="Will execute --shell command."), code: bool = typer.Option(False, help="Provide code as output."), animation: bool = typer.Option(True, help="Typewriter animation."), spinner: bool = typer.Option(True, help="Show loading spinner during API request."), From 0ce4656e9918271edb69c8f965414528f0ee5d19 Mon Sep 17 00:00:00 2001 From: Farkhod Sadykov Date: Thu, 9 Feb 2023 22:01:48 +0100 Subject: [PATCH 2/2] Added linting GitHub workflow --- .github/workflows/lint.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/lint.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..033e751 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,28 @@ +name: Lint + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.9", "3.10"] + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pylint + pip install black + pip install -r requirements.txt + - name: Analysing the code with pylint + run: | + pylint sgpt.py + - name: Analysing the code with black + run: | + black --target-version py310 -l 120 sgpt.py