forked from smooth80/defold
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb_script_doc.py
132 lines (117 loc) · 4.22 KB
/
web_script_doc.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
# Copyright 2020 The Defold Foundation
# Licensed under the Defold License version 1.0 (the "License"); you may not use
# this file except in compliance with the License.
#
# You may obtain a copy of the License, together with FAQs at
# https://www.defold.com/license
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
import json, os
from optparse import OptionParser
def transform_refdoc(doc):
functions = []
macros = []
messages = []
constants = []
properties = []
structs = []
enums = []
typedefs = []
methods = []
for e in doc['elements']:
if e['type'] == 'FUNCTION':
functions.append(e)
if e['type'] == 'MACRO':
macros.append(e)
elif e['type'] == 'MESSAGE':
messages.append(e)
elif e['type'] == 'VARIABLE':
constants.append(e)
elif e['type'] == 'PROPERTY':
properties.append(e)
elif e['type'] == 'STRUCT':
structs.append(e)
elif e['type'] == 'ENUM':
enums.append(e)
elif e['type'] == 'TYPEDEF':
typedefs.append(e)
elif e['type'] == 'METHOD':
methods.append(e)
t = {}
t['functions'] = functions
t['methods'] = methods
t['macros'] = macros
t['messages'] = messages
t['constants'] = constants
t['properties'] = properties
t['structs'] = structs
t['enums'] = enums
t['typedefs'] = typedefs
t['info'] = doc['info']
return t
# Merge doc2 into doc1
def merge_refdocs(doc1, doc2):
if doc1['info']['name'] == '':
doc1['info']['name'] = doc2['info']['name']
if doc1['info']['brief'] == '':
doc1['info']['brief'] = doc2['info']['brief']
if doc1['info']['description'] == '':
doc1['info']['description'] = doc2['info']['description']
doc1['functions'].extend(doc2['functions'])
doc1['methods'].extend(doc2['methods'])
doc1['macros'].extend(doc2['macros'])
doc1['messages'].extend(doc2['messages'])
doc1['constants'].extend(doc2['constants'])
doc1['properties'].extend(doc2['properties'])
doc1['structs'].extend(doc2['structs'])
doc1['enums'].extend(doc2['enums'])
doc1['typedefs'].extend(doc2['typedefs'])
return doc1
def transform_doc(raw_json, version, jsondata, index):
# Transform and merge namespaces
namespace = raw_json['info']['namespace']
if namespace in jsondata:
# If namespace is saved - merge
a = jsondata[namespace]
b = transform_refdoc(raw_json)
jsondata[namespace] = merge_refdocs(a, b)
else:
jsondata[namespace] = transform_refdoc(raw_json)
jsondata[namespace]['info']['version'] = version
# Save index
for namespace in jsondata:
index[namespace] = jsondata[namespace]['info']
return jsondata, index
# Save individual entries
# for namespace in jsondata:
# store_json(sha1, namespace + '.json', jsondata[namespace])
# if latest:
# store_json('latest', namespace + '.json', jsondata[namespace])
#
if __name__ == '__main__':
usage = "usage: %prog [options] INFILE(s) OUTDIR"
parser = OptionParser(usage = usage)
parser.add_option("-v", "--version", dest="version",
help="Version string", metavar="VERSION", default='latest')
(options, args) = parser.parse_args()
if len(args) < 2:
parser.error('At least one argument required')
jsondata = {}
index = {}
orig_json = ''
for name in args[:-1]:
f = open(name, 'rb')
raw_json = json.loads(f.read())
jsondata, index = transform_doc(raw_json, options.version, jsondata, index)
f.close()
output_dir = args[-1]
for namespace in jsondata:
output_file = os.path.join(output_dir, namespace + '.json')
f = open(output_file, "wb")
json.dump(jsondata[namespace], f, indent = 2)
output_file = os.path.join(output_dir, 'index.json')
f = open(output_file, "wb")
json.dump(index, f, indent = 2)