Merge pull request #15 from TheR1D/Linting-Pre-Push

Linting code with pre-push hook and workflow
This commit is contained in:
Farkhod Sadykov
2023-02-09 22:10:41 +01:00
committed by GitHub
4 changed files with 91 additions and 15 deletions
+28
View File
@@ -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
Executable
+41
View File
@@ -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.'
+1 -3
View File
@@ -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",
+21 -12
View File
@@ -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."),