-
Notifications
You must be signed in to change notification settings - Fork 39
/
json_schema_convert.py
138 lines (110 loc) · 3.76 KB
/
json_schema_convert.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
# coding:utf-8
# Simple utility to convert json schema using jinja2 template
import argparse
import glob
import os
import json
import yaml
import jinja2
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
def process_properties(properties):
if not properties:
return
for id, property in properties.items():
if property.get("xmlType"):
continue
ref = property.get("$ref")
if ref:
refs = ref.split("/")
property["xmlType"] = refs[-1]
continue
if property["type"] == "object":
process_schema(property)
if property.get("relation"):
ref = property.get("items", {}).get(
"properties", {}).get("attr", {}).get("$ref")
if ref:
refs = ref.split("/")
property["xmlType"] = refs[-1]
continue
if property["type"] == "string":
if property.get("format") == "date-time":
property["xmlType"] = "xsd:dateTime"
else:
property["xmlType"] = "xsd:string"
if property["type"] == "boolean":
property["xmlType"] = "xsd:boolean"
if property["type"] == "integer":
property["xmlType"] = "xsd:integer"
def process_schema(schema):
process_properties(schema.get("properties"))
process_properties(schema.get("references"))
process_properties(schema.get("parents"))
def process_data(schemas, definitions, schema):
data_types = schema.get("definitions")
if data_types:
definitions.update(data_types)
for id, data_type in data_types.items():
process_schema(data_type)
return
if schema.get("schema"):
process_schema(schema["schema"])
schemas.append(schema)
def read_schema(src_path):
definitions = {}
schemas = []
data_model = {
"definitions": definitions,
"schemas": schemas
}
files = glob.glob(src_path + '/*.json')
for file_path in files:
file_handler = open(file_path, 'r')
try:
schema = json.load(file_handler)
except:
continue
process_data(schemas, definitions, schema)
files = glob.glob(src_path + '/*.yaml')
for file_path in files:
file_handler = open(file_path, 'r')
try:
schema = yaml.load(file_handler)
except:
continue
process_data(schemas, definitions, schema)
return data_model
def output(template_file, data_model):
# Ensure destination directory created
j2_env = jinja2.Environment(
loader=jinja2.FileSystemLoader(THIS_DIR),
trim_blocks=True)
print j2_env.get_template(template_file).render(data_model)
def convert():
parser = argparse.ArgumentParser(
description='Converter from JSON Schema to XML Schema plus IFMap')
parser.add_argument('src',
action='store',
nargs=None,
const=None,
default=None,
type=str,
choices=None,
help='Directory path where your json schema located.',
metavar=None)
parser.add_argument('template',
action='store',
nargs=None,
const=None,
default=None,
type=str,
choices=None,
help='Template file to apply',
metavar=None)
args = parser.parse_args()
src_path = args.src
template_path = args.template
data_model = read_schema(src_path)
output(template_path, data_model)
if __name__ == '__main__':
convert()