Skip to content

Commit

Permalink
feat: ruff linter, fix ipynb2jupytext
Browse files Browse the repository at this point in the history
  • Loading branch information
kiyoon committed Sep 19, 2023
1 parent 5b794f8 commit 31d52d2
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 72 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Linting

on: [push, pull_request]

jobs:
ruff:
name: ruff
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Lint with ruff
uses: chartboost/ruff-action@v1
with:
src: "."
version: 0.0.290
30 changes: 19 additions & 11 deletions src/jupynium/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ def __init__(
"""
self.buf = buf
if self.buf == [""]:
self.num_rows_per_cell: list[int] = [
1
] # each cell's row length. 0-th cell is not a cell, but it's the header. You can put anything above and it won't be synced to Jupyter Notebook.
# each cell's row length. 0-th cell is not a cell, but it's the header.
# You can put anything above and it won't be synced to Jupyter Notebook.
self.num_rows_per_cell: list[int] = [1]

self.cell_types = ["header"] # 0-th cell is not a cell.
else:
self.full_analyse_buf(header_cell_type)
Expand All @@ -66,13 +67,16 @@ def full_analyse_buf(self, header_cell_type="header"):
- cell creation/deletion
- cell type change
During the partial update, the header cell will be continuation from the existing cell.
During the partial update, the header cell will be continuation from
the existing cell.
We don't know if it will be header/cell/markdown.
So we need to pass the header_cell_type.
(This is deprecated, in favour of self.get_cells_text() dealing with content processing.)
(This is deprecated, in favour of self.get_cells_text() dealing with
content processing.)
Args:
header_cell_type (str, optional): Used to be used only when partial update. Now deprecated.
header_cell_type (str, optional): Used to be used only when partial update.
Now deprecated.
"""
num_rows_this_cell = 0
num_rows_per_cell = []
Expand Down Expand Up @@ -207,7 +211,8 @@ def _on_lines_update_buf(self, lines, start_row, old_end_row, new_end_row):
cell_idx, _, row_within_cell = self.get_cell_index_from_row(start_row)

if row_within_cell == 0 and cell_idx > 0:
# If the row is the first row of a cell, and it's not the first cell, then it's a cell separator.
# If the row is the first row of a cell, and it's not the first cell,
# then it's a cell separator.
row_within_cell = self.num_rows_per_cell[cell_idx - 1]
cell_idx -= 1
except IndexError:
Expand Down Expand Up @@ -295,8 +300,10 @@ def _on_lines_update_buf(self, lines, start_row, old_end_row, new_end_row):
modified_cell_idx_end = modified_cell_idx_start + new_lines_buf.num_cells - 1

# Now actually replace the lines
# Optimisation: if the number of lines is not changed, which is most of the cases,
# then we can just replace the the strings in the list instead of modifying list itself.
# Optimisation: if the number of lines is not changed,
# which is most of the cases,
# then we can just replace the the strings in the list
# instead of modifying list itself.
if old_end_row == new_end_row:
for i, line in enumerate(lines):
self.buf[start_row + i] = line
Expand All @@ -323,7 +330,8 @@ def _apply_cell_operations(self, driver, notebook_cell_operations):
elif operation == "cell_type":
for i, cell_type in enumerate(cell_types):
logger.info(
f"Cell {nb_cell_idx + i} type change to {cell_type} from Notebook"
f"Cell {nb_cell_idx + i} type change to {cell_type} "
"from Notebook"
)
# "markdown" or "markdown (jupytext)"
if cell_type == "markdown":
Expand Down Expand Up @@ -360,7 +368,7 @@ def get_cell_index_from_row(
int: cell index
int: cell start row
int: row index within the cell
"""
""" # noqa: E501
if num_rows_per_cell is None:
num_rows_per_cell = self.num_rows_per_cell

Expand Down
6 changes: 4 additions & 2 deletions src/jupynium/cmds/ipynb2jupy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

def get_parser():
parser = argparse.ArgumentParser(
description="Convert ipynb to a jupynium file (.ju.py). Deprecated: use ipynb2jupytext instead.",
description="Convert ipynb to a jupynium file (.ju.py)."
"Deprecated: use ipynb2jupytext instead.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("ipynb_path", help="Path to ipynb file")
parser.add_argument(
"output_jupy_path",
nargs="?",
help="Path to output jupynium file. If not specified, use file name of ipynb file or print to stdout (--stdout)",
help="Path to output jupynium file. "
"If not specified, use file name of ipynb file or print to stdout (--stdout)",
)
parser.add_argument(
"-y", "--yes", action="store_true", help="Do not ask for confirmation"
Expand Down
3 changes: 2 additions & 1 deletion src/jupynium/cmds/ipynb2jupytext.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def get_parser():
parser.add_argument(
"output_jupy_path",
nargs="?",
help="Path to output jupynium file. If not specified, use file name of ipynb file or print to stdout (--stdout)",
help="Path to output jupynium file. "
"If not specified, use file name of ipynb file or print to stdout (--stdout)",
)
parser.add_argument(
"-y", "--yes", action="store_true", help="Do not ask for confirmation"
Expand Down
45 changes: 29 additions & 16 deletions src/jupynium/cmds/jupynium.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def get_parser():
)
parser.add_argument(
"--firefox_profiles_ini_path",
help="Path to firefox profiles.ini which will be used to remember the last session (password, etc.)\n"
help="Path to firefox profiles.ini which will be used to remember the last session (password, etc.)\n" # noqa: E501
"Example path:\n"
"~/.mozilla/firefox/profiles.ini\n"
"~/snap/firefox/common/.mozilla/firefox/profiles.ini",
Expand All @@ -172,14 +172,14 @@ def get_parser():
nargs="+",
default=["jupyter"],
help="Command to start Jupyter Notebook (but without notebook).\n"
"To use conda env, use `--jupyter_command conda run ' --no-capture-output' ' -n' base jupyter`. Notice the space before the dash.\n"
"To use conda env, use `--jupyter_command conda run ' --no-capture-output' ' -n' base jupyter`. Notice the space before the dash.\n" # noqa: E501
"It is used only when the --notebook_URL is localhost, and is not running.",
)
parser.add_argument(
"--notebook_dir",
type=str,
help="When jupyter notebook has started using --jupyter_command, the root dir will be this.\n"
"If None, open at a git dir of nvim's buffer path and still navigate to the buffer dir.\n"
help="When jupyter notebook has started using --jupyter_command, the root dir will be this.\n" # noqa: E501
"If None, open at a git dir of nvim's buffer path and still navigate to the buffer dir.\n" # noqa: E501
"(e.g. localhost:8888/nbclassic/tree/path/to/buffer)",
)
parser.add_argument(
Expand Down Expand Up @@ -214,7 +214,8 @@ def start_if_running_else_clear(args, q: persistqueue.UniqueQ):
else:
if args.attach_only:
logger.error(
"Jupynium is not running. Remove --attach_only option to start a new instance."
"Jupynium is not running. "
"Remove --attach_only option to start a new instance."
)
return 1

Expand Down Expand Up @@ -292,7 +293,9 @@ def generate_notebook_token():

def exception_no_notebook(notebook_URL, nvim):
logger.exception(
f"Exception occurred. Are you sure you're running Jupyter Notebook at {notebook_URL}? Use --jupyter_command to specify the command to start Jupyter Notebook."
"Exception occurred. "
f"Are you sure you're running Jupyter Notebook at {notebook_URL}? "
"Use --jupyter_command to specify the command to start Jupyter Notebook."
)
if nvim is not None:
nvim.lua.Jupynium_notify.error(
Expand Down Expand Up @@ -321,14 +324,16 @@ def kill_child_processes(parent_pid, sig=signal.SIGTERM):
def kill_notebook_proc(notebook_proc):
"""
Kill the notebook process.
Used if we opened a Jupyter Notebook server using the --jupyter_command and when no server is running.
Used if we opened a Jupyter Notebook server using the --jupyter_command
and when no server is running.
"""
if notebook_proc is not None:
if os.name == "nt":
# Windows
os.kill(notebook_proc.pid, signal.CTRL_C_EVENT)
else:
# Need to kill children if the notebook server is started using `conda run` like
# Need to kill children if the notebook server is started using
# `conda run` like
# conda run --no-capture-output -n base jupyter notebook
kill_child_processes(notebook_proc.pid, signal.SIGKILL)

Expand Down Expand Up @@ -373,7 +378,7 @@ def fallback_open_notebook_server(
"--port",
str(notebook_port),
"--no-browser",
f"--NotebookApp.token",
"--NotebookApp.token",
notebook_token,
]

Expand Down Expand Up @@ -414,7 +419,8 @@ def fallback_open_notebook_server(

time.sleep(0.3)
else:
# Process still running but timeout for connecting to notebook. Maybe wrong command?
# Process still running but timeout for connecting to notebook.
# Maybe wrong command?
kill_notebook_proc(notebook_proc)
exception_no_notebook(f"localhost:{notebook_port}{notebook_url_path}", nvim)
return notebook_proc
Expand All @@ -423,8 +429,10 @@ def fallback_open_notebook_server(
# flake8: noqa: C901
def main():
# Initialise with NOTSET level and null device, and add stream handler separately.
# This way, the root logging level is NOTSET (log all), and we can customise each handler's behaviour.
# If we set the level during the initialisation, it will affect to ALL streams, so the file stream cannot be more verbose (lower level) than the console stream.
# This way, the root logging level is NOTSET (log all),
# and we can customise each handler's behaviour.
# If we set the level during the initialisation, it will affect to ALL streams,
# so the file stream cannot be more verbose (lower level) than the console stream.
coloredlogs.install(fmt="", level=logging.NOTSET, stream=open(os.devnull, "w"))

console_handler = logging.StreamHandler()
Expand Down Expand Up @@ -514,8 +522,10 @@ def main():
# Wait for the notebook to load
driver_wait = WebDriverWait(driver, 10)
# Acceptable number of windows is either:
# - Initial number of windows, for regular case where jupynium handles initally focused tab
# - Initial number of windows + 1, if an extension automatically opens a new tab
# - Initial number of windows, for regular case where jupynium handles
# initally focused tab
# - Initial number of windows + 1, if an extension automatically opens
# a new tab
# Ref: https://github.com/kiyoon/jupynium.nvim/issues/59
accept_num_windows = [init_num_windows, init_num_windows + 1]
driver_wait.until(number_of_windows_be_list(accept_num_windows))
Expand All @@ -532,7 +542,9 @@ def main():
}
else:
logger.info(
"No nvim attached. Waiting for nvim to attach. Run jupynium --nvim_listen_addr /tmp/example (use `:echo v:servername` of nvim)"
"No nvim attached. Waiting for nvim to attach. "
"Run jupynium --nvim_listen_addr /tmp/example "
"(use `:echo v:servername` of nvim)"
)

while not sele.is_browser_disconnected(driver):
Expand All @@ -546,7 +558,8 @@ def main():
del_list.append((nvim_listen_addr, None))
except Exception:
logger.exception(
"Uncaught exception occurred while processing events. Detaching nvim."
"Uncaught exception occurred while processing events. "
"Detaching nvim."
)
del_list.append((nvim_listen_addr, None))

Expand Down
Loading

0 comments on commit 31d52d2

Please sign in to comment.