Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some tooling experiments #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Minimal makefile for Sphinx documentation
#

# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build

# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

.PHONY: help Makefile

# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
31 changes: 31 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'jupyter-schemas'
copyright = '2024, .'
author = 'Jupyter Development Team'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = [
'sphinx.ext.autodoc',
'sphinx_design',
'sphinx-jsonschema'
]

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'pydata_sphinx_theme'
html_static_path = ['_static']
129 changes: 129 additions & 0 deletions docs/example.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Example schema
==============

.. tab-set::

.. tab-item:: Rendered using sphinx-jsonschema

.. jsonschema:: ../schema/server/events/kernel-actions/v1/kernel-actions.schema.json

.. tab-item:: JSON

.. literalinclude:: ../schema/server/events/kernel-actions/v1/kernel-actions.schema.json
:language: JSON

.. tab-item:: TOML

.. code-block:: toml

"$schema" = "https://json-schema.org/draft/2020-12/schema"
"$id" = "https://schema.jupyter.org/server/events/kernel-actions/v1/kernel-actions.schema.json"
version = 1
title = "Kernel Manager activities"
personal-data = true
description = "Record events of a kernel manager.\n"
type = "object"
required = [ "action", "msg",]

[properties.action]
enum = [ "start", "interrupt", "shutdown", "restart",]
description = "Action performed by the Kernel Manager.\n\nThis is a required field.\n\nPossible values:\n\n1. start\n A kernel has been started with the given kernel id.\n\n2. interrupt\n A kernel has been interrupted for the given kernel id.\n\n3. shutdown\n A kernel has been shut down for the given kernel id.\n\n4. restart\n A kernel has been restarted for the given kernel id.\n"

[properties.kernel_id]
type = "string"
description = "Kernel id.\n\nThis is a required field for all actions and statuses except action start with status error.\n"

[properties.kernel_name]
type = "string"
description = "Name of the kernel.\n"

[properties.status]
enum = [ "error", "success",]
description = "Status received from a rest api operation to kernel server.\n\nThis is a required field.\n\nPossible values:\n\n1. error\n Error response from a rest api operation to kernel server.\n\n2. success\n Success response from a rest api operation to kernel server.\n"

[properties.status_code]
type = "number"
description = "Http response codes from a rest api operation to kernel server.\nExamples: 200, 400, 502, 503, 599 etc\n"

[properties.msg]
type = "string"
description = "Description of the event specified in action.\n"

[if.not.properties.status]
const = "error"

[if.not.properties.action]
const = "start"

[then]
required = [ "kernel_id",]

.. tab-item:: YAML

.. code-block:: yaml

$schema: https://json-schema.org/draft/2020-12/schema
$id: https://schema.jupyter.org/server/events/kernel-actions/v1/kernel-actions.schema.json
version: 1
title: Kernel Manager activities
personal-data: true
description: 'Record events of a kernel manager.

'
type: object
required:
- action
- msg
properties:
action:
enum:
- start
- interrupt
- shutdown
- restart
description: "Action performed by the Kernel Manager.\n\nThis is a required field.\n\nPossible values:\n\
\n1. start\n A kernel has been started with the given kernel id.\n\n2. interrupt\n A kernel\
\ has been interrupted for the given kernel id.\n\n3. shutdown\n A kernel has been shut down for\
\ the given kernel id.\n\n4. restart\n A kernel has been restarted for the given kernel id.\n"
kernel_id:
type: string
description: 'Kernel id.


This is a required field for all actions and statuses except action start with status error.

'
kernel_name:
type: string
description: 'Name of the kernel.

'
status:
enum:
- error
- success
description: "Status received from a rest api operation to kernel server.\n\nThis is a required field.\n\
\nPossible values:\n\n1. error\n Error response from a rest api operation to kernel server.\n\n\
2. success\n Success response from a rest api operation to kernel server.\n"
status_code:
type: number
description: 'Http response codes from a rest api operation to kernel server.

Examples: 200, 400, 502, 503, 599 etc

'
msg:
type: string
description: 'Description of the event specified in action.

'
if:
not:
properties:
status:
const: error
action:
const: start
then:
required:
- kernel_id
21 changes: 21 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.. jupyter-schemas documentation master file, created by
sphinx-quickstart on Fri Feb 23 14:44:15 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.

Welcome to jupyter-schemas's documentation!
===========================================

.. toctree::
:maxdepth: 2
:caption: Contents:

example


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
35 changes: 35 additions & 0 deletions docs/make.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
@ECHO OFF

pushd %~dp0

REM Command file for Sphinx documentation

if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build

%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)

if "%1" == "" goto help

%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end

:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%

:end
popd
60 changes: 60 additions & 0 deletions jupyter_schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import typing
import json
import jsonschema
import pathlib
import urllib.parse
from jupyter_core.paths import jupyter_path
Expand All @@ -17,6 +18,7 @@ class JupyterSchemaSourceNotFound(Exception):


ROOT_SCHEMA_PATH: pathlib.Path = pathlib.Path(jupyter_path()[0]) / "schema"
ROOT_SCHEMA_URI: str = "https://schema.jupyter.org"


def schema_path(schema_uri: str) -> pathlib.Path:
Expand Down Expand Up @@ -59,3 +61,61 @@ def list_schemas(project: typing.Optional[str] = None) -> typing.List[str]:
logging.warning(f"Could not find an ID/URI in {fpath}.")
pass
return schema_uris


class Schema:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a very similar class to this in Jupyter Events here.

I wonder if we can consolidate some of this logic.

"""A Jupyter schema"""

def __init__(self, filename: str) -> None:
"""Load a schema from JSON (.json), TOML (.toml) or YAML (.yaml) file.

Parameters
----------
filename : str
Name of file containing schema.

TODO: support loading from URI not just local filename.
"""
self._filename = filename
self._dict: dict[str, typing.Any] = {}

suffix = pathlib.Path(filename).suffix
if suffix == ".json":
self._load_json()
else:
raise ValueError(f"Unrecognised file extension on {filename}")

def _load_json(self):
with open(self._filename, "r") as f:
self._dict = json.load(f)

def validate(self) -> None:
"""Validate this schema, raising an exception if invalid."""

# TODO: Better consider type and text of exceptions.

# Initially all schemas are written against the 2020-12 Draft.
if not (meta_schema := self._dict.get("$schema")):
raise RuntimeError(f"Schema {self._filename} does not contain '$schema'")
elif meta_schema != "https://json-schema.org/draft/2020-12/schema":
raise RuntimeError(
f"Schema {self._filename} contains incorrect '$schema': {meta_schema}")

jsonschema.Draft202012Validator.check_schema(self._dict)

# Local schema filename must be consistent with the $id (published URI) property.
if not (id := self._dict.get("$id")):
raise RuntimeError(f"Schema {self._filename} must contain an '$id'")

relative_path = pathlib.Path(self._filename).relative_to(ROOT_SCHEMA_PATH)
expected_id = urllib.parse.urljoin(ROOT_SCHEMA_URI, str(relative_path))
if id != expected_id:
raise RuntimeError(f"Inconsistent schema path {self._filename} and URI {id}")

# Schema path relative to root must contain version number exactly once.
if not (version := self._dict.get("version")):
raise RuntimeError(f"Schema {self._filename} must contain a 'version'")

if (count := relative_path.parts.count(f"v{version}")) != 1:
raise RuntimeError(
f"Schema path {relative_path} must contain version number once not {count} times")
27 changes: 24 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,32 @@ classifiers = [
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = ["jupyter_core"]
dependencies = [
"jsonschema",
"jupyter_core",
]

[project.urls]
Documentation = "https://github.com/unknown/jupyter-schemas#readme"
Issues = "https://github.com/unknown/jupyter-schemas/issues"
Source = "https://github.com/unknown/jupyter-schemas"

[project.optional-dependencies]
test = [
"pytest",
]
docs = [
"pydata_sphinx_theme",
"sphinx",
"sphinx_design",
"sphinx-jsonschema",
]

[tool.hatch.version]
path = "jupyter_schemas.py"

[tool.hatch.build.targets.wheel.shared-data]
"jupyter_server" = "share/jupyter/schema/jupyter_server"
"schema" = "share/jupyter/schema"

[tool.hatch.envs.default]
dependencies = ["coverage[toml]>=6.5", "pytest"]
Expand All @@ -50,8 +64,15 @@ python = ["3.8", "3.9", "3.10", "3.11", "3.12"]
dependencies = ["mypy>=1.0.0"]


[tool.pytest.ini_options]
addopts = [
"-raXs", "--color=yes",
]
testpaths = ["tests/"]


[tool.coverage.run]
source_pkgs = ["jupyter_schemas", "tests"]
source_pkgs = ["schema", "tests"]
branch = true
parallel = true

Expand Down
24 changes: 24 additions & 0 deletions schema/kernel/messages/v1/kernel-info-request.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://schema.jupyter.org/kernel/messages/v1/kernel-info-request.schema.json",
"version": 1,
"title": "Kernel info request message",
"type": "object",
"properties": {
"header": {
"$ref": "/kernel/messages/v1/message-header.schema.json",
"description": "Message header",
"properties": {
"msg_type": {
"const": "kernel_info_request"
}
}
},
"content": {
"type": "object"
}
},
"required": [
"header", "content"
]
}
Loading