This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcodegen_writer.py
333 lines (299 loc) · 20.1 KB
/
codegen_writer.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
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
# OData Python Client and Server Libraries ver. 1.0.0
# Copyright (c) Microsoft Corporation
# All rights reserved.
# MIT License
# 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.
from codegen_initializer import *
import codegen_template as TP
INDENT = " "
class codegen_writer:
def __init__(self, initializer):
self._initializer = initializer
def begin_generate_file(self):
if self._initializer is None:
print "Initialize failed"
return
outf = open(self._initializer.get_config().file_name + ".py", "w")
self._generate_import_files(outf)
for schema_name in self._initializer._code_gen_map:
self._generate_enum_types(outf, self._initializer._code_gen_map[schema_name])
self._generate_complex_types(outf, self._initializer._code_gen_map[schema_name])
self._generate_entity_types(outf, self._initializer._code_gen_map[schema_name])
self._generate_entity_container(outf, self._initializer._code_gen_map[schema_name])
self._generate_derived_creator(outf, self._initializer._code_gen_map[schema_name])
def _primitive_resolver(self, _info):
ret = r"eval(primitive_value.to_string())"
if _info.class_member_type in ("uint8_t", "int8_t", "int16_t", "int32_t", "int64_t"):
ret = r"int(primitive_value.to_string())"
elif _info.class_member_type in ("float", "double", "long double"):
ret = r"float(primitive_value.to_string())"
elif _info.class_member_type == "::utility::string_t":
ret = r"primitive_value.to_string()"
elif _info.class_member_type == "bool":
ret = r"primitive_value.to_string() == 'true'"
return ret
def _generate_import_files(self, outf):
outf.write(r"import odata_client_python" + '\n')
outf.write(r"from odata_service_context import *" + '\n')
outf.write(r"from codegen_base import *" + '\n')
outf.write('\n')
def _generate_class_begin(self, outf, class_info):
if class_info.base_class_name:
outf.write(r"class {}({}):".format(class_info.class_name, class_info.base_class_name) + "\n")
else:
outf.write(r"class {}:".format(class_info.class_name) + "\n")
def _generate_derived_creator(self, outf, _schema_info):
for class_info in _schema_info.class_list:
if class_info.type != CLASS_TYPE.E_CLASS_COMPLEX and class_info.type != CLASS_TYPE.E_CLASS_ENTITY:
continue
if class_info.class_name in _schema_info.derived_classes:
derived_classes = _schema_info.derived_classes[class_info.class_name]
outf.write(r"{}._derived_creator_map = ".format(class_info.class_name) + '{')
for derived_class_name in derived_classes:
outf.write(r'"{0}" : {0}, '.format(derived_class_name))
outf.write('}\n')
def _generate_enum_types(self, outf, _schema_info):
for class_info in _schema_info.class_list:
if class_info.type != CLASS_TYPE.E_CLASS_ENUM:
continue
self._generate_class_begin(outf, class_info)
class_property_map = _schema_info.class_property_map[class_info.class_name]
for property_name in class_property_map:
_info = class_property_map[property_name]
self._generate_enum_member(outf, _info)
self._generate_common_methods_in_enum(outf, class_info, class_property_map)
outf.write("\n")
def _generate_common_methods_in_enum(self, outf, class_info, class_property_map):
outf.write(TP.GET_ENUM_TYPE_NAMESPACE.format(class_info.edm_namespace))
outf.write(TP.BEGIN_GET_ENUM_TYPE_FROM_STRING)
for property_name in class_property_map:
_info = class_property_map[property_name]
outf.write(TP.ON_GET_ENUM_TYPE_FROM_STRING.format(class_info.class_name, _info.edm_name, _info.class_member_name))
outf.write(TP.END_GET_ENUM_TYPE_FROM_STRING)
outf.write(TP.BEGIN_GET_STRING_FROM_ENUM_TYPE)
for property_name in class_property_map:
_info = class_property_map[property_name]
outf.write(TP.ON_GET_STRING_FROM_ENUM_TYPE.format(class_info.class_name, _info.edm_name, _info.class_member_name))
outf.write(TP.END_GET_STRING_FROM_ENUM_TYPE)
def _generate_enum_member(self, outf, _info):
outf.write(INDENT + _info.class_member_name + " = " + _info.default_value + "\n")
def _generate_complex_types(self, outf, _schema_info):
for class_info in _schema_info.class_list:
if class_info.type != CLASS_TYPE.E_CLASS_COMPLEX:
continue
self._generate_class_begin(outf, class_info)
class_property_map = _schema_info.class_property_map[class_info.class_name]
self._generate_common_methods_in_complex(outf, class_info, class_property_map)
for property in class_property_map:
_info = class_property_map[property]
self._generate_property_in_complex(outf, class_info, _info)
self._generate_complex_instance_creator(outf, class_info, class_property_map)
self._generate_to_complex_value(outf, class_info, class_property_map)
outf.write("\n")
def _generate_complex_constructor(self, outf, class_info, class_property_map):
outf.write(TP.BEGIN_COMPLEX_CONSTRUCTOR.format(class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type == PROPERTY_TYPE.E_FUNCTION or _info.type == PROPERTY_TYPE.E_ACTION:
continue
outf.write(TP.ON_PROPERTY_IN_COMPLEX_CONSTRUCTOR.format(_info.class_member_name, _info.default_value))
outf.write('\n')
def _generate_common_methods_in_complex(self, outf, class_info, class_property_map):
self._generate_complex_constructor(outf, class_info, class_property_map)
outf.write(TP.GET_ROOT_URL)
outf.write(TP.EDM_INFO.format(class_info.class_name, class_info.edm_namespace, class_info.edm_name))
def _generate_property_in_complex(self, outf, class_info, _info):
if _info.type == PROPERTY_TYPE.E_PRIMITIVE:
outf.write(TP.PRIMITIVE_PROPERTY_IN_COMPLEX_MAPPING.format(_info.class_member_name, _info.edm_name, self._primitive_resolver(_info)))
elif _info.type == PROPERTY_TYPE.E_COMPLEX:
outf.write(TP.COMPLEX_PROPERTY_IN_COMPLEX_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_ENUM:
outf.write(TP.ENUM_PROPERTY_IN_COMPLEX_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_PRIMITIVE:
outf.write(TP.COLLECTION_PRIMITIVE_PROPERTY_IN_COMPLEX_MAPPING.format(_info.class_member_name, _info.edm_name, self._primitive_resolver(_info)))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_ENUM:
outf.write(TP.COLLECTION_ENUM_PROPERTY_IN_COMPLEX_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
outf.write('\n')
def _generate_complex_instance_creator(self, outf, class_info, class_property_map):
outf.write(TP.BEGIN_COMPLEX_INSTANCE_CREATOR.format(class_info.class_name, class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type == PROPERTY_TYPE.E_ACTION or _info.type == PROPERTY_TYPE.E_FUNCTION:
continue
outf.write(TP.ON_PROPERTY_IN_COMPLEX_INSTANCE_CREATOR.format(_info.class_member_name))
outf.write(TP.END_COMPLEX_INSTANCE_CREATOR)
def _generate_to_complex_value(self, outf, class_info, class_property_map):
if class_info.base_class_name == "type_base":
outf.write(TP.BEGIN_TO_COMPLEX_VALUE)
else:
outf.write(TP.BEGIN_TO_COMPLEX_VALUE_WITH_BASE_CLASS.format(class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type in (PROPERTY_TYPE.E_ACTION, PROPERTY_TYPE.E_FUNCTION, PROPERTY_TYPE.E_ENTITY, PROPERTY_TYPE.E_COLLECTION_ENTITY):
continue
outf.write(TP.ON_TO_COMPLEX_VALUE.format(_info.class_member_name))
outf.write(TP.END_TO_COMPLEX_VALUE)
def _generate_entity_types(self, outf, _schema_info):
for class_info in _schema_info.class_list:
if class_info.type != CLASS_TYPE.E_CLASS_ENTITY:
continue
self._generate_class_begin(outf, class_info)
class_property_map = _schema_info.class_property_map[class_info.class_name]
self._generate_common_methods_in_entity(outf, class_info, class_property_map)
for property in class_property_map:
_info = class_property_map[property]
self._generate_property_in_entity(outf, class_info, _info)
self._generate_entity_instance_creator(outf, class_info, class_property_map)
self._generate_to_entity_value(outf, class_info, class_property_map)
outf.write('\n')
def _generate_entity_constructor(self, outf, class_info, class_property_map):
outf.write(TP.BEGIN_ENTITY_CONSTRUCTOR.format(class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type == PROPERTY_TYPE.E_FUNCTION or _info.type == PROPERTY_TYPE.E_ACTION:
continue
outf.write(TP.ON_PROPERTY_IN_ENTITY_CONSTRUCTOR.format(_info.class_member_name, _info.default_value))
outf.write('\n')
def _generate_common_methods_in_entity(self, outf, class_info, class_property_map):
self._generate_entity_constructor(outf, class_info, class_property_map)
outf.write(TP.GET_ROOT_URL)
outf.write(TP.EDM_INFO.format(class_info.class_name, class_info.edm_namespace, class_info.edm_name))
def _generate_property_in_entity(self, outf, class_info, _info):
if _info.type == PROPERTY_TYPE.E_ACTION or _info.type == PROPERTY_TYPE.E_FUNCTION:
self._generate_operation_in_entity(outf, class_info, _info)
return
if _info.type == PROPERTY_TYPE.E_PRIMITIVE:
outf.write(TP.PRIMITIVE_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, self._primitive_resolver(_info)))
elif _info.type == PROPERTY_TYPE.E_ENUM:
outf.write(TP.ENUM_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_COMPLEX:
outf.write(TP.COMPLEX_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_ENTITY:
outf.write(TP.NAVIGATION_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_PRIMITIVE:
outf.write(TP.COLLECTION_PRIMITIVE_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, self._primitive_resolver(_info)))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_ENUM:
outf.write(TP.COLLECTION_ENUM_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_COMPLEX:
outf.write(TP.COLLECTION_COMPLEX_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
elif _info.type == PROPERTY_TYPE.E_COLLECTION_ENTITY:
outf.write(TP.COLLECTION_NAVIGATION_PROPERTY_IN_ENTITY_MAPPING.format(_info.class_member_name, _info.edm_name, _info.class_member_type))
outf.write('\n')
def _generate_operation_in_entity(self, outf, class_info, _info):
if _info is None:
return
_operation_info = _info.operation_info
if _operation_info is None:
return
arguments = "self"
for param in _operation_info.params:
arguments += ", " + param.member_name
outf.write(TP.BEGIN_OPERATION.format(_info.class_member_name, arguments))
for param in _operation_info.params:
if param.member_type == PROPERTY_TYPE.E_PRIMITIVE:
outf.write(TP.ON_PRIMITIVE_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name))
elif param.member_type == PROPERTY_TYPE.E_COMPLEX or param.member_type == PROPERTY_TYPE.E_ENTITY:
outf.write(TP.ON_CLASS_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_ENUM:
outf.write(TP.ON_ENUM_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_PRIMITIVE:
outf.write(TP.ON_COLLECTION_PRIMITIVE_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_ENTITY or param.member_type == PROPERTY_TYPE.E_COLLECTION_COMPLEX:
outf.write(TP.ON_COLLECTION_CLASS_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_ENUM:
outf.write(TP.ON_COLLECTION_ENUM_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
is_function = "True" if _info.type == PROPERTY_TYPE.E_FUNCTION else "False"
if _operation_info.return_type == "void":
outf.write(TP.END_OPERATION_VOID.format(_operation_info.executor_name, is_function))
else:
return_type = _operation_info.return_type
if _operation_info.executor_name == "operation_query_primitive":
return_type = '"' + return_type + '"'
outf.write(TP.END_OPERATION_WITH_RETURN_VALUE.format(_operation_info.executor_name, is_function, return_type))
outf.write('\n')
def _generate_entity_instance_creator(self, outf, class_info, class_property_map):
outf.write(TP.BEGIN_ENTITY_INSTANCE_CREATOR.format(class_info.class_name, class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type == PROPERTY_TYPE.E_ACTION or _info.type == PROPERTY_TYPE.E_FUNCTION:
continue
outf.write(TP.ON_PROPERTY_IN_ENTITY_INSTANCE_CREATOR.format(_info.class_member_name))
outf.write(TP.END_ENTITY_INSTANCE_CREATOR)
def _generate_to_entity_value(self, outf, class_info, class_property_map):
if class_info.base_class_name == "type_base":
outf.write(TP.BEGIN_TO_ENTITY_VALUE)
else:
outf.write(TP.BEGIN_TO_ENTITY_VALUE_WITH_BASE_CLASS.format(class_info.base_class_name))
for property in class_property_map:
_info = class_property_map[property]
if _info.type in (PROPERTY_TYPE.E_ACTION, PROPERTY_TYPE.E_FUNCTION, PROPERTY_TYPE.E_ENTITY, PROPERTY_TYPE.E_COLLECTION_ENTITY):
continue
outf.write(TP.ON_TO_ENTITY_VALUE.format(_info.class_member_name))
outf.write(TP.END_TO_ENTITY_VALUE)
def _generate_entity_container(self, outf, _schema_info):
for class_info in _schema_info.class_list:
if class_info.type != CLASS_TYPE.E_CLASS_CONTAINER:
continue
self._generate_class_begin(outf, class_info)
outf.write(TP.ENTITY_CONTAINER_CONSTRUCTOR)
class_property_map = _schema_info.class_property_map[class_info.class_name]
for property_name in class_property_map:
_info = class_property_map[property_name]
if _info.type == PROPERTY_TYPE.E_CONTAINER_ENTITY_SET:
self._generate_entity_set_in_entity_container(outf, class_info, _info)
elif _info.type == PROPERTY_TYPE.E_CONTAINER_SINGLETON:
self._generate_singleton_in_entity_conatiner(outf, class_info, _info)
elif _info.type == PROPERTY_TYPE.E_ACTION or _info.type == PROPERTY_TYPE.E_FUNCTION:
self._generate_operation_imports_in_entity_container(outf, class_info, _info)
else:
continue
def _generate_entity_set_in_entity_container(self, outf, class_info, _info):
outf.write(TP.QUERY_ENTITY_SET_IN_ENTITY_CONTAINER.format(_info.class_member_name, _info.edm_name, _info.strong_type_name))
def _generate_singleton_in_entity_conatiner(self, outf, class_info, _info):
outf.write(TP.QUERY_SINGLETON_IN_ENTITY_CONTAINER.format(_info.class_member_name, _info.edm_name, _info.strong_type_name))
def _generate_operation_imports_in_entity_container(self, outf, class_info, _info):
if _info is None:
return
_operation_info = _info.operation_info
if _operation_info is None:
return
arguments = "self"
for param in _operation_info.params:
arguments += ", " + param.member_name
outf.write(TP.BEGIN_OPERATION_IMPORT.format(_info.class_member_name, arguments))
for param in _operation_info.params:
if param.member_type == PROPERTY_TYPE.E_PRIMITIVE:
outf.write(TP.ON_PRIMITIVE_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name))
elif param.member_type == PROPERTY_TYPE.E_COMPLEX or param.member_type == PROPERTY_TYPE.E_ENTITY:
outf.write(TP.ON_CLASS_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_ENUM:
outf.write(TP.ON_ENUM_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_PRIMITIVE:
outf.write(TP.ON_COLLECTION_PRIMITIVE_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_ENTITY or param.member_type == PROPERTY_TYPE.E_COLLECTION_COMPLEX:
outf.write(TP.ON_COLLECTION_CLASS_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
elif param.member_type == PROPERTY_TYPE.E_COLLECTION_ENUM:
outf.write(TP.ON_COLLECTION_ENUM_PARAMETER_IN_OPERATION.format(param.member_name, param.edm_name, param.member_strong_type_name))
is_function = "True" if _info.type == PROPERTY_TYPE.E_FUNCTION else "False"
if _operation_info.return_type == "void":
outf.write(TP.END_OPERATION_IMPORT_VOID.format(_operation_info.executor_name, is_function))
else:
return_type = _operation_info.return_type
if _operation_info.executor_name == "operation_query_primitive":
return_type = '"' + return_type + '"'
outf.write(TP.END_OPERATION_IMPORT_WITH_RETURN_VALUE.format(_operation_info.executor_name, is_function, return_type))
outf.write('\n')