Commit 599ce8b2 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Added metadata validator.

parent 3384e64a
......@@ -11,7 +11,7 @@ documentation = 'README.md'
license = 'MIT License'
keywords = []
dependencies = ['pyyaml', 'requests']
dependencies = ['jsonschema', 'pyyaml', 'requests']
supported = [(2, 7), (3, 3), (3, 4)]
classifiers = [
'Development Status :: 3 - Alpha',
......
......@@ -6,8 +6,11 @@ import json
import socket
import sys
import jsonschema
import yaml
from jsonschema.exceptions import ValidationError
from . import doc_split, usage, version
from .transfer_client import TransferClient
......@@ -315,6 +318,21 @@ def transfer_summary(
return
def check_metadata(metadata_handle, server_name, user_id, ssl_check):
"""
Check a metadata file agains the JSON schema.
:arg stream output_handle: Open writeable handle to a file.
:arg stream metadata_handle: Open readable handle to a metadata file.
:arg str server_name: Name of the transfer server.
:arg str user_id: User ID.
:arg bool ssl_check: Check server SSL certificate.
"""
jsonschema.validate(
json.loads(metadata_handle.read()),
TransferClient(server_name, ssl_check).schema(user_id))
def main():
"""
Main entry point.
......@@ -359,6 +377,11 @@ def main():
'-n', dest='ssl_check', default=True, action='store_false',
help='disable ssl certificate check')
metadata_parser = argparse.ArgumentParser(add_help=False)
metadata_parser.add_argument(
'metadata_handle', metavar='METADATA', type=argparse.FileType('r'),
help='metadata file')
default_parser = argparse.ArgumentParser(
add_help=False,
parents=[output_parser, server_name_parser, user_id_parser,
......@@ -376,11 +399,8 @@ def main():
subparser = subparsers.add_parser(
'transfers',
parents=[default_parser],
parents=[default_parser, metadata_parser],
description=doc_split(transfers))
subparser.add_argument(
'metadata_handle', metavar='METADATA', type=argparse.FileType('r'),
help='metadata file')
subparser.set_defaults(func=transfers)
subparser = subparsers.add_parser(
......@@ -447,6 +467,13 @@ def main():
description=doc_split(transfer_summary))
subparser.set_defaults(func=transfer_summary)
subparser = subparsers.add_parser(
'check_metadata',
parents=[
server_name_parser, user_id_parser, ssl_parser, metadata_parser],
description=doc_split(check_metadata))
subparser.set_defaults(func=check_metadata)
try:
args = parser.parse_args()
except IOError, error:
......@@ -456,7 +483,7 @@ def main():
args.func(
**{k: v for k, v in vars(args).items()
if k not in ('func', 'subcommand')})
except (ValueError, IOError, OSError), error:
except (ValueError, IOError, OSError, ValidationError) as error:
parser.error(error)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment