Skip to content

Commit

Permalink
Meta: remove Python 2 workaround for JSON validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Dec 20, 2024
1 parent f72843b commit f69e56c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 62 deletions.
75 changes: 15 additions & 60 deletions meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import genquery
import irods_types
import jsonschema
from deepdiff import DeepDiff

import meta_form
Expand Down Expand Up @@ -82,73 +83,27 @@ def get_json_metadata_errors(ctx: rule.Context,
:returns: List of errors in JSON object
"""
def transform_error(e):
"""Turn a ValidationError into a data structure for the frontend."""
return {'message': e.message,
'path': list(e.path),
'schema_path': list(e.schema_path),
'validator': e.validator}

if schema is None:
schema = schema_.get_active_schema(ctx, metadata_path)

if metadata is None:
metadata = jsonutil.read(ctx, metadata_path)

# Perform validation and filter errors.
# Validation is handed to a Python 3 interpreter to validate with the Draft201909 validator.
# This can be removed when we can use Python 3 in the ruleset (iRODS 4.3.x).
# validator = jsonschema.Draft7Validator(schema)
#
# errors = validator.iter_errors(metadata)
#
# if ignore_required:
# errors = filter(lambda e: e.validator not in ['required', 'dependencies'], errors)
#
# def transform_error(e):
# """Turn a ValidationError into a data structure for the frontend."""
# return {'message': e.message,
# 'path': list(e.path),
# 'schema_path': list(e.schema_path),
# 'validator': e.validator}
#
# return map(transform_error, errors)

# Can't serialize OrderedDict, so transform to dicts.
schema = json.loads(json.dumps(schema))
metadata = json.loads(json.dumps(metadata))

# Create gateway to Python 3.8.
import execnet
gw = execnet.makegateway("popen//python=" + config.python3_interpreter)

channel = gw.remote_exec("""
import jsonschema
errors = []
while 1:
schema, metadata, ignore_required = channel.receive()
if schema is None:
break
# Perform validation and filter errors.
validator = jsonschema.Draft201909Validator(schema)
errors = validator.iter_errors(metadata)
if ignore_required:
errors = filter(lambda e: e.validator not in ['required', 'dependencies'], errors)
def transform_error(e):
return {'message': e.message,
'path': list(e.path),
'schema_path': list(e.schema_path),
'validator': e.validator}
errors = list(map(transform_error, errors))
channel.send(errors)
""")

channel.send((schema, metadata, ignore_required))
channel.send((None, None, None))
errors = channel.receive()

# Log metadata errors.
for error in errors:
log.write(ctx, error)

return errors
validator = jsonschema.Draft201909Validator(schema)
errors = validator.iter_errors(metadata)

if ignore_required:
errors = filter(lambda e: e.validator not in ['required', 'dependencies'], errors)

return list(map(transform_error, errors))


def is_json_metadata_valid(ctx: rule.Context,
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ configparser==4.0.2
zipp==1.2.0
contextlib2==0.6.0.post1
importlib-metadata==2.1.1
jsonschema==4.19.1
jsonschema==4.23.0
pathvalidate==0.29.1
requests_cache==0.5.2
irods-avu-json==2.2.0
python2-secrets==1.0.5
python-dateutil==2.7.0
python-magic==0.4.27
certifi==2021.10.8
execnet==1.9.0
deepdiff==8.0.1
persist-queue==0.8.1
redis==3.5.3
Expand Down

0 comments on commit f69e56c

Please sign in to comment.