/
wf2js.py
90 lines (73 loc) · 3.28 KB
/
wf2js.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
import datetime
import json
import os
import sys
import urllib.request
jsdefs = {}
# schema at: https://github.com/NCATSTranslator/OperationsAndWorkflows/blob/main/schema/operation.json
raw_schema = 'https://raw.githubusercontent.com/NCATSTranslator/OperationsAndWorkflows/main/schema/operation.json'
sys.path.append(os.path.dirname(os.path.abspath(__file__))+"/../../ARAX/ARAXQuery")
from operation_to_ARAXi import WorkflowToARAXi
wf = WorkflowToARAXi()
in_arax = wf.implemented
# for local testing:
#with open('wfschema.json') as file:
# schema = json.load(file)
with urllib.request.urlopen(raw_schema) as file:
schema = json.loads(file.read().decode())
for operation in schema["$defs"]:
id = schema["$defs"][operation]['properties']['id']['enum'][0]
print("In: "+operation+" ("+id+")")
jsdefs[id] = {}
jsdefs[id]['in_arax'] = id in in_arax
all_props = []
if 'properties' in schema["$defs"][operation]['properties']['parameters']:
all_props.append(schema["$defs"][operation]['properties']['parameters']['properties'])
if 'oneOf' in schema["$defs"][operation]['properties']['parameters']:
jsdefs[id]['warning'] = 'Only one of the following sets of parameters can be used: '
ortxt = ''
for p in schema["$defs"][operation]['properties']['parameters']['oneOf']:
all_props.append(p['properties'])
jsdefs[id]['warning'] += ortxt + '['
comma = ''
for o in p['properties']:
jsdefs[id]['warning'] += comma + o
comma = ', '
jsdefs[id]['warning'] += ']'
ortxt = ' OR '
params = {}
for p in all_props:
for par in p:
if par in params:
del params[par]
params[par] = {}
ref = p[par]
if 'required' in schema["$defs"][operation]['properties']['parameters']:
params[par]['is_required'] = par in schema["$defs"][operation]['properties']['parameters']['required']
else:
params[par]['is_required'] = False;
# rename and discard some fields
if 'minimum' in ref:
ref['min'] = ref.pop('minimum')
if 'maximum' in ref:
ref['max'] = ref.pop('maximum')
if 'example' in ref:
ref['examples'] = ref.pop('example')
if 'items' in ref:
del ref['items']
# capture what we care about
for v in ['type', 'minLength', 'enum', 'default','min', 'max', 'examples', 'description']:
if v in ref:
params[par][v] = ref.pop(v)
for a in ref:
print("--------------- missed: ["+par+"]: "+a)
jsdefs[id]['parameters'] = params
ahora = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
file_name = 'wfoperations.js.new'
fid = open(file_name, 'w')
fid.write("// WARNING:\n")
fid.write("// This file was auto-generated by "+__file__+" on: "+ahora+"\n")
fid.write("//\n")
fid.write("var wf_operations = "+json.dumps(jsdefs,indent=2)+"\n")
fid.close()
print('\nOutput written to: '+file_name)