forked from ga4gh-schemablocks/sb-schemablocks-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sbOpenAPIparser.py
215 lines (152 loc) · 6.76 KB
/
sbOpenAPIparser.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/local/bin/python3
import sys, re
from ruamel.yaml import YAML
from os import path as path
import argparse
# local
dir_path = path.dirname(path.abspath(__file__))
"""podmd
The `sbOpenAPIparser` tool reads schema files defined using OpenAPI and extracts
the embedded schemas as individual YAML documents, with an added metadata header
compatible to use in [SchemaBlocks](https://schemablocks.org/categories/schemas.html)
schema documents.
##### Examples
* `python3 sbOpenAPIparser.py -o ~/GitHub/ga4gh-schemablocks/sb-discovery-search/schemas/ -f ~/GitHub/ga4gh-schemablocks/sb-discovery-search/source/search-api.yaml -p "sb-discovery-search" -m ~/GitHub/ga4gh-schemablocks/sb-discovery-search/source/header.yaml`
podmd"""
################################################################################
################################################################################
################################################################################
def _get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", help="path to the sbOpenAPIparser.yaml configuration file")
args = parser.parse_args()
return args
################################################################################
def main():
"""podmd
A single-file OpenAPI schema is read in from a YAML file, and the
`components.schemas` object is iterated over, extracting each individual
schema.
This schema is updated with metadata, either from a provided SchemaBlocks
header file or with a default from a configuration file (e.g.
`sbOpenAPIparser.yaml`).
Some of the parameter values are adjusted (which probably will have to be
expanded for different use cases); e.g. the internal reference paths
are interpreted as pointing to individual schema files in the current
directory.
end_podmd"""
cff = _get_config_path(dir_path, _get_args())
yaml = YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
try:
with open( cff ) as cf:
config = yaml.load( cf )
except Exception as e:
print("Error loading the config file ({}): {}".format(cff, e) )
exit()
config.update( { "config_base_path": path.dirname(cff) } )
_check_config(config)
with open( config[ "schemafile" ] ) as f:
oas = yaml.load( f )
_config_add_project_specs(config, oas)
for s_name in oas["components"]["schemas"].keys():
f_name = s_name+".yaml"
print(f_name)
s = oas["components"]["schemas"][ s_name ]
_add_header(config, s, s_name)
_fix_relative_ref_paths(s)
if "$id" in s:
s[ "$id" ] = re.sub( r"__schema__", s_name, s[ "$id" ] )
s[ "$id" ] = re.sub( r"__project__", config[ "project" ], s[ "$id" ] )
ofp = path.join( config[ "outdir" ], f_name )
with open(ofp, 'w') as of:
docs = yaml.dump(s, of)
################################################################################
def _get_config_path(dir_path, args):
cfp = path.join( path.abspath( dir_path ), "sbOpenAPIparser.yaml")
if vars(args)["config"]:
cfp = path.abspath( vars(args)["config"] )
if not path.isfile( cfp ):
print("""
The configuration file:
{}
...does not exist; please use a correct "-c" parameter".
""".format(cfp))
sys.exit( )
return cfp
################################################################################
def _check_config(config):
for p in ["schemafile", "outdir", "project"]:
if not p in config:
print('No {} parameter has been provided the configuration file => exiting.'.format(p))
sys.exit( )
config.update({ "outdir": path.join( config[ "config_base_path" ], config[ "outdir" ]) } )
config.update({ "schemafile": path.join( config[ "config_base_path" ], config[ "schemafile" ]) } )
if not path.isdir( config[ "outdir" ] ):
print("""
The output directory:
{}
...does not exist; please use a correct relative path in the configuration file.
""".format(config[ "outdir" ]))
sys.exit( )
if not path.isfile( config[ "schemafile" ] ):
print("""
The input file:
{}
...does not exist; please use a correct relative path in the configuration file.
""".format(config[ "outdir" ]))
sys.exit( )
return config
################################################################################
def _config_add_project_specs(config, oas):
h_k_n = len( config["header"].keys() )
if "info" in oas:
if "version" in oas[ "info" ]:
config["header"].update( { "$id" : re.sub( r"__schemaversion__", oas["info"][ "version" ], config["header"]["$id" ]) } )
config["header"].insert(h_k_n, "version", oas["info"][ "version" ])
return config
################################################################################
def _add_header(config, s, s_name):
pos = 0
for k, v in config[ "header" ].items():
s.insert(pos, k, v)
pos += 1
s.insert(pos, "title", s_name)
return s
################################################################################
def _fix_relative_ref_paths(s):
"""podmd
The path fixes here are very much "experience driven" and should be replaced
with a more systematic version, including existence & type checking ...
podmd"""
properties = s
if "properties" in s:
properties = s[ "properties" ]
for p in properties.keys():
if '$ref' in properties[ p ]:
if not "http" in properties[ p ][ '$ref' ]:
properties[ p ][ '$ref' ] = re.sub( '#/components/schemas/', '', properties[ p ][ '$ref' ] ) + '.yaml#/'
if 'items' in properties[ p ]:
if '$ref' in properties[ p ][ "items" ]:
if not "http" in properties[ p ][ "items" ][ '$ref' ]:
properties[ p ][ "items" ][ '$ref' ] = re.sub( '#/components/schemas/', '', properties[ p ][ "items" ][ '$ref' ] ) + '.yaml#/'
if "properties" in s:
s[ "properties" ].update( { p: properties[ p ] } )
else:
s.update( { p: properties[ p ] } )
if "oneOf" in s:
o_o = [ ]
for o in s[ "oneOf" ]:
if "$ref" in o.keys():
if not "http" in o["$ref"]:
o["$ref"] = re.sub( '#/components/schemas/', '', o["$ref"] ) + '.yaml#/'
o_o.append( { "$ref": o["$ref"]} )
else:
o_o.append( o )
s.update( { "oneOf": o_o } )
return s
################################################################################
################################################################################
################################################################################
if __name__ == '__main__':
main( )