diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index f0bc01f..73bd346 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -7,9 +7,63 @@ jobs: name: ruff runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Lint with ruff - uses: chartboost/ruff-action@v1 + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 with: - src: "." - version: 0.0.290 + python-version-file: pyproject.toml + - name: Install ruff and requirements + run: | + pip3 install -r <(cat requirements_dev.txt | grep '^ruff==') + - name: Run ruff + run: | + set +e # Do not exit shell on ruff failure + + nonzero_exit=0 + files=$(find . -type f -name "*.py" | sort) + while read file; do + out=$(ruff check --force-exclude "$file" 2> ruff_stderr.txt) + exit_code=$? + err=$(> $GITHUB_STEP_SUMMARY + echo "${out}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + fi + if [[ -n "$err" ]]; then + echo "${err}" + echo "\`\`\`python" >> $GITHUB_STEP_SUMMARY + echo "${err}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + fi + + out=$(ruff check --diff --force-exclude "$file" 2> ruff_stderr.txt) + err=$(> $GITHUB_STEP_SUMMARY + echo "${out}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + fi + if [[ -n "$err" ]]; then + echo "${err}" + echo "\`\`\`python" >> $GITHUB_STEP_SUMMARY + echo "${err}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + fi + done <<< "$files" + + # Exit with the first non-zero exit-code returned by ruff + # or just zero if all passed + exit ${nonzero_exit} diff --git a/.github/workflows/styles.yml b/.github/workflows/styles.yml index a69b08f..87b1812 100644 --- a/.github/workflows/styles.yml +++ b/.github/workflows/styles.yml @@ -15,14 +15,66 @@ jobs: version: latest args: --check . - black: - name: Black + ruff-format: + name: ruff-format runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Check black - uses: psf/black@stable + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 with: - options: '--check --verbose' - src: '.' - version: '~= 22.12.0' + python-version-file: pyproject.toml + - name: Install ruff + run: | + pip3 install -r <(cat requirements_dev.txt | grep '^ruff==') + - name: Run ruff format + run: | + set +e # Do not exit shell on black failure + out=$(ruff format --check --diff . 2> app_stderr.txt) + exit_code=$? + err=$(> $GITHUB_STEP_SUMMARY + echo "${out}" >> $GITHUB_STEP_SUMMARY + echo "${err}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + + # Exit with the exit-code returned by ruff + exit ${exit_code} + + ruff-isort: + name: ruff-isort + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version-file: pyproject.toml + - name: Install ruff + run: | + pip3 install -r <(cat requirements_dev.txt | grep '^ruff==') + - name: Run ruff isort + run: | + set +e # Do not exit shell on app failure + out=$(ruff --select I --diff . 2> app_stderr.txt) + exit_code=$? + err=$(> $GITHUB_STEP_SUMMARY + echo "${out}" >> $GITHUB_STEP_SUMMARY + echo "${err}" >> $GITHUB_STEP_SUMMARY + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY + + # Exit with the exit-code returned by ruff + exit ${exit_code} diff --git a/README.md b/README.md index ab78402..1a274fe 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # Jupynium: Control Jupyter Notebook on Neovim with ZERO Compromise -[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) @@ -570,4 +569,4 @@ You probably would have accidentally modified directly from the notebook. - I spent my whole Christmas and New Year holidays (and more) just making this plugin. - This is the star history chart with relevant plugins. Thank you for helping it grow! -[![Star History Chart](https://api.star-history.com/svg?repos=kiyoon/jupynium.nvim,untitled-ai/jupyter_ascending,untitled-ai/jupyter_ascending.vim,dccsillag/magma-nvim,luk400/vim-jukit&type=Date)](https://star-history.com/#kiyoon/jupynium.nvim&untitled-ai/jupyter_ascending&untitled-ai/jupyter_ascending.vim&dccsillag/magma-nvim&luk400/vim-jukit&Date) +[![Star History Chart](https://api.star-history.com/svg?repos=kiyoon/jupynium.nvim,imbue-ai/jupyter_ascending,imbue-ai/jupyter_ascending.vim,dccsillag/magma-nvim,luk400/vim-jukit&type=Date)](https://star-history.com/#kiyoon/jupynium.nvim&imbue-ai/jupyter_ascending&imbue-ai/jupyter_ascending.vim&dccsillag/magma-nvim&luk400/vim-jukit&Date) diff --git a/pyproject.toml b/pyproject.toml index 97c6af5..e24dcc9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,13 +41,12 @@ extra = [ "notebook >= 6.4.5", ] dev = [ - "black >= 22.1.0", + "ruff >= 0.4.2", "pre-commit >= 2.21.0", ] test = [ "pytest >= 6.0", "pytest-cov >= 2.0", - "flake8 >= 3.9", "importlib-metadata < 5.0.0; python_version < '3.8'", # flake8 dependency "tox >= 3.24", ] @@ -74,3 +73,13 @@ testpaths = [ [tool.ruff] target-version = "py37" +src = ["src"] # for ruff isort +extend-exclude = [ + "src/jupynium/_version.py", # CHANGE +] + +[tool.ruff.lint.isort] +## Uncomment this if you want to use Python < 3.10 +required-imports = [ + "from __future__ import annotations", +] diff --git a/requirements_dev.txt b/requirements_dev.txt index d6f202c..93af41a 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,5 +1,4 @@ -black==22.12.0 -ruff==0.0.290 +ruff==0.4.2 tox==3.24.3 pytest==6.2.5 pytest-cov==2.12.1 diff --git a/setup.py b/setup.py index 62e217b..ed169ca 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from setuptools import setup if __name__ == "__main__": diff --git a/src/jupynium/__init__.py b/src/jupynium/__init__.py index dadee0e..07c7386 100644 --- a/src/jupynium/__init__.py +++ b/src/jupynium/__init__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + try: from ._version import __version__, __version_tuple__ except ImportError: diff --git a/src/jupynium/__main__.py b/src/jupynium/__main__.py index 7de7293..d77fd8d 100644 --- a/src/jupynium/__main__.py +++ b/src/jupynium/__main__.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from .cmds.jupynium import main if __name__ == "__main__": diff --git a/src/jupynium/buffer.py b/src/jupynium/buffer.py index 0293362..ababd3b 100644 --- a/src/jupynium/buffer.py +++ b/src/jupynium/buffer.py @@ -150,8 +150,7 @@ def get_cells_text( self._process_cell_text( self.cell_types[start_cell_idx], self.buf[ - start_row - + start_row_offset : start_row + start_row + start_row_offset : start_row + self.num_rows_per_cell[start_cell_idx] ], ) @@ -292,9 +291,9 @@ def _on_lines_update_buf(self, lines, start_row, old_end_row, new_end_row): row_within_cell + new_lines_buf.num_rows_per_cell[0] ) new_lines_buf.num_rows_per_cell[-1] += num_tail_rows - self.num_rows_per_cell[ - cell_idx + 1 : cell_idx + 1 - ] = new_lines_buf.num_rows_per_cell[1:] + self.num_rows_per_cell[cell_idx + 1 : cell_idx + 1] = ( + new_lines_buf.num_rows_per_cell[1:] + ) self.cell_types[cell_idx + 1 : cell_idx + 1] = new_lines_buf.cell_types[1:] modified_cell_idx_end = modified_cell_idx_start + new_lines_buf.num_cells - 1 diff --git a/src/jupynium/definitions.py b/src/jupynium/definitions.py index 4e29e4e..42c0787 100644 --- a/src/jupynium/definitions.py +++ b/src/jupynium/definitions.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from pathlib import Path import platformdirs diff --git a/src/jupynium/events_control.py b/src/jupynium/events_control.py index ed43948..68962dc 100644 --- a/src/jupynium/events_control.py +++ b/src/jupynium/events_control.py @@ -347,10 +347,11 @@ def choose_default_kernel(driver, page_type: str, buf_filetype, conda_or_venv_pa valid_kernel_names.append(kernel_name) def match_with_path(env_path: str) -> str | None: - """Match kernel executable path with conda/virtual environment bin directory + """ + Match kernel executable path with conda/virtual environment bin directory. Args: - env_path (str): Path of the conda/virtual environment directory + env_path: Path of the conda/virtual environment directory Returns: str: Name of the kernel matching the environment, returns None if no match @@ -666,7 +667,14 @@ def process_notification_event( output_ipynb_path = os.path.splitext(output_ipynb_path)[0] output_ipynb_path += ".ipynb" - download_ipynb(driver, nvim_info, bufnr, output_ipynb_path) + try: + download_ipynb(driver, nvim_info, bufnr, output_ipynb_path) + except OSError as e: + logger.warning( + f"Failed to auto-download ipynb with error: {e}.\n" + "Maybe a remote nvim is used and the path " + f"{output_ipynb_path} is not accessible on the local machine." + ) elif event[1] == "download_ipynb": (buf_filepath, filename) = event_args assert buf_filepath != "" @@ -686,7 +694,18 @@ def process_notification_event( output_ipynb_path = os.path.splitext(output_ipynb_path)[0] output_ipynb_path += ".ipynb" - download_ipynb(driver, nvim_info, bufnr, output_ipynb_path) + try: + download_ipynb(driver, nvim_info, bufnr, output_ipynb_path) + except OSError as e: + nvim_info.nvim.lua.Jupynium_notify.error( + ["Failed to download ipynb file to", output_ipynb_path], + async_=True, + ) + logger.error( + f"Failed to download ipynb with error: {e}.\n" + f"Maybe the path {output_ipynb_path} is not accessible " + "on the local machine." + ) elif event[1] == "toggle_selected_cells_outputs_scroll": driver.switch_to.window(nvim_info.window_handles[bufnr]) diff --git a/src/jupynium/jupyter_notebook_selenium.py b/src/jupynium/jupyter_notebook_selenium.py index 412f7bf..f1c98bd 100644 --- a/src/jupynium/jupyter_notebook_selenium.py +++ b/src/jupynium/jupyter_notebook_selenium.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import logging logger = logging.getLogger(__name__) diff --git a/src/jupynium/process.py b/src/jupynium/process.py index b2a079b..2417731 100644 --- a/src/jupynium/process.py +++ b/src/jupynium/process.py @@ -1,4 +1,6 @@ # https://stackoverflow.com/questions/36799192/check-if-python-script-is-already-running +from __future__ import annotations + from os import getpid from os.path import exists diff --git a/src/jupynium/pynvim_helpers.py b/src/jupynium/pynvim_helpers.py index 57f5672..23cf7e7 100644 --- a/src/jupynium/pynvim_helpers.py +++ b/src/jupynium/pynvim_helpers.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import logging import time diff --git a/src/jupynium/rpc_messages.py b/src/jupynium/rpc_messages.py index 62e6c7e..a5fc90c 100644 --- a/src/jupynium/rpc_messages.py +++ b/src/jupynium/rpc_messages.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import logging from pynvim import Nvim diff --git a/src/jupynium/selenium_helpers.py b/src/jupynium/selenium_helpers.py index e32100b..a3a1ec7 100644 --- a/src/jupynium/selenium_helpers.py +++ b/src/jupynium/selenium_helpers.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import logging from selenium.common.exceptions import TimeoutException diff --git a/tests/conftest.py b/tests/conftest.py index 7a82579..ff41756 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import os import subprocess import tempfile diff --git a/tests/test_buffer.py b/tests/test_buffer.py index a1c47ab..3ac4add 100644 --- a/tests/test_buffer.py +++ b/tests/test_buffer.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import pytest from jupynium.buffer import JupyniumBuffer diff --git a/tests/test_events.py b/tests/test_events.py index d06628e..de2784d 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import time from pynvim import Nvim