Skip to content

Commit

Permalink
fix: download_ipynb exception (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon authored Apr 30, 2024
1 parent 71c3c15 commit a0cfec0
Show file tree
Hide file tree
Showing 19 changed files with 183 additions and 28 deletions.
64 changes: 59 additions & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=$(<ruff_stderr.txt)
if [[ $exit_code -ne 0 ]]; then
nonzero_exit=$exit_code
fi
if [[ -n "$out" ]]; then
# Display the raw output in the step
echo "${out}"
# Display the Markdown output in the job summary
echo "\`\`\`python" >> $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=$(<ruff_stderr.txt)
if [[ -n "$out" ]]; then
# Display the raw output in the step
echo "${out}"
# Display the Markdown output in the job summary
echo "\`\`\`diff" >> $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}
68 changes: 60 additions & 8 deletions .github/workflows/styles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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=$(<app_stderr.txt)
# Display the raw output in the step
echo "${out}"
echo "${err}"
# Display the Markdown output in the job summary
echo "\`\`\`diff" >> $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=$(<app_stderr.txt)
# Display the raw output in the step
echo "${out}"
echo "${err}"
# Display the Markdown output in the job summary
echo "\`\`\`diff" >> $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}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
<a href="https://github.com/kiyoon/jupynium.nvim/actions/workflows/tests.yml">
<img src="https://github.com/kiyoon/jupynium.nvim/workflows/Tests/badge.svg?style=flat" />
</a>
Expand Down Expand Up @@ -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)
13 changes: 11 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand All @@ -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",
]
3 changes: 1 addition & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from setuptools import setup

if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

try:
from ._version import __version__, __version_tuple__
except ImportError:
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from .cmds.jupynium import main

if __name__ == "__main__":
Expand Down
9 changes: 4 additions & 5 deletions src/jupynium/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
],
)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/definitions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from pathlib import Path

import platformdirs
Expand Down
27 changes: 23 additions & 4 deletions src/jupynium/events_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 != ""
Expand All @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/jupyter_notebook_selenium.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging

logger = logging.getLogger(__name__)
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/process.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/pynvim_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import time

Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/rpc_messages.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging

from pynvim import Nvim
Expand Down
2 changes: 2 additions & 0 deletions src/jupynium/selenium_helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging

from selenium.common.exceptions import TimeoutException
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import os
import subprocess
import tempfile
Expand Down
2 changes: 2 additions & 0 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import pytest

from jupynium.buffer import JupyniumBuffer
Expand Down
2 changes: 2 additions & 0 deletions tests/test_events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import time

from pynvim import Nvim
Expand Down

0 comments on commit a0cfec0

Please sign in to comment.