-
Notifications
You must be signed in to change notification settings - Fork 4
/
local_validate.py
executable file
·103 lines (87 loc) · 2.71 KB
/
local_validate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python
# -*- coding: latin-1 -*-
'''Validate an ENCODE JSON object'''
'''use requests to handle the HTTP connection'''
import requests
'''use json to convert between Python dictionaries and JSON objects'''
import json
'''use jsonschema to validate objects against the JSON schemas'''
import jsonschema
import sys, os.path
EPILOG = __doc__
'''force return from the server in JSON format'''
HEADERS = {'content-type': 'application/json'}
def get_ENCODE(obj_id):
'''GET an ENCODE object as JSON and return as dict'''
url = SERVER+obj_id+'?limit=all'
response = requests.get(url, auth=(AUTHID, AUTHPW), headers=HEADERS)
if not response.status_code == 200:
print >> sys.stderr, response.text
return response.json()
def processkeys(args):
keysf = open(args.keyfile,'r')
keys_json_string = keysf.read()
keysf.close()
keys = json.loads(keys_json_string)
key_dict = keys[args.key]
global AUTHID
global AUTHPW
global SERVER
if not args.authid:
AUTHID = key_dict['key']
else:
AUTHID = args.authid
if not args.authpw:
AUTHPW = key_dict['secret']
else:
AUTHPW = args.authpw
if not args.server:
SERVER = key_dict['server']
else:
SERVER = args.server
if not SERVER.endswith("/"):
SERVER += "/"
def main():
import argparse
parser = argparse.ArgumentParser(
description="Validate ENCODE JSON object against the appropriate ENCODE schema", epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('--infile', '-i',
help="File containing the JSON object as a JSON string.")
parser.add_argument('--server',
help="Full URL of the server.")
parser.add_argument('--key',
default='default',
help="The keypair identifier from the keyfile. Default is --key=default")
parser.add_argument('--keyfile',
default=os.path.expanduser("~/keypairs.json"),
help="The keypair file. Default is --keyfile=%s" %(os.path.expanduser("~/keypairs.json")))
parser.add_argument('--authid',
help="The HTTP auth ID.")
parser.add_argument('--authpw',
help="The HTTP auth PW.")
parser.add_argument('--debug',
default=False,
action='store_true',
help="Print debug messages. Default is False.")
args = parser.parse_args()
processkeys(args)
if args.infile:
infile = open(args.infile,'r')
else:
infile = sys.stdin
json_string = infile.read()
json_object = json.loads(json_string)
try:
datatype = json_object.pop('@type')[0]
except:
print "No datatype in @type property"
raise
schema_uri = '/profiles/' + datatype + '.json'
object_schema = get_ENCODE(schema_uri)
schema_fields = dict.fromkeys(object_schema['properties'].keys())
jsonschema.validate(json_object, object_schema)
print "Validation passed"
if __name__ == '__main__':
main()