-
Notifications
You must be signed in to change notification settings - Fork 6
/
readJSON.py
executable file
·203 lines (161 loc) · 5.91 KB
/
readJSON.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
'''
The MIT License (MIT)
Copyright (c) 2013 SinnerSchrader Mobile GmbH
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
__version__ = "2.7.2"
import json
import os
import glob
import shutil
import sys, getopt
from JSONScheme import *
from ObjectiveCCodeGenerator import *
from JavaCodeGenerator import *
jsonfiles = []
inputfile = 'do you have files?'
projectPrefix = ''
target = 'iOS'
dirPathToSaveCodes = './src'
objectSuffix = "JSONObject"
usageString = '\nreadJSON.py [ -p | -t | -o | -s ] [-i]\n'
usageString += 'Options:\n'
usageString += ' -h, --help shows help\n'
usageString += ' -p, --prefix= project prefix (default: S2M)\n'
usageString += ' -s, --suffix= classname suffix (default: JSONObject). Use "-s false" for no suffix\n'
usageString += ' -t, --target= target platform iOS or Android (default: iOS)\n'
usageString += ' -i, --input= meta-JSON file to read\n'
usageString += ' -o, --output= ouput path of generated source codes\n'
if __name__ == "__main__":
argv = sys.argv[1:]
try:
opts, args = getopt.getopt(argv,"ho:p:s:t:i:",["prefix=","suffix=","target=","input=","output="])
except getopt.GetoptError:
print usageString
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print usageString
sys.exit()
elif opt in ("-p", "--prefix"):
projectPrefix = arg
elif opt in ("-t", "--target"):
target = arg
elif opt in ("-i", "--input"):
inputfile = arg
elif opt in ("-o", "--output"):
dirPathToSaveCodes = arg
elif opt in ("-s", "--suffix"):
objectSuffix = arg
if objectSuffix == "false":
objectSuffix = ""
addFiles = False
for arg in argv :
if arg.endswith(inputfile) :
jsonfiles.append(inputfile)
addFiles = True
continue
if arg.startswith('-') and addFiles :
addFiles = False
break
if addFiles :
jsonfiles.append(arg)
if target == 'none' :
print 'error - target platform is not defined'
print usageString
sys.exit()
if len(jsonfiles) == 0 :
print 'error - meta JSON file is not defined'
print usageString
sys.exit()
templatePath = os.path.abspath( __file__ )
templatePath = templatePath.replace('/readJSON.py', '/templates')
if not os.path.exists(dirPathToSaveCodes):
os.makedirs(dirPathToSaveCodes)
def openFileAndParseJSON(filePath):
f = open(filePath)
try:
obj = json.load(f)
finally:
f.close()
return obj
iOS = False
Android = False
if target == 'iOS' :
iOS = True
elif target == 'Android' :
Android = True
else :
print 'error - unknown target platform : ' + target
print usageString
sys.exit()
hasError = False
if iOS :
if dirPathToSaveCodes.endswith("/") :
dirPathToSaveCodes = dirPathToSaveCodes[:-1]
if os.path.exists(dirPathToSaveCodes+ "/abstractInterfaceFiles"):
shutil.rmtree(dirPathToSaveCodes + "/abstractInterfaceFiles")
templateCodeGen = TemplateCodeGenerator()
templateCodeGen.templatePath = templatePath
templateCodeGen.projectPrefix = projectPrefix
templateCodeGen.dirPath = dirPathToSaveCodes
templateCodeGen.writeTemplates()
JSONScheme.projectPrefix = projectPrefix
JSONScheme.objectSuffix = objectSuffix
print "\nGenerate source codes for " + target + ", with Project Prefix \'" + projectPrefix + "\' and suffix \'" + objectSuffix + "\'"
print "Output path : \'" + dirPathToSaveCodes + "\'\n"
for filePath in jsonfiles :
print "read " + filePath + " to parse ...."
jsonObj = openFileAndParseJSON(filePath)
if type(jsonObj) == list :
for dic in jsonObj :
schemeObj = JSONScheme()
schemeObj.projectPrefix = projectPrefix
schemeObj.objectSuffix = objectSuffix
if schemeObj.parseDictionary(dic) == False:
hasError = True
break
elif type(jsonObj) == dict :
schemeObj = JSONScheme()
if schemeObj.parseDictionary(jsonObj) == False :
hasError = True
else :
hasError = True
print "error : no JSON Scheme"
break;
if hasError :
print "error - Fail to make scheme object."
break;
if hasError :
break
if not hasError:
codeGen = 0
if Android :
codeGen = JavaCodeGenerator()
else :
codeGen = ObjectiveCCodeGenerator()
codeGen.projectPrefix = projectPrefix
codeGen.dirPath = dirPathToSaveCodes
codeGen.objectSuffix = objectSuffix
allSchemes = JSONScheme.JSONSchemeDic
codeGen.JSONSchemeDic = allSchemes
rootDic = allSchemes["ROOT"]
allRootKeys = rootDic.keys()
for typeName in rootDic :
obj = rootDic[typeName];
if obj.isNaturalType() == False :
codeGen.make(obj)