Skip to content

Commit

Permalink
Merge branch 'main' into pre-commit-ci-update-config
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Jan 28, 2024
2 parents 371c460 + 6567bcb commit 652ce3b
Show file tree
Hide file tree
Showing 43 changed files with 357 additions and 170 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Initial pre-commit reformat
92edb7756e411f7e8295013b5da6a4255e89ac75
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
# GitHub Actions
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
groups:
actions:
patterns:
- "*"
# Python
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Tests

on:
workflow_dispatch:
push:
pull_request:
branches:
Expand Down
67 changes: 67 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,77 @@
ci:
autoupdate_schedule: monthly
autoupdate_commit_msg: "chore: update pre-commit hooks"

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: check-ast
- id: check-docstring-first
- id: check-executables-have-shebangs
- id: check-added-large-files
- id: check-case-conflict
- id: check-merge-conflict
- id: check-json
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.27.3
hooks:
- id: check-github-workflows

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies:
[mdformat-gfm, mdformat-frontmatter, mdformat-footnote]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: "v4.0.0-alpha.8"
hooks:
- id: prettier
types_or: [yaml, html, json]

- repo: https://github.com/codespell-project/codespell
rev: "v2.2.6"
hooks:
- id: codespell
args: ["-L", "sur,nd"]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: "v1.10.0"
hooks:
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal

- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.8.0"
hooks:
- id: mypy
files: kernel_gateway
stages: [manual]
additional_dependencies: []

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.9
hooks:
- id: ruff
types_or: [python, jupyter]
args: ["--fix", "--show-fixes"]
exclude: ^kernel_gateway/tests/resources/
- id: ruff-format
types_or: [python, jupyter]
exclude: ^kernel_gateway/tests/resources/

- repo: https://github.com/scientific-python/cookie
rev: "2023.12.21"
hooks:
- id: sp-repo-review
additional_dependencies: ["repo-review[cli]"]
17 changes: 17 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version: 2
build:
os: ubuntu-22.04
tools:
python: "3.9"
sphinx:
configuration: docs/source/conf.py
python:
install:
# install itself with pip install .
- method: pip
path: .
extra_requirements:
- docs
formats:
- epub
- htmlzip
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
- Add support for specifying notebook-http APIs using full Swagger specs
- Add option to serve static web assets from Tornado in notebook-http mode
- Add command line aliases for common options (e.g., `--ip`)
- Fix Tornado 4.4 compatbility: sending an empty body string with a 204 response
- Fix Tornado 4.4 compatibility: sending an empty body string with a 204 response

## 1.0.0 (2016-07-15)

Expand Down
19 changes: 13 additions & 6 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

import os
import logging
import pytest
import os
from binascii import hexlify
from traitlets.config import Config
from kernel_gateway.gatewayapp import KernelGatewayApp

# isort: off
# This must come before any Jupyter imports.
os.environ["JUPYTER_PLATFORM_DIRS"] = "1"
# isort: on

import pytest # noqa: E402
from traitlets.config import Config # noqa: E402

from kernel_gateway.gatewayapp import KernelGatewayApp # noqa: E402

pytest_plugins = ["pytest_jupyter.jupyter_core", "pytest_jupyter.jupyter_server"]

Expand Down Expand Up @@ -92,13 +99,13 @@ def jp_server_cleanup(jp_asyncio_loop):
try:
jp_asyncio_loop.run_until_complete(app.async_shutdown())
except (RuntimeError, SystemExit) as e:
print("ignoring cleanup error", e)
print("ignoring cleanup error", e) # noqa: T201
if hasattr(app, "kernel_manager"):
app.kernel_manager.context.destroy()
KernelGatewayApp.clear_instance()


@pytest.fixture
@pytest.fixture()
def jp_auth_header(jp_serverapp):
"""Configures an authorization header using the token from the serverapp fixture."""
return {"Authorization": f"token {jp_serverapp.identity_provider.token}"}
5 changes: 2 additions & 3 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# This file is execfile()d with the current directory set to its
# containing dir.
Expand Down Expand Up @@ -60,7 +59,7 @@
#
_version_py = "../../kernel_gateway/_version.py"
version_ns = {}
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns)
exec(compile(open(_version_py).read(), _version_py, "exec"), version_ns) # noqa: S102
# The short X.Y version.
version = "%i.%i" % version_ns["version_info"][:2]
# The full version, including alpha/beta/rc tags.
Expand Down Expand Up @@ -337,7 +336,7 @@
# The format is a list of tuples containing the path and title.
# epub_pre_files = []

# HTML files shat should be inserted after the pages created by sphinx.
# HTML files that should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title.
# epub_post_files = []

Expand Down
2 changes: 1 addition & 1 deletion docs/source/summary-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ See `git log` for a more detailed summary of changes.
- Add support for specifying notebook-http APIs using full Swagger specs
- Add option to serve static web assets from Tornado in notebook-http mode
- Add command line aliases for common options (e.g., `--ip`)
- Fix Tornado 4.4 compatbility: sending an empty body string with a 204 response
- Fix Tornado 4.4 compatibility: sending an empty body string with a 204 response

## 1.0

Expand Down
5 changes: 3 additions & 2 deletions etc/api_examples/setting_response_metadata.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
},
"outputs": [],
"source": [
"import hashlib\n",
"import json\n",
"from dicttoxml import dicttoxml\n",
"import hashlib"
"\n",
"from dicttoxml import dicttoxml"
]
},
{
Expand Down
5 changes: 2 additions & 3 deletions kernel_gateway/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
"""Entrypoint for the kernel gateway package."""
from .gatewayapp import launch_instance

from ._version import version_info, __version__
from ._version import __version__, version_info # noqa: F401
from .gatewayapp import launch_instance # noqa: F401
1 change: 0 additions & 1 deletion kernel_gateway/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
"""CLI entrypoint for the kernel gateway package."""
from __future__ import absolute_import

if __name__ == "__main__":
import kernel_gateway.gatewayapp as app
Expand Down
5 changes: 2 additions & 3 deletions kernel_gateway/auth/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
This defines the _authentication_ layer of Jupyter Server,
to be used in combination with Authorizer for _authorization_.
"""
from traitlets import default
from tornado import web

from jupyter_server.auth.identity import IdentityProvider, User
from jupyter_server.base.handlers import JupyterHandler
from tornado import web
from traitlets import default


class GatewayIdentityProvider(IdentityProvider):
Expand Down
7 changes: 3 additions & 4 deletions kernel_gateway/base/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# Distributed under the terms of the Modified BSD License.
"""Tornado handlers for the base of the API."""

from tornado import web
import jupyter_server.base.handlers as server_handlers
from ..mixins import TokenAuthorizationMixin, CORSMixin, JSONErrorsMixin
from tornado import web

from ..mixins import CORSMixin, JSONErrorsMixin, TokenAuthorizationMixin


class APIVersionHandler(
Expand All @@ -14,8 +15,6 @@ class APIVersionHandler(
JSON errors.
"""

pass


class NotFoundHandler(JSONErrorsMixin, web.RequestHandler):
"""Catches all requests and responds with 404 JSON messages.
Expand Down
56 changes: 24 additions & 32 deletions kernel_gateway/gatewayapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,40 @@

import asyncio
import errno
import importlib
import logging
import hashlib
import hmac
import importlib
import logging
import os
import sys
import signal
import select
import socket
import signal
import ssl
import sys
import threading
from base64 import encodebytes

import nbformat
from jupyter_server.services.kernels.kernelmanager import MappingKernelManager

from urllib.parse import urlparse
from traitlets import Unicode, Integer, Bytes, default, observe, Type, Instance, List, CBool

from jupyter_core.application import JupyterApp, base_aliases
import nbformat
from jupyter_client.kernelspec import KernelSpecManager

from tornado import httpserver
from tornado import web, ioloop
from tornado.log import enable_pretty_logging, LogFormatter

from jupyter_core.application import JupyterApp, base_aliases
from jupyter_core.paths import secure_write
from jupyter_server.auth.authorizer import AllowAllAuthorizer, Authorizer
from jupyter_server.serverapp import random_ports
from ._version import __version__
from .services.sessions.sessionmanager import SessionManager
from .services.kernels.manager import SeedingMappingKernelManager
from jupyter_server.services.kernels.connection.base import BaseKernelWebsocketConnection
from jupyter_server.services.kernels.connection.channels import ZMQChannelsWebsocketConnection
from jupyter_server.services.kernels.kernelmanager import MappingKernelManager
from tornado import httpserver, ioloop, web
from tornado.log import LogFormatter, enable_pretty_logging
from traitlets import Bytes, CBool, Instance, Integer, List, Type, Unicode, default, observe

from ._version import __version__
from .auth.identity import GatewayIdentityProvider
from .jupyter_websocket import JupyterWebsocketPersonality

# Only present for generating help documentation
from .notebook_http import NotebookHTTPPersonality
from .jupyter_websocket import JupyterWebsocketPersonality

from jupyter_server.auth.authorizer import AllowAllAuthorizer, Authorizer
from jupyter_server.services.kernels.connection.base import BaseKernelWebsocketConnection
from jupyter_server.services.kernels.connection.channels import ZMQChannelsWebsocketConnection

from .services.kernels.manager import SeedingMappingKernelManager
from .services.sessions.sessionmanager import SessionManager

# Add additional command line aliases
aliases = dict(base_aliases)
Expand Down Expand Up @@ -135,7 +127,7 @@ def base_url_default(self):
return os.getenv(self.base_url_env, self.base_url_default_value)

# Token authorization
auth_token_env = "KG_AUTH_TOKEN"
auth_token_env = "KG_AUTH_TOKEN" # noqa: S105
auth_token = Unicode(
config=True, help="Authorization token required for all requests (KG_AUTH_TOKEN env var)"
)
Expand Down Expand Up @@ -196,7 +188,7 @@ def expose_headers_default(self):
trust_xheaders = CBool(
False,
config=True,
help="Use x-* header values for overriding the remote-ip, useful when application is behing a proxy. (KG_TRUST_XHEADERS env var)",
help="Use x-* header values for overriding the remote-ip, useful when application is behind a proxy. (KG_TRUST_XHEADERS env var)",
)

@default("trust_xheaders")
Expand Down Expand Up @@ -303,7 +295,7 @@ def api_changed(self, event):
self._load_api_module(event["new"])
except ImportError:
# re-raise with more sensible message to help the user
raise ImportError("API module {} not found".format(event["new"]))
raise ImportError("API module {} not found".format(event["new"])) from None

certfile_env = "KG_CERTFILE"
certfile = Unicode(
Expand Down Expand Up @@ -506,7 +498,7 @@ def _load_notebook(self, uri):
# Remote file
import requests

resp = requests.get(uri)
resp = requests.get(uri, timeout=200)
resp.raise_for_status()
notebook = nbformat.reads(resp.text, 4)

Expand Down Expand Up @@ -594,7 +586,7 @@ def init_configurables(self):
raise RuntimeError(msg)

api_module = self._load_api_module(self.api)
func = getattr(api_module, "create_personality")
func = api_module.create_personality
self.personality = func(parent=self, log=self.log)

self.io_loop.call_later(
Expand Down Expand Up @@ -701,7 +693,7 @@ def init_http_server(self):
for port in random_ports(self.port, self.port_retries + 1):
try:
self.http_server.listen(port, self.ip)
except socket.error as e:
except OSError as e:
if e.errno == errno.EADDRINUSE:
self.log.info("The port %i is already in use, trying another port." % port)
continue
Expand Down Expand Up @@ -764,7 +756,7 @@ def _confirm_exit(self):
return
yes = "y"
no = "n"
sys.stdout.write("Shutdown this Jupyter server (%s/[%s])? " % (yes, no))
sys.stdout.write("Shutdown this Jupyter server (%s/[%s])? " % (yes, no)) # noqa: UP031
sys.stdout.flush()
r, w, x = select.select([sys.stdin], [], [], 5)
if r:
Expand Down
Loading

0 comments on commit 652ce3b

Please sign in to comment.