From c551606503bcab0de79e88f73d25a9225422593c Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 19:43:47 +0000 Subject: [PATCH 01/10] Whitespace in line with flake8y --- clipea/__init__.py | 2 +- clipea/clipea_llm.py | 2 +- clipea/commands.py | 4 ++-- clipea/utils.py | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/clipea/__init__.py b/clipea/__init__.py index e9930aa..e4d96a5 100644 --- a/clipea/__init__.py +++ b/clipea/__init__.py @@ -1,5 +1,5 @@ """Clipea application -📎🟢 Like Clippy but for the CLI. A blazing fast AI helper for your command line +📎🟢 Like Clippy but for the CLI. A blazing fast AI helper for your command line """ import os from clipea import utils, cli diff --git a/clipea/clipea_llm.py b/clipea/clipea_llm.py index 144e4d2..2609f0b 100644 --- a/clipea/clipea_llm.py +++ b/clipea/clipea_llm.py @@ -57,7 +57,7 @@ def process_command(): current_command = command[2:new_line_pos] else: current_command = command[2:] - command = command[new_line_pos + 1 :] + command = command[new_line_pos + 1:] if output_file is not None: buffer += current_command + os.linesep diff --git a/clipea/commands.py b/clipea/commands.py index e049666..de8b26c 100644 --- a/clipea/commands.py +++ b/clipea/commands.py @@ -22,7 +22,7 @@ def setup(): ) else: print("An OpenAI key is already set-up, proceed if you want to change it.") - llm.cli.keys_set() + llm.cli.keys_set() def clipea_execute_prompt(user_prompt: str): @@ -43,7 +43,7 @@ def clipea_execute_prompt(user_prompt: str): from clipea import clipea_llm from llm import Model, Response - llm_name:str = '' + llm_name: str = '' if user_prompt.startswith('4 '): user_prompt = user_prompt[2:] llm_name = 'gpt-4' diff --git a/clipea/utils.py b/clipea/utils.py index 1fe54f2..5e7701a 100644 --- a/clipea/utils.py +++ b/clipea/utils.py @@ -16,6 +16,7 @@ def anystr_force_str(value: AnyStr) -> str: """ return value.decode("utf-8") if isinstance(value, bytes) else value + def read_file(file_path: str) -> str: """Reads a file From 1183c5788cd571a26a4b3a80e3f0c46122f1c38c Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 19:47:34 +0000 Subject: [PATCH 02/10] Add a quick test, to check the pipeline --- tests/__init__.py | 0 tests/test_utils.py | 6 ++++++ 2 files changed, 6 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_utils.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..3ae3622 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,6 @@ +from clipea import utils + + +def test_anystr_force_str(): + assert utils.anystr_force_str(b'hello') == 'hello' + assert utils.anystr_force_str('hello') == 'hello' From 93de76c270b3903a5294bb09068068906243db5c Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 19:53:50 +0000 Subject: [PATCH 03/10] Add requirements.txt --- requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ded3a7a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +llm==0.12 +setuptools==68.0.0 From 6f084ce5cd7eff6aab4ed94beb41211a084d6e96 Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:14:00 +0000 Subject: [PATCH 04/10] Linter fixes, dockblocks, clean up --- clipea/__main__.py | 9 +++++++-- clipea/commands.py | 13 +++++++------ tests/test_utils.py | 3 +++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/clipea/__main__.py b/clipea/__main__.py index b9a2dd2..eef29d9 100644 --- a/clipea/__main__.py +++ b/clipea/__main__.py @@ -6,11 +6,16 @@ def clipea_main() -> None: + """ + Main function for the Clipea. It checks if the "llm" dependency is installed + and then prompts the user for input. It then routes the user's input + to the appropriate command in the router. + """ if shutil.which("llm") is None: sys.exit('Error: dependency "llm" not found. Run "clipea setup" to install') - USER_PROMPT = " ".join(sys.argv[1:]) - router.commands_router(USER_PROMPT) + user_prompt = " ".join(sys.argv[1:]) + router.commands_router(user_prompt) if __name__ == "__main__": diff --git a/clipea/commands.py b/clipea/commands.py index de8b26c..728bdd8 100644 --- a/clipea/commands.py +++ b/clipea/commands.py @@ -8,7 +8,7 @@ def setup(): """Checks if `llm` has an openai key and prompt to change it or create one""" - import llm.cli + import llm.cli # pylint: disable=import-outside-toplevel should_setup = True path = llm.cli.user_dir() / "keys.json" @@ -22,7 +22,8 @@ def setup(): ) else: print("An OpenAI key is already set-up, proceed if you want to change it.") - llm.cli.keys_set() + # Trigger key setting (llm uses Click) + llm.cli.keys_set() # pylint: disable=no-value-for-parameter def clipea_execute_prompt(user_prompt: str): @@ -40,8 +41,8 @@ def clipea_execute_prompt(user_prompt: str): Args: user_prompt (str): user command input """ - from clipea import clipea_llm - from llm import Model, Response + from clipea import clipea_llm # pylint: disable=import-outside-toplevel + from llm import Model, Response # pylint: disable=import-outside-toplevel llm_name: str = '' if user_prompt.startswith('4 '): @@ -50,7 +51,7 @@ def clipea_execute_prompt(user_prompt: str): try: model: Model = clipea_llm.init_llm(llm_name) - except Exception as e: + except Exception as e: # pylint: disable=broad-exception-caught sys.exit(str(e)) user_data: str = cli.get_input() @@ -64,7 +65,7 @@ def clipea_execute_prompt(user_prompt: str): def alias(): """Gives zsh's alias (automatic command buffering) commands to the user""" shell: str = ENV["shell"] - if shell == "zsh" or shell == "-zsh": + if shell in ('zsh', '-zsh'): command: str = f"alias '??'='source {CLIPEA_DIR}/clipea.zsh'" user_prompt: str = f"Append this line to my {shell} startup file, \ watching out for quotes and escaping, then explain how to manually source it: {command}" diff --git a/tests/test_utils.py b/tests/test_utils.py index 3ae3622..a89906a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,6 +1,9 @@ +"""Utils tests +""" from clipea import utils def test_anystr_force_str(): + """Basic test for anystr_force_str()""" assert utils.anystr_force_str(b'hello') == 'hello' assert utils.anystr_force_str('hello') == 'hello' From a5596a634436e5d9441e0c8b43dae7ee32658d50 Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:14:45 +0000 Subject: [PATCH 05/10] Create pylint.yml --- .github/workflows/pylint.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/pylint.yml diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml new file mode 100644 index 0000000..383e65c --- /dev/null +++ b/.github/workflows/pylint.yml @@ -0,0 +1,23 @@ +name: Pylint + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.8", "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 + - name: Analysing the code with pylint + run: | + pylint $(git ls-files '*.py') From ba72a24c110a3c22ada12b51fa1d69a4d00262be Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:20:39 +0000 Subject: [PATCH 06/10] Pylint --- .github/workflows/pylint.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pylint.yml b/.github/workflows/pylint.yml index 383e65c..2c2c20b 100644 --- a/.github/workflows/pylint.yml +++ b/.github/workflows/pylint.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.8", "3.9", "3.10"] + python-version: ["3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} @@ -18,6 +18,7 @@ jobs: run: | python -m pip install --upgrade pip pip install pylint + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Analysing the code with pylint run: | pylint $(git ls-files '*.py') From dec30ee9a8d2c9b600196f735f425f408ec6e6e6 Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:22:40 +0000 Subject: [PATCH 07/10] Fix the pylint errors added when trying to ignore other pylint errors --- clipea/commands.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/clipea/commands.py b/clipea/commands.py index 728bdd8..fb349bb 100644 --- a/clipea/commands.py +++ b/clipea/commands.py @@ -8,7 +8,7 @@ def setup(): """Checks if `llm` has an openai key and prompt to change it or create one""" - import llm.cli # pylint: disable=import-outside-toplevel + import llm.cli # pylint: disable=import-outside-toplevel should_setup = True path = llm.cli.user_dir() / "keys.json" @@ -23,7 +23,7 @@ def setup(): else: print("An OpenAI key is already set-up, proceed if you want to change it.") # Trigger key setting (llm uses Click) - llm.cli.keys_set() # pylint: disable=no-value-for-parameter + llm.cli.keys_set() # pylint: disable=no-value-for-parameter def clipea_execute_prompt(user_prompt: str): @@ -41,8 +41,8 @@ def clipea_execute_prompt(user_prompt: str): Args: user_prompt (str): user command input """ - from clipea import clipea_llm # pylint: disable=import-outside-toplevel - from llm import Model, Response # pylint: disable=import-outside-toplevel + from clipea import clipea_llm # pylint: disable=import-outside-toplevel + from llm import Model, Response # pylint: disable=import-outside-toplevel llm_name: str = '' if user_prompt.startswith('4 '): @@ -51,7 +51,7 @@ def clipea_execute_prompt(user_prompt: str): try: model: Model = clipea_llm.init_llm(llm_name) - except Exception as e: # pylint: disable=broad-exception-caught + except Exception as e: # pylint: disable=broad-exception-caught sys.exit(str(e)) user_data: str = cli.get_input() From cdab006ed8a6e67a95ea6090e6832a3e56239fbe Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:24:39 +0000 Subject: [PATCH 08/10] Create python-app.yml --- .github/workflows/python-app.yml | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .github/workflows/python-app.yml diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml new file mode 100644 index 0000000..d1aa47c --- /dev/null +++ b/.github/workflows/python-app.yml @@ -0,0 +1,35 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python application + +on: [push] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python 3.10 + uses: actions/setup-python@v3 + with: + python-version: "3.10" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install flake8 pytest + if [ -f requirements.txt ]; then pip install -r requirements.txt; fi + - name: Lint with flake8 + run: | + # stop the build if there are Python syntax errors or undefined names + flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics + # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide + flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics + - name: Test with pytest + run: | + pytest From 89b28ff1e873248de2b06136f28986bb78a31e6a Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:29:16 +0000 Subject: [PATCH 09/10] Remove escaped space. I'm not sure what this was for but it doesn't seem to be needed --- clipea/clipea_llm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clipea/clipea_llm.py b/clipea/clipea_llm.py index 2609f0b..b096f71 100644 --- a/clipea/clipea_llm.py +++ b/clipea/clipea_llm.py @@ -87,5 +87,5 @@ def process_command(): if output_file: utils.write_to_file( output_file, - ';\ '.join(buffer.rstrip(os.linesep).split(os.linesep)) + os.linesep, + ';'.join(buffer.rstrip(os.linesep).split(os.linesep)) + os.linesep, ) From f3f823abb94524f36ea414b2b059caf125f86b41 Mon Sep 17 00:00:00 2001 From: Dave Hulbert Date: Sun, 3 Dec 2023 20:35:45 +0000 Subject: [PATCH 10/10] Rename workflow action --- .github/workflows/python-app.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index d1aa47c..fca6f97 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -1,7 +1,7 @@ # This workflow will install Python dependencies, run tests and lint with a single version of Python # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python -name: Python application +name: Flake8 and pytest on: [push]