Skip to content

Commit

Permalink
Add jsonschema for validating schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
manics committed Jan 28, 2021
1 parent 8f55263 commit d3ba8c9
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ include *.txt
include *.rst
prune dist
prune build
include src/omero_render/*.yaml
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ def read(fname):

setup(
version=version,
packages=['', 'omero.plugins'],
packages=['', 'omero.plugins', 'omero_render'],
package_dir={"": "src"},
include_package_data=True,
name='omero-cli-render',
description="Plugin for use in the OMERO CLI.",
long_description=read('README.rst'),
Expand All @@ -120,6 +121,7 @@ def read(fname):
install_requires=[
'PyYAML',
'omero-py>=5.6.0',
'jsonschema',
'future'
],
python_requires='>=3',
Expand Down
6 changes: 6 additions & 0 deletions src/omero_cli_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

from omero import UnloadedEntityException

from omero_render import validate_renderdef, validate_renderdef_batch

HELP = "Tools for working with rendering settings"

INFO_HELP = """Show details of a rendering setting
Expand Down Expand Up @@ -667,6 +669,8 @@ def _load_rendering_settings(self, source, session=None):
self.ctx.dbg(e)
self.ctx.die(103, "Could not read %s" % source)

validate_renderdef(data)

if 'channels' not in data:
self.ctx.die(104, "ERROR: No channels found in %s" % source)

Expand Down Expand Up @@ -773,6 +777,8 @@ def batchset(self, args):
except Exception as e:
self.ctx.dbg(e)
self.ctx.die(103, "Could not read %s" % args.path)
validate_renderdef_batch(batch_data)

for key, containers in batch_data.items():
if key.startswith('_'):
continue
Expand Down
2 changes: 2 additions & 0 deletions src/omero_render/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .schema import validate_renderdef, validate_renderdef_batch
__all__ = ['validate_renderdef', 'validate_renderdef_batch']
115 changes: 115 additions & 0 deletions src/omero_render/renderdef-batch-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---

$schema: https://json-schema.org/schema#
$id: https://github.com/ome/omero-cli-render#renderdef-batch
title: jsonschema for batch rendering configuration
type: object
additionalProperties: false
# Top level objects beginning with _ will be ignored
patternProperties:
"^_":
type:
object

# Python jsonschema doesn't support $ref so use YAML anchors instead
definitions:
channel: &channel
type: object
additionalProperties: false
properties:
active:
title: Active channel
type: boolean
color:
title: Channel color as HTML RGB triplet
type: string
label:
title: Channel name
type: string
start:
title: Start of rendering window, optional (needs end)
type: number
end:
title: End of rendering window, optional (needs start)
type: number
min:
title: Minimum pixel intensity, optional (needs max)
type: number
max:
title: Maximum pixel intensity, optional (needs min)
type: number

renderdef: &renderdef
type: object
additionalProperties: false
required:
- channels
properties:
channels:
title: Dictionary of channels
type: object
additionalProperties: false
patternProperties:
"^[0-9]+$":
title: Channel index, 1-based
type: object
properties: *channel
# $ref: "#/definitions/channel"
greyscale:
title: Greyscale rendering, optional
type: boolean
z:
title: Default Z plane index, 1-based, optional
type: integer
t:
title: Default T plane index, 1-based, optional
type: integer
version:
title: Version of the renderdef specification
type: integer

dataset: &dataset
type: object
additionalProperties: false
required:
- name
- renderdef
properties:
name:
title: Name of Dataset
type: string
renderdef: *renderdef
# $ref: "#/definitions/renderdef"

project: &project
type: object
additionalProperties: false
required:
- name
properties:
name:
title: Name of Project
type: string
datasets:
title: List of Datasets
type: array
minItems: 1
items: *dataset
# $ref: "#/definitions/dataset"
renderdef: *renderdef
# $ref: "#/definitions/renderdef"

properties:
projects:
title: List of Projects
type: array
minItems: 1
items: *project
# $ref: "#/definitions/project"

datasets:
title: List of Datasets
type: array
minItems: 1
items: *dataset
# $ref: "#/definitions/dataset"
61 changes: 61 additions & 0 deletions src/omero_render/renderdef-schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---

$schema: https://json-schema.org/schema#
$id: https://github.com/ome/omero-cli-render#renderdef
title: jsonschema for render configuration file
type: object
additionalProperties: false
required:
- channels

# Python jsonschema doesn't support $ref so use YAML anchors instead
definitions:
channel: &channel
type: object
additionalProperties: false
properties:
active:
title: Active channel
type: boolean
color:
title: Channel color as HTML RGB triplet
type: string
label:
title: Channel name
type: string
start:
title: Start of rendering window, optional (needs end)
type: number
end:
title: End of rendering window, optional (needs start)
type: number
min:
title: Minimum pixel intensity, optional (needs max)
type: number
max:
title: Maximum pixel intensity, optional (needs min)
type: number

properties:
channels:
title: Dictionary of channels
type: object
additionalProperties: false
patternProperties:
"^[0-9]+$":
title: Channel index, 1-based
type: object
properties:
channel: *channel
greyscale:
title: Greyscale rendering, optional
type: boolean
z:
title: Default Z plane index, 1-based, optional
type: integer
t:
title: Default T plane index, 1-based, optional
type: integer
version:
title: Version of the renderdef specification
type: integer
31 changes: 31 additions & 0 deletions src/omero_render/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import json
from jsonschema import Draft7Validator
from pkgutil import get_data
import yaml


def _validate(schema_name, renderdef):
schemastr = get_data('omero_render', schema_name)
yml = yaml.safe_load(schemastr)
# Hack to expand anchors
# https://stackoverflow.com/a/64993515
schema = json.loads(json.dumps(yml))
v = Draft7Validator(schema)
# print(yaml.dump(schema))
# print(renderdef)
# Hack to ensure YAML integer keys are JSON strings
renderdef_json = json.loads(json.dumps(renderdef))

if not v.is_valid(renderdef_json):
errs = '\n\n** '.join(
['Invalid definition'] +
['\n\n'.join(str(e) for e in v.iter_errors(renderdef_json))])
raise ValueError(errs)


def validate_renderdef(renderdef):
_validate('renderdef-schema.yaml', renderdef)


def validate_renderdef_batch(renderdef):
_validate('renderdef-batch-schema.yaml', renderdef)

0 comments on commit d3ba8c9

Please sign in to comment.