Skip to content

Commit

Permalink
Keep event schemas in YAML
Browse files Browse the repository at this point in the history
Primary advantage over JSON is that we can do multi-line strings
for more detailed documentation. We also expect humans to read &
write these, so YAML is a much better format there. All JSON
is also valid YAML, so that helps.

Depends on jupyter/telemetry#13
  • Loading branch information
yuvipanda authored and kiendang committed Dec 17, 2020
1 parent 913215a commit a921ff0
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
7 changes: 4 additions & 3 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import warnings
import webbrowser
import urllib
from ruamel.yaml import YAML
from glob import glob

from types import ModuleType
Expand Down Expand Up @@ -1626,14 +1627,14 @@ def _init_asyncio_patch():
def init_eventlog(self):
self.eventlog = EventLog(parent=self)

yaml = YAML(typ='safe')
event_schemas_dir = os.path.join(os.path.dirname(__file__), 'event-schemas')
# Recursively register all .json files under event-schemas
for dirname, _, files in os.walk(event_schemas_dir):
for file in files:
if file.endswith('.json'):
if file.endswith('.yaml'):
file_path = os.path.join(dirname, file)
with open(file_path) as f:
self.eventlog.register_schema(json.load(f))
self.eventlog.register_schema_file(file_path)

@catch_config_error
def initialize(self, argv=None, find_extensions=True, new_httpserver=True):
Expand Down
57 changes: 57 additions & 0 deletions notebook/tests/test_eventlog.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import os
import re
import jsonschema
from ruamel.yaml import YAML
from notebook.notebookapp import NotebookApp
from notebook.utils import eventlogging_schema_fqn
from unittest import TestCase

yaml = YAML(typ='safe')

class RegisteredSchemasTestCase(TestCase):
def schema_files(self):
event_schemas_dir = os.path.realpath(
os.path.join(os.path.dirname(__file__), '..', 'event-schemas')
)
schemas = []
for dirname, _, files in os.walk(event_schemas_dir):
for file in files:
if file.endswith('.yaml'):
yield os.path.join(dirname, file)

def test_eventlogging_schema_fqn(self):
self.assertEqual(
eventlogging_schema_fqn('test'),
'eventlogging.jupyter.org/notebook/test'
)
def test_valid_schemas(self):
"""
All schemas must be valid json schemas
"""
for schema_file in self.schema_files():
with open(schema_file) as f:
jsonschema.Draft7Validator.check_schema(yaml.load(f))

def test_schema_conventions(self):
"""
Test schema naming convention for this repo.
1. All schemas should be under event-schamas/{name}/v{version}.yaml
2. Schema id should be eventlogging.jupyter.org/notebook/{name}
3. Schema version should match version in file
"""
for schema_file in self.schema_files():
filename = os.path.basename(schema_file)
match = re.match('v(\d+)\.yaml', filename)
# All schema locations must match the following pattern
# schema-name/v(version).yaml
self.assertIsNotNone(match)

with open(schema_file) as f:
schema = yaml.load(f)

self.assertEqual(schema['$id'], eventlogging_schema_fqn(
os.path.basename(os.path.dirname(schema_file))
))
self.assertEqual(schema['version'], int(match.groups()[0]))

0 comments on commit a921ff0

Please sign in to comment.