-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.py
More file actions
334 lines (236 loc) · 15.4 KB
/
Copy pathgenerator.py
File metadata and controls
334 lines (236 loc) · 15.4 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/python
import plistlib
import os
import sys
import getopt
import dircache
import glob
import commands
import json
from fileimporter import ProjectFileImporter
sys.dont_write_bytecode = True
from constants import Type
from constants import MappingKey
class ClassGenerator:
def __init__(self, mappingPath, output_directory, version, testEnabled, jsonFormatEnabled):
self.mappingPath = mappingPath
self.output_directory = output_directory
self.version = version
self.testEnabled = testEnabled
self.jsonFormatEnabled = jsonFormatEnabled
def internalGeneratedClass(self):
propertyMappings = self.propertyMappingsArray()
fileString = self.baseTemplate(self.getPathForFile("internal_class_template.txt"))
fileString = str.replace(fileString, "{ BASE_CLASS_LIBRARY_IMPORT }", self.base_class_imports(propertyMappings))
fileString = str.replace(fileString, "{ BASE_CLASS }", self.base_class_extention(propertyMappings))
fileString = str.replace(fileString, "{ OPTIONALS }", self.optional_property_definitions(propertyMappings))
fileString = str.replace(fileString, "{ NONOPTIONALS }", self.non_optional_property_definitions(propertyMappings))
fileString = str.replace(fileString, "{ REQUIRED_INIT_PARAMS }", self.required_init_properties_string(propertyMappings))
fileString = str.replace(fileString, "{ REQUIRED_INIT_SETTERS }", self.required_init_properties_setters_string(propertyMappings))
fileString = str.replace(fileString, "{ FAILABLE_INIT_TEMP_NONOPTIONALS }", self.init_temp_non_optionals(propertyMappings))
fileString = str.replace(fileString, "{ SELF_NONOPTIONALS_INIT }", self.non_optional_self_init_parameters(propertyMappings))
fileString = str.replace(fileString, "{ OPTIONALS_UNWRAP }", self.unwrap_optional_parameters(propertyMappings))
fileString = str.replace(fileString, "{ NONOPTIONALS_UNWRAP }", self.unwrap_non_optional_parameters(propertyMappings))
fileString = str.replace(fileString, "{ DEBUG_DESCRIPTION_OPTIONALS }", self.debug_optional_property_definitions(propertyMappings))
fileString = str.replace(fileString, "{ DEBUG_DESCRIPTION_NONOPTIONALS }", self.debug_non_optional_property_definitions(propertyMappings))
nonOptionalArray = self.filtered_mappings(propertyMappings, True)
classname = self.mappingPath[self.mappingPath.rindex('/',0,-1)+1:-1] if self.mappingPath.endswith('/') else self.mappingPath[self.mappingPath.rindex('/')+1:].split('.', 1 )[0]
fileString = str.replace(fileString, "{ CLASSNAME }", classname)
if len(nonOptionalArray) == 0:
fileString = str.replace(fileString, "let valuesDict", "let _")
return fileString
def externalGeneratedClass(self):
path = self.baseTemplate(self.getPathForFile("external_class_template.txt"))
return path
'''
Replaces { BASE_CLASS_LIBRARY_IMPORT } in the template
'''
def base_class_imports(self, propertyMappings):
baseClassString = ""
if MappingKey.ModelConfig in propertyMappings.keys():
if MappingKey.ModelImport in propertyMappings[MappingKey.ModelConfig].keys():
for lib in propertyMappings[MappingKey.ModelConfig][MappingKey.ModelImport]:
baseClassString = baseClassString + '\n' + 'import ' + str(lib)
return baseClassString
'''
Replaces { BASE_CLASS } in the template
'''
def base_class_extention(self, propertyMappings):
baseClassString = ""
if MappingKey.ModelConfig in propertyMappings.keys():
if MappingKey.ModelBaseClass in propertyMappings[MappingKey.ModelConfig].keys():
baseClassString = ": " + str(propertyMappings[MappingKey.ModelConfig][MappingKey.ModelBaseClass])
return baseClassString
'''
Replaces { OPTIONALS } in the template
'''
def optional_property_definitions(self, propertyMappings):
valueTemplate = "attributes var propertyname : datatype?"
arrayTemplate = "attributes var propertyname : [datatype]?"
dictionatyTemplate = "attributes var propertyname : [String : datatype]?"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, True)
return "\t" + self.generate_template_string(filteredMappings, templateArray, False, "", "\r\n " )
'''
Replaces { NONOPTIONALS } in the template
'''
def non_optional_property_definitions(self, propertyMappings):
valueTemplate = "attributes var propertyname : datatype"
arrayTemplate = "attributes var propertyname : [datatype]"
dictionatyTemplate = "attributes var propertyname : [String : datatype]"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
if len(filteredMappings) == 0 :
return ""
return "\r\n\t" + self.generate_template_string(filteredMappings, templateArray, False, "", "\r\n " )
'''
Replaces { REQUIRED_INIT_PARAMS } in the template
'''
def required_init_properties_string(self, propertyMappings):
valueTemplate = "propertyname _propertyname : datatype"
arrayTemplate = "propertyname _propertyname : [datatype]"
dictionatyTemplate = "propertyname _propertyname : [String : datatype]"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
return self.generate_template_string(filteredMappings, templateArray, True, "\t\t\t ", ",\r\n " )
'''
Replaces { REQUIRED_INIT_SETTERS } in the template
'''
def required_init_properties_setters_string(self, propertyMappings):
valueTemplate = "propertyname = _propertyname"
templateArray = [valueTemplate, valueTemplate, valueTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
if len(filteredMappings) == 0 :
return ""
return "\r\n\t" + self.generate_template_string(filteredMappings, templateArray, False, "\t\t", "\r\n " )
'''
Replaces { FAILABLE_INIT_TEMP_NONOPTIONALS } in the template
'''
def init_temp_non_optionals(self, propertyMappings):
valueTemplate = "let temp_propertyname : datatype = typeCast(valuesDict[\"propertyname\"])!"
arrayTemplate = "let temp_propertyname : [datatype] = typeCast(valuesDict[\"propertyname\"])!"
dictionatyTemplate = "let temp_propertyname : [String : datatype] = typeCast(valuesDict[\"propertyname\"])!"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
if len(filteredMappings) == 0 :
return ""
return "\r\n\t\t\t" + self.generate_template_string(filteredMappings, templateArray, True, "\t\t", "\r\n " )
'''
Replaces { SELF_NONOPTIONALS_INIT } in the template
'''
def non_optional_self_init_parameters(self, propertyMappings):
valueTemplate = "propertyname : temp_propertyname"
templateArray = [valueTemplate, valueTemplate, valueTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
return self.generate_template_string(filteredMappings, templateArray, True, "\t\t\t\t ", "\r\n " )
'''
Replaces { SELF_NONOPTIONALS_INIT } in the template
'''
def non_optional_self_init_parameters(self, propertyMappings):
valueTemplate = "propertyname : temp_propertyname"
templateArray = [valueTemplate, valueTemplate, valueTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
return self.generate_template_string(filteredMappings, templateArray, True, "\t\t\t ", ",\r\n " )
'''
Replaces { OPTIONALS_UNWRAP } in the template
'''
def unwrap_optional_parameters(self, propertyMappings):
valueTemplate = "if let unwrapped_propertyname : Any = valuesDict[\"propertyname\"] { \r\n\t\t\t\tpropertyname = typeCast(unwrapped_propertyname)! \r\n\t\t\t}\r\n"
arrayTemplate = "if let unwrapped_propertyname : Any = valuesDict[\"propertyname\"] { \r\n\t\t\t\tpropertyname = typeCast(unwrapped_propertyname)! \r\n\t\t\t}\r\n"
dictionatyTemplate = "if let unwrapped_propertyname : Any = valuesDict[\"propertyname\"] { \r\n\t\t\t\tpropertyname = typeCast(unwrapped_propertyname)! \r\n\t\t\t}\r\n"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, True)
return self.generate_template_string(filteredMappings, templateArray, True, "\t\t", "\r\n " )
'''
Replaces { NONOPTIONALS_UNWRAP } in the template
'''
def unwrap_non_optional_parameters(self, propertyMappings):
valueTemplate = "if let unwrapped_propertyname : Any = valuesDict[\"propertyname\"] { \r\n\t\t\t\tpropertyname = typeCast(unwrapped_propertyname)! \r\n\t\t\t}\r\n"
templateArray = [valueTemplate, valueTemplate, valueTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
return self.generate_template_string(filteredMappings, templateArray, True, "\t\t", "\r\n " )
'''
Replaces { DEBUG_DESCRIPTION_OPTIONALS } in the template
'''
def debug_optional_property_definitions(self, propertyMappings):
valueTemplate = "if let unwrapped_propertyname = propertyname { \r\n\t\t\tdebug_string += \"\\n - propertyname : \(unwrapped_propertyname)\"\r\n\t\t}\r\n"
arrayTemplate = "if let unwrapped_propertyname = propertyname { \r\n\t\t\tdebug_string += \" \\n\\n - propertyname : \\n\"\r\n\n\t\t\tif unwrapped_propertyname.count > 0 {\n\t\t\t\tfor value in unwrapped_propertyname {\n\t\t\t\t\tdebug_string += \"\\n \(String(describing: value).replacingOccurrences(of: \" \", with: \" \"))\"\n\t\t\t\t}\n\t\t\t} \n\t\t} else {\n\t\t\tdebug_string += \"[ ]\"\n\t\t}\n\t\t\t"
dictionatyTemplate = "if let unwrapped_propertyname = propertyname { \r\n\t\t\tdebug_string += \" \\n\\n - propertyname : \\n\"\r\n\n\t\t\tif unwrapped_propertyname.count > 0 {\n\t\t\t\tfor (_, value) in unwrapped_propertyname {\n\t\t\t\t\tdebug_string += \"\\n \(String(describing: value).replacingOccurrences(of: \" \", with: \" \"))\"\n\t\t\t\t}\n\t\t\t} \n\t\t} else {\n\t\t\tdebug_string += \"[ ]\"\n\t\t}\n\t\t\t"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, True)
return self.generate_template_string(filteredMappings, templateArray, True, "\t", "\r " )
'''
Replaces { DEBUG_DESCRIPTION_NONOPTIONALS } in the template
'''
def debug_non_optional_property_definitions(self, propertyMappings):
valueTemplate = "debug_string += \"\\n - propertyname : \(propertyname)\""
arrayTemplate = "\n\t\tdebug_string += \" \\n\\n - propertyname : \\n\"\r\n\n\t\tif propertyname.count > 0 {\n\t\t\tfor value in propertyname {\n\t\t\t\tdebug_string += \"\\n \(String(describing: value).replacingOccurrences(of: \" \", with: \" \"))\"\n\t\t\t}\n\t\t} else {\n\t\t\tdebug_string += \"[ ]\"\n\t\t}"
dictionatyTemplate = "\n\t\tdebug_string += \" \\n\\n - propertyname : \\n\"\r\n\n\t\tif propertyname.count > 0 {\n\t\t\tfor (_, value) in propertyname {\n\t\t\t\tdebug_string += \"\\n \(String(describing: value).replacingOccurrences(of: \" \", with: \" \"))\"\n\t\t\t\tdebug_string += \"\"\n\t\t\t}\n\t\t} else {\n\t\t\tdebug_string += \"[ ]\"\n\t\t}"
templateArray = [valueTemplate, arrayTemplate, dictionatyTemplate]
filteredMappings = self.filtered_mappings(propertyMappings, False)
return self.generate_template_string(filteredMappings, templateArray, True, "\t", "\r " )
def generate_template_string(self, propertyMappings, templateArray, skipInitialIndentation, indentation, carriageString):
if len(propertyMappings) == 0 :
return ""
propertyString = ""
for propertyKey in propertyMappings.keys():
propertyMapping = propertyMappings[propertyKey]
propertyType = str(propertyMapping[MappingKey.Type])
isMappingOptional = self.is_property_mapping_optional(propertyMapping)
templateValues = {}
templateValues["propertyname"] = propertyKey
templateValues["datatype"] = propertyType
templateValues["attributes "] = ''
if MappingKey.Attributes in propertyMapping.keys():
templateValues["attributes "] = str(propertyMapping[MappingKey.Attributes]) + ' '
templateIndex = 0
if skipInitialIndentation == False or propertyString != "":
propertyString += indentation
if propertyType in Type.CollectionTypes:
templateValues["datatype"] = str(propertyMapping[MappingKey.SubType])
if propertyType == Type.ArrayType:
templateIndex = 1
elif propertyType == Type.DictionaryType:
templateIndex = 2
propertyString += self.dictionaryValueString(templateArray[templateIndex], templateValues)
if propertyMappings.keys().index(propertyKey) <= len(propertyMappings.keys()) - 2:
propertyString += carriageString
else:
propertyString += ""
return propertyString
def is_property_mapping_optional(self, mapping):
if MappingKey.NonOptional in mapping.keys() and mapping[MappingKey.NonOptional]:
return False
else:
return True
def dictionaryValueString(self, templateString, dictionaryValues):
renderedString = templateString
for key in dictionaryValues.keys():
renderedString = str.replace(renderedString, key, str(dictionaryValues[key]))
return renderedString
def propertyMappingsArray(self):
if self.jsonFormatEnabled:
return json.load(open(self.mappingPath))
else:
return plistlib.readPlist(self.mappingPath)
def filtered_mappings(self, propertyMappings, optional):
filteredMappings = {}
for propertyKey in propertyMappings.keys():
propertyMapping = propertyMappings[propertyKey]
if MappingKey.ModelConfig == propertyKey:
continue
if self.is_property_mapping_optional(propertyMapping) == optional:
filteredMappings[propertyKey] = propertyMapping
return filteredMappings
def getPathForFile(self, fileName):
return str.replace(os.path.abspath(__file__), "generator.py", fileName)
def baseTemplate(self, path):
propertyMappings = self.propertyMappingsArray()
fileString = str.replace(open(path, 'r').read(), '\n', '\r\n')
classname = self.mappingPath[self.mappingPath.rindex('/',0,-1)+1:-1] if self.mappingPath.endswith('/') else self.mappingPath[self.mappingPath.rindex('/')+1:].split('.', 1 )[0]
if self.testEnabled == 0:
fileString = str.replace(fileString, "{ TEST_IMPORT }", "import JSONModelKit\r\n")
else:
fileString = str.replace(fileString, "{ TEST_IMPORT }", '\n')
fileString = str.replace(fileString, "{ CLASSNAME }", classname)
return fileString