@darabonba/python-generator 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/generator.js +10 -6
- package/package.json +1 -1
- package/tests/expected/complex/tea_python_tests/client.py +5 -4
- package/tests/expected/multi/tea_python_tests/model/user.py +3 -0
- package/tests/fixtures/complex/main.dara +1 -0
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/Teafile +7 -1
- package/tests/fixtures/multi/libraries/darabonba_Util_0.2.11/util.tea +2 -0
- package/tests/fixtures/multi/model/user.dara +2 -0
- package/tests/output/complex/tea_python_tests/client.py +5 -4
- package/tests/output/multi/tea_python_tests/model/user.py +3 -0
- package/src/generator.js +0 -231
- package/src/langs/common/combinator.js +0 -193
- package/src/langs/common/config.js +0 -60
- package/src/langs/common/enum.js +0 -179
- package/src/langs/common/items.js +0 -551
- package/src/langs/common/package_info.js +0 -53
- package/src/langs/python/combinator.js +0 -1898
- package/src/langs/python/config.js +0 -169
- package/src/langs/python/files/.gitignore.tmpl +0 -5
- package/src/langs/python/files/__init__.py.tmpl +0 -1
- package/src/langs/python/files/setup.py.tmpl +0 -74
- package/src/langs/python/package_info.js +0 -78
- package/src/langs/python2/combinator.js +0 -1770
- package/src/langs/python2/config.js +0 -162
- package/src/langs/python2/files/.gitignore.tmpl +0 -5
- package/src/langs/python2/files/__init__.py.tmpl +0 -1
- package/src/langs/python2/files/setup.py.tmpl +0 -82
- package/src/langs/python2/package_info.js +0 -78
- package/src/lib/debug.js +0 -55
- package/src/lib/emitter.js +0 -95
- package/src/lib/helper.js +0 -207
- package/src/resolver/base.js +0 -282
- package/src/resolver/client.js +0 -1013
- package/src/resolver/model.js +0 -159
|
@@ -1,1770 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const assert = require('assert');
|
|
4
|
-
const debug = require('../../lib/debug');
|
|
5
|
-
const CombinatorBase = require('../common/combinator');
|
|
6
|
-
const Emitter = require('../../lib/emitter');
|
|
7
|
-
const PackageInfo = require('./package_info');
|
|
8
|
-
|
|
9
|
-
const {
|
|
10
|
-
Symbol,
|
|
11
|
-
} = require('../common/enum');
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
AnnotationItem,
|
|
15
|
-
ConstructItem,
|
|
16
|
-
FuncItem,
|
|
17
|
-
PropItem,
|
|
18
|
-
|
|
19
|
-
GrammerVar,
|
|
20
|
-
GrammerCall,
|
|
21
|
-
GrammerCatch,
|
|
22
|
-
GrammerValue,
|
|
23
|
-
BehaviorToMap,
|
|
24
|
-
} = require('../common/items');
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
_isBasicType,
|
|
28
|
-
_upperFirst,
|
|
29
|
-
_config,
|
|
30
|
-
_convertStaticParam,
|
|
31
|
-
_toSnakeCase,
|
|
32
|
-
_toCamelCase,
|
|
33
|
-
_avoidKeywords,
|
|
34
|
-
_avoidFuncKeywords,
|
|
35
|
-
_avoidClassKeywords,
|
|
36
|
-
_avoidVarKeywords,
|
|
37
|
-
_exception,
|
|
38
|
-
_symbol,
|
|
39
|
-
_deepClone,
|
|
40
|
-
_isSnakeCase
|
|
41
|
-
} = require('../../lib/helper');
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
function _name(name) {
|
|
45
|
-
return _avoidKeywords(_toSnakeCase(name));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function _varName(name){
|
|
49
|
-
return _avoidVarKeywords(_toSnakeCase(name));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function _funcName(name){
|
|
53
|
-
return _avoidFuncKeywords(_toSnakeCase(name));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function _type(type) {
|
|
57
|
-
const config = _config();
|
|
58
|
-
let t = type instanceof Object ? type.lexeme : type;
|
|
59
|
-
if (t) {
|
|
60
|
-
if (config.typeMap[t]) {
|
|
61
|
-
return config.typeMap[t];
|
|
62
|
-
}
|
|
63
|
-
if (t.indexOf('map[') === 0) {
|
|
64
|
-
return 'dict';
|
|
65
|
-
}
|
|
66
|
-
if (!_isBasicType(t)) {
|
|
67
|
-
return t;
|
|
68
|
-
}
|
|
69
|
-
if (t[0] === '$') {
|
|
70
|
-
t = t.replace('$', 'Tea');
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return t;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
class Combinator extends CombinatorBase {
|
|
77
|
-
constructor(config, imports) {
|
|
78
|
-
super(config, imports);
|
|
79
|
-
this.eol = '';
|
|
80
|
-
this.clientMap = {};
|
|
81
|
-
if (this.config.modelDirName) {
|
|
82
|
-
this.config.model.dir = this.config.modelDirName;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Darafile: name (Tea Package name)
|
|
86
|
-
if (!_isSnakeCase(this.config.package)) {
|
|
87
|
-
debug.stack('python package name is must be snake case, Example: alibabacloud_tea.');
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
addInclude(className, fromPackage) {
|
|
92
|
-
let importName = '';
|
|
93
|
-
let fromName = '';
|
|
94
|
-
let needAlias = false;
|
|
95
|
-
let alias = '';
|
|
96
|
-
|
|
97
|
-
if (className.indexOf('$') > -1) {
|
|
98
|
-
let coreClassName = this.coreClass(className);
|
|
99
|
-
let coreNamespl = coreClassName.split('.');
|
|
100
|
-
importName = coreNamespl[coreNamespl.length - 1];
|
|
101
|
-
coreNamespl.splice(coreNamespl.length - 1, 1);
|
|
102
|
-
fromName = coreNamespl.join('.');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (this.thirdPackageNamespace[className]) {
|
|
106
|
-
// is third package
|
|
107
|
-
importName = _toCamelCase(this.thirdPackageClient[className]);
|
|
108
|
-
fromName = this.thirdPackageNamespace[className] + '.' + this.thirdPackageClient[className];
|
|
109
|
-
|
|
110
|
-
if (importName === _toCamelCase(this.config.client.defaultName)) {
|
|
111
|
-
needAlias = true;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this.includeList.forEach(item => {
|
|
115
|
-
if (item.alias) {
|
|
116
|
-
if (item.alias === importName) {
|
|
117
|
-
needAlias = true;
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
if (item.import === importName && item.from !== fromName) {
|
|
121
|
-
needAlias = true;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
if (needAlias === true) {
|
|
127
|
-
className = className.replace(/[^a-zA-Z0-9_]/, '');
|
|
128
|
-
// import classname as classname
|
|
129
|
-
if (/^[A-Z]+$/.test(importName[0])) {
|
|
130
|
-
alias = className + importName;
|
|
131
|
-
} else {
|
|
132
|
-
alias = _toSnakeCase(className) + '_' + importName;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (fromPackage) {
|
|
138
|
-
fromName = fromPackage;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
let existResult = this.includeList.some(item => item.import === importName && item.from === fromName);
|
|
142
|
-
|
|
143
|
-
if (!existResult) {
|
|
144
|
-
this.includeList.push({
|
|
145
|
-
'from': fromName,
|
|
146
|
-
'import': importName,
|
|
147
|
-
'alias': alias,
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
let packageName = '';
|
|
152
|
-
if (alias) {
|
|
153
|
-
packageName = alias;
|
|
154
|
-
} else {
|
|
155
|
-
packageName = importName;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
this.clientMap[packageName] = this.thirdPackageClient[className];
|
|
159
|
-
return packageName;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
addModelInclude(modelName) {
|
|
163
|
-
let accessPath = modelName.split('.');
|
|
164
|
-
let importName = '';
|
|
165
|
-
let fromName = '';
|
|
166
|
-
let resultName = '';
|
|
167
|
-
let alias = '';
|
|
168
|
-
|
|
169
|
-
if (modelName.indexOf('$') > -1) {
|
|
170
|
-
let coreModelName = this.coreClass(modelName);
|
|
171
|
-
let coreNamespl = coreModelName.split('.');
|
|
172
|
-
importName = coreNamespl[coreNamespl.length - 1];
|
|
173
|
-
coreNamespl.splice(coreNamespl.length - 1, 1);
|
|
174
|
-
fromName = coreNamespl.join('.');
|
|
175
|
-
resultName = fromName.split('.').join('_') + '_' + importName + '.' + modelName.split('.').join('');
|
|
176
|
-
} else if (accessPath.length > 1 && this.thirdPackageNamespace[accessPath[0]]) {
|
|
177
|
-
// is third package
|
|
178
|
-
importName = 'models';
|
|
179
|
-
fromName = this.thirdPackageNamespace[accessPath[0]];
|
|
180
|
-
const className = accessPath[0].replace(/[^a-zA-Z0-9_]/, '');
|
|
181
|
-
alias = _toSnakeCase(className) + '_models';
|
|
182
|
-
resultName = alias + '.' + accessPath.slice(1).map(item => _upperFirst(item)).join('');
|
|
183
|
-
} else {
|
|
184
|
-
// self model
|
|
185
|
-
fromName = this.config.package;
|
|
186
|
-
importName = 'models';
|
|
187
|
-
if (this.config.emitType === 'model') {
|
|
188
|
-
resultName = modelName.split('.').map(m => _upperFirst(m)).join('');
|
|
189
|
-
return resultName;
|
|
190
|
-
}
|
|
191
|
-
alias = _toSnakeCase(this.config.name) + '_' + importName;
|
|
192
|
-
resultName = alias + '.' + modelName.split('.').map(m => _upperFirst(m)).join('');
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
let existResult = this.includeList.some(item => {
|
|
196
|
-
if (item.import === importName && item.from === fromName) {
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
if (!existResult) {
|
|
202
|
-
this.includeList.push({
|
|
203
|
-
'from': fromName,
|
|
204
|
-
'import': importName,
|
|
205
|
-
'alias': alias,
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
return resultName;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
addTypedefInclude(typeName) {
|
|
212
|
-
let accessPath = typeName.split('.');
|
|
213
|
-
let importName = '';
|
|
214
|
-
let fromName = '';
|
|
215
|
-
let typedefModule = {};
|
|
216
|
-
if (accessPath.length === 2) {
|
|
217
|
-
if (this.importsTypedef[accessPath[0]] && this.importsTypedef[accessPath[0]][accessPath[1]]) {
|
|
218
|
-
typedefModule = this.importsTypedef[accessPath[0]][accessPath[1]];
|
|
219
|
-
}
|
|
220
|
-
} else if (accessPath.length === 1 && this.typedef[accessPath[0]]) {
|
|
221
|
-
typedefModule = this.typedef[accessPath[0]];
|
|
222
|
-
}
|
|
223
|
-
if (typedefModule.import || typedefModule.package) {
|
|
224
|
-
if (typedefModule.import) {
|
|
225
|
-
fromName = typedefModule.import;
|
|
226
|
-
}
|
|
227
|
-
if (typedefModule.type) {
|
|
228
|
-
importName = typedefModule.type;
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
let existResult = this.includeList.some(item => item.import === importName && item.from === fromName);
|
|
233
|
-
if (!existResult) {
|
|
234
|
-
this.includeList.push({
|
|
235
|
-
'from': fromName,
|
|
236
|
-
'import': importName,
|
|
237
|
-
'alias': '',
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
return typedefModule.type || typeName;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
combine(objectArr = []) {
|
|
244
|
-
super.combine(objectArr);
|
|
245
|
-
this.config.dir = this.config.outputDir + '/' + this.config.package + '/';
|
|
246
|
-
const [clientObjectItem] = objectArr.filter(obj => obj.type === 'client');
|
|
247
|
-
this.combineClient(clientObjectItem);
|
|
248
|
-
this.includeList = [];
|
|
249
|
-
const models = objectArr.filter(obj => obj.type === 'model');
|
|
250
|
-
if (models.length > 0) {
|
|
251
|
-
this.combineModel(models);
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
checkSyntax(content, emitter) {
|
|
256
|
-
const includeList = this.includeList;
|
|
257
|
-
includeList.forEach(i => {
|
|
258
|
-
let importName = i.import;
|
|
259
|
-
if (i.alias) {
|
|
260
|
-
importName = i.alias;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const re = new RegExp(importName);
|
|
264
|
-
const ignoreModule = ['sys', 'unicode_literals'];
|
|
265
|
-
if (!re.test(content) && ignoreModule.indexOf(importName) === -1) {
|
|
266
|
-
this.includeList.splice(this.includeList.indexOf(i), 1);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
let contentLine = content.split(emitter.eol);
|
|
271
|
-
contentLine.forEach((l, index) => {
|
|
272
|
-
const symbol = ['+', '-', '=', '*', '/', '%'];
|
|
273
|
-
l = l.replace(/(\s*$)/g, '');
|
|
274
|
-
if (symbol.indexOf(l[l.length - 1]) > -1) {
|
|
275
|
-
contentLine[index] = `${l}\\`;
|
|
276
|
-
}
|
|
277
|
-
});
|
|
278
|
-
return contentLine.join(emitter.eol);
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
combineClient(object) {
|
|
282
|
-
this.config.emitType = 'client';
|
|
283
|
-
this.includeList = object.includeList;
|
|
284
|
-
if (this.config.packageInfo) {
|
|
285
|
-
const packageInfo = new PackageInfo(this.config);
|
|
286
|
-
packageInfo.emit(this.config.packageInfo, this.requirePackage);
|
|
287
|
-
}
|
|
288
|
-
let emitter, outputParts = {
|
|
289
|
-
head: '',
|
|
290
|
-
body: '',
|
|
291
|
-
foot: ''
|
|
292
|
-
};
|
|
293
|
-
// generate __init__.py
|
|
294
|
-
emitter = new Emitter(this.config);
|
|
295
|
-
emitter.config.filename = '__init__';
|
|
296
|
-
emitter.emitln('__version__ = "1.0.0"');
|
|
297
|
-
emitter.save();
|
|
298
|
-
/******************************** emit foot ********************************/
|
|
299
|
-
if (this.config.exec) {
|
|
300
|
-
emitter = new Emitter(this.config);
|
|
301
|
-
this.emitExec(emitter);
|
|
302
|
-
outputParts.foot = emitter.output;
|
|
303
|
-
}
|
|
304
|
-
/******************************** emit body ********************************/
|
|
305
|
-
emitter = new Emitter(this.config);
|
|
306
|
-
|
|
307
|
-
this.emitClass(emitter, object);
|
|
308
|
-
|
|
309
|
-
outputParts.body = this.checkSyntax(emitter.output, emitter);
|
|
310
|
-
/******************************** emit head *******************************/
|
|
311
|
-
emitter = new Emitter(this.config);
|
|
312
|
-
emitter.emitln('# -*- coding: utf-8 -*-');
|
|
313
|
-
if (object.topAnnotation.length > 0) {
|
|
314
|
-
this.emitAnnotations(emitter, object.topAnnotation);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
this.emitInclude(emitter);
|
|
318
|
-
outputParts.head = emitter.output;
|
|
319
|
-
|
|
320
|
-
/***************************** combine output ******************************/
|
|
321
|
-
const config = _deepClone(this.config);
|
|
322
|
-
if (_isSnakeCase(this.config.clientName)) {
|
|
323
|
-
config.filename = this.config.clientName;
|
|
324
|
-
} else {
|
|
325
|
-
debug.stack('python clientName is must be snake case, Example: rpc_client.');
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
this.combineOutputParts(config, outputParts);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
combineModel(models) {
|
|
332
|
-
this.config.emitType = 'model';
|
|
333
|
-
let emitter, outputParts = {
|
|
334
|
-
head: '',
|
|
335
|
-
body: '',
|
|
336
|
-
foot: ''
|
|
337
|
-
};
|
|
338
|
-
let includeSet = [];
|
|
339
|
-
// merge includeList
|
|
340
|
-
models.forEach(object => {
|
|
341
|
-
object.includeList.forEach(include => {
|
|
342
|
-
let key = `${include.import}:${include.from}`;
|
|
343
|
-
if (includeSet.indexOf(key) === -1) {
|
|
344
|
-
this.includeList.push(include);
|
|
345
|
-
includeSet.push(key);
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
object.subObject.forEach(subObject => {
|
|
350
|
-
subObject.includeList.forEach(include => {
|
|
351
|
-
let key = `${include.import}:${include.from}`;
|
|
352
|
-
if (includeSet.indexOf(key) === -1) {
|
|
353
|
-
this.includeList.push(include);
|
|
354
|
-
includeSet.push(key);
|
|
355
|
-
}
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
});
|
|
359
|
-
/******************************** emit body ********************************/
|
|
360
|
-
emitter = new Emitter(this.config);
|
|
361
|
-
models.forEach((object, i) => {
|
|
362
|
-
if (object.subObject && object.subObject.length > 0) {
|
|
363
|
-
object.subObject.forEach((obj, j) => {
|
|
364
|
-
this.emitClass(emitter, obj);
|
|
365
|
-
emitter.emitln().emitln();
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
this.emitClass(emitter, object);
|
|
369
|
-
emitter.emitln().emitln();
|
|
370
|
-
});
|
|
371
|
-
outputParts.body = this.checkSyntax(emitter.output, emitter);
|
|
372
|
-
|
|
373
|
-
/******************************** emit head ********************************/
|
|
374
|
-
emitter = new Emitter(this.config);
|
|
375
|
-
emitter.emitln('# -*- coding: utf-8 -*-');
|
|
376
|
-
for (let i = 0; i < models.length; i++) {
|
|
377
|
-
if (models[0].topAnnotation) {
|
|
378
|
-
this.emitAnnotations(emitter, models[0].topAnnotation);
|
|
379
|
-
break;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
this.emitInclude(emitter);
|
|
384
|
-
outputParts.head = emitter.output;
|
|
385
|
-
|
|
386
|
-
/***************************** combine output ******************************/
|
|
387
|
-
const config = _deepClone(this.config);
|
|
388
|
-
config.layer = '';
|
|
389
|
-
config.filename = 'models';
|
|
390
|
-
this.combineOutputParts(config, outputParts);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
getClassName(name) {
|
|
394
|
-
let className = name;
|
|
395
|
-
if (this.config.emitType === 'client') {
|
|
396
|
-
className = this.config.clientName;
|
|
397
|
-
} else {
|
|
398
|
-
className = name.split('.').map(item => _upperFirst(item)).join('');
|
|
399
|
-
}
|
|
400
|
-
return _toCamelCase(_avoidClassKeywords(className));
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
emitClass(emitter, object) {
|
|
404
|
-
var parent = '';
|
|
405
|
-
let className = this.getClassName(object.name);
|
|
406
|
-
|
|
407
|
-
let tmp = [];
|
|
408
|
-
if (!(object.extends instanceof Array)) {
|
|
409
|
-
object.extends = [object.extends];
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
object.extends.forEach(baseClass => {
|
|
413
|
-
tmp.push(baseClass);
|
|
414
|
-
});
|
|
415
|
-
if (tmp.length > 0) {
|
|
416
|
-
parent = '(' + tmp.join(', ') + ')';
|
|
417
|
-
} else {
|
|
418
|
-
parent = '(object)';
|
|
419
|
-
}
|
|
420
|
-
|
|
421
|
-
emitter.emitln(`class ${className}${parent}:`, this.level);
|
|
422
|
-
|
|
423
|
-
this.levelUp();
|
|
424
|
-
if (object.annotations.length > 0) {
|
|
425
|
-
this.emitAnnotations(emitter, object.annotations);
|
|
426
|
-
}
|
|
427
|
-
const notes = this.resolveNotes(object.body);
|
|
428
|
-
if (Object.keys(notes).length > 0) {
|
|
429
|
-
this.emitNotes(emitter, notes);
|
|
430
|
-
}
|
|
431
|
-
let props = object.body.filter(node => node instanceof PropItem);
|
|
432
|
-
let propItems = object.body.filter(node => node instanceof PropItem || node instanceof AnnotationItem);
|
|
433
|
-
|
|
434
|
-
if (object.body.filter(node => node instanceof ConstructItem).length === 0) {
|
|
435
|
-
object.body.unshift(new ConstructItem());
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
// emit body nodes : PropItem | FuncItem | ConstructItem | AnnotationItem
|
|
439
|
-
object.body.forEach(node => {
|
|
440
|
-
if (node instanceof FuncItem) {
|
|
441
|
-
this.emitFunc(emitter, node);
|
|
442
|
-
} else if (node instanceof ConstructItem) {
|
|
443
|
-
if (this.config.emitType === 'model') {
|
|
444
|
-
this.emitConstruct(emitter, node, propItems, false);
|
|
445
|
-
} else if (this.config.emitType === 'client') {
|
|
446
|
-
if (propItems.length > 0) {
|
|
447
|
-
// emit @type
|
|
448
|
-
propItems.forEach(p => {
|
|
449
|
-
if (p instanceof AnnotationItem) {
|
|
450
|
-
this.emitAnnotation(emitter, p);
|
|
451
|
-
} else if (p instanceof PropItem) {
|
|
452
|
-
const type = this.typeHint(p.type);
|
|
453
|
-
if (type) {
|
|
454
|
-
emitter.emitln(`${_varName(p.name)} = None # type: ${type}`, this.level);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
emitter.emitln();
|
|
459
|
-
}
|
|
460
|
-
this.emitConstruct(emitter, node, [], true);
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
});
|
|
464
|
-
|
|
465
|
-
if (this.config.emitType === 'model') {
|
|
466
|
-
this.emitValidate(emitter, className, props, notes);
|
|
467
|
-
this.emitToMap(emitter, className, props, notes);
|
|
468
|
-
this.emitFromMap(emitter, className, props, notes);
|
|
469
|
-
}
|
|
470
|
-
this.levelDown();
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
emitComplexValidate(emitter, name, fieldType, depth) {
|
|
474
|
-
if (fieldType.objectType) {
|
|
475
|
-
if (fieldType.objectType === 'array') {
|
|
476
|
-
if (depth > 0) {
|
|
477
|
-
emitter.emitln(`for k${depth} in ${name}:`, this.level);
|
|
478
|
-
this.levelUp();
|
|
479
|
-
this.emitComplexValidate(emitter, `k${depth}`, fieldType.itemType, depth + 1);
|
|
480
|
-
this.levelDown();
|
|
481
|
-
} else {
|
|
482
|
-
emitter.emitln(`if self.${_varName(name)}:`, this.level);
|
|
483
|
-
this.levelUp();
|
|
484
|
-
emitter.emitln(`for k in self.${_varName(name)}:`, this.level);
|
|
485
|
-
this.levelUp();
|
|
486
|
-
this.emitComplexValidate(emitter, 'k', fieldType.itemType, depth + 1);
|
|
487
|
-
this.levelDown();
|
|
488
|
-
this.levelDown();
|
|
489
|
-
}
|
|
490
|
-
} else if (fieldType.objectType === 'map') {
|
|
491
|
-
if (depth > 0) {
|
|
492
|
-
emitter.emitln(`for v${depth} in ${name}.values():`, this.level);
|
|
493
|
-
this.levelUp();
|
|
494
|
-
this.emitComplexValidate(emitter, `v${depth}`, fieldType.valType, depth + 1);
|
|
495
|
-
this.levelDown();
|
|
496
|
-
} else {
|
|
497
|
-
emitter.emitln(`if self.${_varName(name)}:`, this.level);
|
|
498
|
-
this.levelUp();
|
|
499
|
-
emitter.emitln(`for v in self.${_varName(name)}.values():`, this.level);
|
|
500
|
-
this.levelUp();
|
|
501
|
-
this.emitComplexValidate(emitter, 'v', fieldType.valType, depth + 1);
|
|
502
|
-
this.levelDown();
|
|
503
|
-
this.levelDown();
|
|
504
|
-
}
|
|
505
|
-
} else if (fieldType.objectType === 'model') {
|
|
506
|
-
emitter.emitln(`if ${name}:`, this.level);
|
|
507
|
-
this.levelUp();
|
|
508
|
-
emitter.emitln(`${name}.validate()`, this.level);
|
|
509
|
-
this.levelDown();
|
|
510
|
-
emitter.needSave = true;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
emitValidate(emitter, className, props, notes) {
|
|
516
|
-
//print validate
|
|
517
|
-
emitter.emitln('');
|
|
518
|
-
emitter.emitln('def validate(self):', this.level);
|
|
519
|
-
this.levelUp();
|
|
520
|
-
let haveValidate = false;
|
|
521
|
-
props.forEach(prop => {
|
|
522
|
-
|
|
523
|
-
let required = prop.notes.filter(item => item.key === 'required');
|
|
524
|
-
let maxLength = prop.notes.filter(item => item.key === 'maxLength');
|
|
525
|
-
let pattern = prop.notes.filter(item => item.key === 'pattern');
|
|
526
|
-
let maximum = prop.notes.filter(item => item.key === 'maximum');
|
|
527
|
-
let minimum = prop.notes.filter(item => item.key === 'minimum');
|
|
528
|
-
|
|
529
|
-
if (required.length > 0) {
|
|
530
|
-
emitter.emitln(
|
|
531
|
-
`self.validate_required(self.${_varName(prop.name)}, '${_varName(prop.name)}')`,
|
|
532
|
-
this.level
|
|
533
|
-
);
|
|
534
|
-
haveValidate = true;
|
|
535
|
-
}
|
|
536
|
-
if (maxLength.length > 0 || pattern.length > 0 || maximum.length > 0 || minimum.length > 0) {
|
|
537
|
-
emitter.emitln(`if self.${_varName(prop.name)} is not None:`, this.level);
|
|
538
|
-
this.levelUp();
|
|
539
|
-
|
|
540
|
-
if (maxLength.length > 0) {
|
|
541
|
-
emitter.emitln(`self.validate_max_length(self.${_varName(prop.name)}, '${_varName(prop.name)}', ${maxLength[0].value})`, this.level);
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
if (pattern.length > 0) {
|
|
545
|
-
emitter.emitln(`self.validate_pattern(self.${_varName(prop.name)}, '${_varName(prop.name)}', '${pattern[0].value}')`, this.level);
|
|
546
|
-
}
|
|
547
|
-
|
|
548
|
-
if (maximum.length > 0) {
|
|
549
|
-
emitter.emitln(`self.validate_maximum(self.${_varName(prop.name)}, '${_varName(prop.name)}', ${maximum[0].value})`, this.level);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
if (minimum.length > 0) {
|
|
553
|
-
emitter.emitln(`self.validate_minimum(self.${_varName(prop.name)}, '${_varName(prop.name)}', ${minimum[0].value})`, this.level);
|
|
554
|
-
}
|
|
555
|
-
this.levelDown();
|
|
556
|
-
haveValidate = true;
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
if (prop.type.objectType === 'array' || prop.type.objectType === 'map') {
|
|
561
|
-
let emt = new Emitter(emitter.config);
|
|
562
|
-
this.emitComplexValidate(emt, prop.name, prop.type, 0);
|
|
563
|
-
if (emt.needSave === true) {
|
|
564
|
-
haveValidate = true;
|
|
565
|
-
emitter.emit(emt.output);
|
|
566
|
-
}
|
|
567
|
-
} else if (prop.type.objectType === 'model') {
|
|
568
|
-
emitter.emitln(`if self.${_varName(prop.name)}:`, this.level);
|
|
569
|
-
this.levelUp();
|
|
570
|
-
emitter.emitln(`self.${_varName(prop.name)}.validate()`, this.level);
|
|
571
|
-
this.levelDown();
|
|
572
|
-
haveValidate = true;
|
|
573
|
-
}
|
|
574
|
-
});
|
|
575
|
-
|
|
576
|
-
if (props.length === 0 || !haveValidate) {
|
|
577
|
-
emitter.emitln('pass', this.level);
|
|
578
|
-
}
|
|
579
|
-
this.levelDown();
|
|
580
|
-
emitter.emitln();
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
emitComplexToMap(emitter, prop, carrier, depth) {
|
|
584
|
-
const name = prop.name;
|
|
585
|
-
const fieldName = prop.fieldName;
|
|
586
|
-
const type = prop.type;
|
|
587
|
-
const parentType = prop.parentType;
|
|
588
|
-
|
|
589
|
-
if (type.objectType === 'array') {
|
|
590
|
-
if (depth > 0) {
|
|
591
|
-
emitter.emitln(`l${depth} = []`, this.level);
|
|
592
|
-
emitter.emitln(`for k${depth} in ${name}:`, this.level);
|
|
593
|
-
this.levelUp();
|
|
594
|
-
const propInfo = {
|
|
595
|
-
name: `k${depth}`,
|
|
596
|
-
fieldName: fieldName,
|
|
597
|
-
type: type.itemType,
|
|
598
|
-
parentType: type.lexeme
|
|
599
|
-
};
|
|
600
|
-
this.emitComplexToMap(emitter, propInfo, `l${depth}`, depth + 1);
|
|
601
|
-
this.levelDown();
|
|
602
|
-
if (parentType === 'array') {
|
|
603
|
-
emitter.emitln(`${carrier}.append(l${depth})`, this.level);
|
|
604
|
-
} else if (parentType === 'map') {
|
|
605
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
606
|
-
emitter.emitln(`${carrier}[k${num}] = l${depth}`, this.level);
|
|
607
|
-
}
|
|
608
|
-
} else {
|
|
609
|
-
emitter.emitln(`result['${fieldName}'] = []`, this.level);
|
|
610
|
-
emitter.emitln(`if self.${_varName(name)} is not None:`, this.level);
|
|
611
|
-
this.levelUp();
|
|
612
|
-
emitter.emitln(`for k in self.${_varName(name)}:`, this.level);
|
|
613
|
-
this.levelUp();
|
|
614
|
-
if (type.itemType.valType || type.itemType.itemType) {
|
|
615
|
-
const propInfo = {
|
|
616
|
-
name: 'k',
|
|
617
|
-
fieldName: fieldName,
|
|
618
|
-
type: type.itemType,
|
|
619
|
-
parentType: type.lexeme
|
|
620
|
-
};
|
|
621
|
-
this.emitComplexToMap(emitter, propInfo, `result['${fieldName}']`, depth + 1);
|
|
622
|
-
} else {
|
|
623
|
-
if (type.itemType.objectType === 'model') {
|
|
624
|
-
emitter.emitln(`result['${fieldName}'].append(k.to_map() if k else None)`, this.level);
|
|
625
|
-
emitter.needSave = true;
|
|
626
|
-
} else {
|
|
627
|
-
emitter.needSave = false;
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
this.levelDown();
|
|
631
|
-
this.levelDown();
|
|
632
|
-
}
|
|
633
|
-
} else if (type.objectType === 'map') {
|
|
634
|
-
if (depth > 0) {
|
|
635
|
-
emitter.emitln(`d${depth} = {}`, this.level);
|
|
636
|
-
emitter.emitln(`for k${depth} ,v${depth} in ${name}.items():`, this.level);
|
|
637
|
-
this.levelUp();
|
|
638
|
-
const propInfo = {
|
|
639
|
-
name: `v${depth}`,
|
|
640
|
-
fieldName: fieldName,
|
|
641
|
-
type: type.valType,
|
|
642
|
-
parentType: type.lexeme
|
|
643
|
-
};
|
|
644
|
-
this.emitComplexToMap(emitter, propInfo, `d${depth}`, depth + 1);
|
|
645
|
-
this.levelDown();
|
|
646
|
-
if (parentType === 'array') {
|
|
647
|
-
emitter.emitln(`${carrier}.append(d${depth})`, this.level);
|
|
648
|
-
} else if (parentType === 'map') {
|
|
649
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
650
|
-
emitter.emitln(`${carrier}[k${num}] = d${depth}`, this.level);
|
|
651
|
-
}
|
|
652
|
-
} else {
|
|
653
|
-
emitter.emitln(`result['${fieldName}'] = {}`, this.level);
|
|
654
|
-
emitter.emitln(`if self.${_varName(name)} is not None:`, this.level);
|
|
655
|
-
this.levelUp();
|
|
656
|
-
emitter.emitln(`for k, v in self.${_varName(name)}.items():`, this.level);
|
|
657
|
-
this.levelUp();
|
|
658
|
-
if (type.valType.valType || type.valType.itemType) {
|
|
659
|
-
const propInfo = {
|
|
660
|
-
name: 'v',
|
|
661
|
-
fieldName: fieldName,
|
|
662
|
-
type: type.valType,
|
|
663
|
-
parentType: type.lexeme
|
|
664
|
-
};
|
|
665
|
-
this.emitComplexToMap(emitter, propInfo, `result['${fieldName}']`, depth + 1);
|
|
666
|
-
} else {
|
|
667
|
-
if (type.valType.objectType === 'model') {
|
|
668
|
-
emitter.emitln(`result['${fieldName}'][k] = v.to_map()`, this.level);
|
|
669
|
-
emitter.needSave = true;
|
|
670
|
-
} else {
|
|
671
|
-
emitter.needSave = false;
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
this.levelDown();
|
|
675
|
-
this.levelDown();
|
|
676
|
-
}
|
|
677
|
-
} else if (type.objectType === 'model' || !this.config.typeMap[type] && !this.thirdPackageNamespace[type]) {
|
|
678
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
679
|
-
if (parentType === 'array') {
|
|
680
|
-
emitter.emitln(`l${num}.append(k${num}.to_map() if k${num} else None)`, this.level);
|
|
681
|
-
} else if (parentType === 'map') {
|
|
682
|
-
emitter.emitln(`d${num}[k${num}] = v${num}.to_map()`, this.level);
|
|
683
|
-
}
|
|
684
|
-
emitter.needSave = true;
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
emitToMap(emitter, className, props, notes) {
|
|
689
|
-
emitter.emitln('def to_map(self):', this.level);
|
|
690
|
-
this.levelUp();
|
|
691
|
-
emitter.emitln(`_map = super(${className}, self).to_map()`, this.level);
|
|
692
|
-
emitter.emitln('if _map is not None:', this.level);
|
|
693
|
-
this.levelUp();
|
|
694
|
-
emitter.emitln('return _map', this.level);
|
|
695
|
-
this.levelDown();
|
|
696
|
-
emitter.emitln();
|
|
697
|
-
emitter.emitln('result = dict()', this.level);
|
|
698
|
-
props.forEach(prop => {
|
|
699
|
-
let noteName = prop.notes.filter(item => item.key === 'name');
|
|
700
|
-
let name = noteName.length > 0 ? noteName[0].value : prop.name;
|
|
701
|
-
if (prop.type.objectType === 'array' || prop.type.objectType === 'map') {
|
|
702
|
-
let emt = new Emitter(emitter.config);
|
|
703
|
-
const propInfo = {
|
|
704
|
-
name: prop.name,
|
|
705
|
-
fieldName: name,
|
|
706
|
-
parentType: prop.type.objectType,
|
|
707
|
-
type: prop.type,
|
|
708
|
-
};
|
|
709
|
-
this.emitComplexToMap(emt, propInfo, null, 0);
|
|
710
|
-
if (emt.needSave === true) {
|
|
711
|
-
emitter.emit(emt.output);
|
|
712
|
-
} else {
|
|
713
|
-
emitter.emitln(`if self.${_varName(prop.name)} is not None:`, this.level);
|
|
714
|
-
this.levelUp();
|
|
715
|
-
emitter.emitln(`result['${name}'] = self.${_varName(prop.name)}`, this.level);
|
|
716
|
-
this.levelDown();
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
} else {
|
|
720
|
-
emitter.emitln(`if self.${_varName(prop.name)} is not None:`, this.level);
|
|
721
|
-
this.levelUp();
|
|
722
|
-
if (prop.type.objectType === 'model') {
|
|
723
|
-
emitter.emitln(`result['${name}'] = self.${_varName(prop.name)}.to_map()`, this.level);
|
|
724
|
-
} else {
|
|
725
|
-
emitter.emitln(`result['${name}'] = self.${_varName(prop.name)}`, this.level);
|
|
726
|
-
}
|
|
727
|
-
this.levelDown();
|
|
728
|
-
}
|
|
729
|
-
});
|
|
730
|
-
|
|
731
|
-
emitter.emitln('return result', this.level);
|
|
732
|
-
this.levelDown();
|
|
733
|
-
emitter.emitln();
|
|
734
|
-
}
|
|
735
|
-
|
|
736
|
-
emitComplexFromMap(emitter, prop, carrier, depth) {
|
|
737
|
-
const name = prop.name;
|
|
738
|
-
const fieldName = prop.fieldName;
|
|
739
|
-
const type = prop.type;
|
|
740
|
-
const parentType = prop.parentType;
|
|
741
|
-
|
|
742
|
-
if (type.objectType === 'array') {
|
|
743
|
-
if (depth > 0) {
|
|
744
|
-
const propInfo = {
|
|
745
|
-
name: `k${depth}`,
|
|
746
|
-
fieldName: fieldName,
|
|
747
|
-
type: type.itemType,
|
|
748
|
-
parentType: type.lexeme
|
|
749
|
-
};
|
|
750
|
-
|
|
751
|
-
emitter.emitln(`l${depth} = []`, this.level);
|
|
752
|
-
emitter.emitln(`for k${depth} in ${name}:`, this.level);
|
|
753
|
-
this.levelUp();
|
|
754
|
-
this.emitComplexFromMap(emitter, propInfo, `l${depth}`, depth + 1);
|
|
755
|
-
this.levelDown();
|
|
756
|
-
if (parentType === 'array') {
|
|
757
|
-
emitter.emitln(`${carrier}.append(l${depth})`, this.level);
|
|
758
|
-
} else if (parentType === 'map') {
|
|
759
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
760
|
-
emitter.emitln(`${carrier}['k${num}'] = l${depth}`, this.level);
|
|
761
|
-
}
|
|
762
|
-
} else {
|
|
763
|
-
emitter.emitln(`self.${_varName(name)} = []`, this.level);
|
|
764
|
-
emitter.emitln(`if m.get('${fieldName}') is not None:`, this.level);
|
|
765
|
-
this.levelUp();
|
|
766
|
-
emitter.emitln(`for k in m.get('${fieldName}'):`, this.level);
|
|
767
|
-
this.levelUp();
|
|
768
|
-
if (type.itemType.valType || type.itemType.itemType) {
|
|
769
|
-
const propInfo = {
|
|
770
|
-
name: 'k',
|
|
771
|
-
fieldName: fieldName,
|
|
772
|
-
type: type.itemType,
|
|
773
|
-
parentType: type.lexeme
|
|
774
|
-
};
|
|
775
|
-
this.emitComplexFromMap(emitter, propInfo, `self.${_varName(name)}`, depth + 1);
|
|
776
|
-
} else {
|
|
777
|
-
if (type.itemType.objectType === 'model') {
|
|
778
|
-
emitter.emitln(`temp_model = ${type.itemType.lexeme}()`, this.level);
|
|
779
|
-
emitter.emitln(`self.${_varName(name)}.append(temp_model.from_map(k))`, this.level);
|
|
780
|
-
emitter.needSave = true;
|
|
781
|
-
} else {
|
|
782
|
-
emitter.needSave = false;
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
this.levelDown();
|
|
786
|
-
this.levelDown();
|
|
787
|
-
}
|
|
788
|
-
} else if (type.objectType === 'map') {
|
|
789
|
-
if (depth > 0) {
|
|
790
|
-
const propInfo = {
|
|
791
|
-
name: `v${depth}`,
|
|
792
|
-
fieldName: fieldName,
|
|
793
|
-
type: type.valType,
|
|
794
|
-
parentType: type.lexeme
|
|
795
|
-
};
|
|
796
|
-
|
|
797
|
-
emitter.emitln(`d${depth} = {}`, this.level);
|
|
798
|
-
emitter.emitln(`for k${depth} ,v${depth} in ${name}.items():`, this.level);
|
|
799
|
-
this.levelUp();
|
|
800
|
-
this.emitComplexFromMap(emitter, propInfo, `d${depth}`, depth + 1);
|
|
801
|
-
this.levelDown();
|
|
802
|
-
if (parentType === 'array') {
|
|
803
|
-
emitter.emitln(`${carrier}.append(d${depth})`, this.level);
|
|
804
|
-
} else if (parentType === 'map') {
|
|
805
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
806
|
-
emitter.emitln(`${carrier}[k${num}] = d${depth}`, this.level);
|
|
807
|
-
}
|
|
808
|
-
} else {
|
|
809
|
-
emitter.emitln(`self.${_varName(name)} = {}`, this.level);
|
|
810
|
-
emitter.emitln(`if m.get('${fieldName}') is not None:`, this.level);
|
|
811
|
-
this.levelUp();
|
|
812
|
-
emitter.emitln(`for k, v in m.get('${fieldName}').items():`, this.level);
|
|
813
|
-
this.levelUp();
|
|
814
|
-
if (type.valType.valType || type.valType.itemType) {
|
|
815
|
-
const propInfo = {
|
|
816
|
-
name: 'v',
|
|
817
|
-
fieldName: fieldName,
|
|
818
|
-
type: type.valType,
|
|
819
|
-
parentType: type.lexeme
|
|
820
|
-
};
|
|
821
|
-
this.emitComplexFromMap(emitter, propInfo, `self.${_varName(name)}`, depth + 1);
|
|
822
|
-
} else {
|
|
823
|
-
if (type.valType.objectType === 'model') {
|
|
824
|
-
emitter.emitln(`temp_model = ${type.valType.lexeme}()`, this.level);
|
|
825
|
-
emitter.emitln(`self.${_varName(name)}[k] = temp_model.from_map(v)`, this.level);
|
|
826
|
-
emitter.needSave = true;
|
|
827
|
-
} else {
|
|
828
|
-
emitter.needSave = false;
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
this.levelDown();
|
|
832
|
-
this.levelDown();
|
|
833
|
-
}
|
|
834
|
-
} else if (type.objectType === 'model') {
|
|
835
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
836
|
-
if (parentType === 'array') {
|
|
837
|
-
emitter.emitln(`temp_model = ${type.lexeme}()`, this.level);
|
|
838
|
-
emitter.emitln(`l${num}.append(temp_model.from_map(${name}))`, this.level);
|
|
839
|
-
} else if (parentType === 'map') {
|
|
840
|
-
emitter.emitln(`temp_model = ${type.lexeme}()`, this.level);
|
|
841
|
-
emitter.emitln(`d${num}[k${num}] = temp_model.from_map(${name})`, this.level);
|
|
842
|
-
}
|
|
843
|
-
emitter.needSave = true;
|
|
844
|
-
} else if (!this.config.typeMap[type] && !this.thirdPackageNamespace[type]) {
|
|
845
|
-
const num = depth - 1 > 0 ? depth - 1 : '';
|
|
846
|
-
if (parentType === 'array') {
|
|
847
|
-
emitter.emitln(`temp_model = ${type}()`, this.level);
|
|
848
|
-
emitter.emitln(`l${num}.append(temp_model.from_map(${name}))`, this.level);
|
|
849
|
-
} else if (parentType === 'map') {
|
|
850
|
-
emitter.emitln(`temp_model = ${type}()`, this.level);
|
|
851
|
-
emitter.emitln(`d${num}[k${num}] = temp_model.from_map(${name})`, this.level);
|
|
852
|
-
}
|
|
853
|
-
emitter.needSave = true;
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
|
-
|
|
857
|
-
emitFromMap(emitter, modelName, props, notes) {
|
|
858
|
-
emitter.emitln('def from_map(self, m=None):', this.level);
|
|
859
|
-
this.levelUp();
|
|
860
|
-
emitter.emitln('m = m or dict()', this.level);
|
|
861
|
-
props.forEach(prop => {
|
|
862
|
-
let noteName = prop.notes.filter(item => item.key === 'name');
|
|
863
|
-
let name = noteName.length > 0 ? noteName[0].value : prop.name;
|
|
864
|
-
|
|
865
|
-
if (prop.type.objectType === 'array' || prop.type.objectType === 'map') {
|
|
866
|
-
let emt = new Emitter(emitter.config);
|
|
867
|
-
const propInfo = {
|
|
868
|
-
name: prop.name,
|
|
869
|
-
fieldName: name,
|
|
870
|
-
parentType: prop.type.objectType,
|
|
871
|
-
type: prop.type,
|
|
872
|
-
};
|
|
873
|
-
this.emitComplexFromMap(emt, propInfo, null, 0);
|
|
874
|
-
if (emt.needSave === true) {
|
|
875
|
-
emitter.emit(emt.output);
|
|
876
|
-
} else {
|
|
877
|
-
emitter.emitln(`if m.get('${name}') is not None:`, this.level);
|
|
878
|
-
this.levelUp();
|
|
879
|
-
emitter.emitln(`self.${_varName(prop.name)} = m.get('${name}')`, this.level);
|
|
880
|
-
this.levelDown();
|
|
881
|
-
}
|
|
882
|
-
} else {
|
|
883
|
-
emitter.emitln(`if m.get('${name}') is not None:`, this.level);
|
|
884
|
-
this.levelUp();
|
|
885
|
-
if (prop.type.objectType === 'model') {
|
|
886
|
-
let type = _type(prop.type);
|
|
887
|
-
emitter.emitln(`temp_model = ${type}()`, this.level);
|
|
888
|
-
emitter.emitln(`self.${_varName(prop.name)} = temp_model.from_map(m['${name}'])`, this.level);
|
|
889
|
-
} else {
|
|
890
|
-
emitter.emitln(`self.${_varName(prop.name)} = m.get('${name}')`, this.level);
|
|
891
|
-
}
|
|
892
|
-
this.levelDown();
|
|
893
|
-
}
|
|
894
|
-
});
|
|
895
|
-
emitter.emitln('return self', this.level);
|
|
896
|
-
this.levelDown();
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
emitNotes(emitter, notes) {}
|
|
900
|
-
|
|
901
|
-
emitConstruct(emitter, construct, props, isModule = false) {
|
|
902
|
-
if (construct.params.length + props.length > 0) {
|
|
903
|
-
let constructParams = [];
|
|
904
|
-
construct.params.forEach(param => {
|
|
905
|
-
if (param.value !== null && param.value !== 'null') {
|
|
906
|
-
constructParams.push(`${_varName(param.key)}=${param.value}`);
|
|
907
|
-
} else {
|
|
908
|
-
constructParams.push(`${_varName(param.key)}`);
|
|
909
|
-
}
|
|
910
|
-
});
|
|
911
|
-
emitter.emit('def __init__(self', this.level);
|
|
912
|
-
|
|
913
|
-
if (constructParams.length > 0) {
|
|
914
|
-
emitter.emit(', ');
|
|
915
|
-
emitter.emit(constructParams.join(', '));
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
if (props.length > 0) {
|
|
919
|
-
let constructProps = [];
|
|
920
|
-
let max_length = 90;
|
|
921
|
-
let curr_length = 0;
|
|
922
|
-
props.forEach((prop) => {
|
|
923
|
-
if (prop instanceof PropItem) {
|
|
924
|
-
let str = ` ${_varName(prop.name)}=None`;
|
|
925
|
-
if(curr_length+str.length>=max_length){
|
|
926
|
-
str = emitter.eol + emitter.indent(this.level + 3) + str;
|
|
927
|
-
curr_length = 0;
|
|
928
|
-
}else{
|
|
929
|
-
curr_length = curr_length + str.length;
|
|
930
|
-
}
|
|
931
|
-
constructProps.push(str);
|
|
932
|
-
}
|
|
933
|
-
});
|
|
934
|
-
emitter.emit(',');
|
|
935
|
-
emitter.emit(constructProps.join(','));
|
|
936
|
-
}
|
|
937
|
-
emitter.emitln('):');
|
|
938
|
-
this.levelUp();
|
|
939
|
-
this.func_self = 'self';
|
|
940
|
-
|
|
941
|
-
} else {
|
|
942
|
-
emitter.emitln('def __init__(self):', this.level);
|
|
943
|
-
this.levelUp();
|
|
944
|
-
}
|
|
945
|
-
if (construct.annotations) {
|
|
946
|
-
this.emitFuncComment(emitter, construct);
|
|
947
|
-
//this.emitAnnotations(emitter, construct.annotations);
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
props.forEach(prop => {
|
|
951
|
-
if (prop instanceof AnnotationItem) {
|
|
952
|
-
this.emitAnnotation(emitter, prop);
|
|
953
|
-
return;
|
|
954
|
-
}
|
|
955
|
-
const description = prop.notes.filter(note => {
|
|
956
|
-
if (note.key === 'description') {
|
|
957
|
-
return note.value;
|
|
958
|
-
}
|
|
959
|
-
});
|
|
960
|
-
if (description.length === 1) {
|
|
961
|
-
const desc = description[0].value.split(emitter.eol);
|
|
962
|
-
desc.forEach(d => {
|
|
963
|
-
emitter.emitln(`# ${d}`, this.level);
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
|
|
967
|
-
const fieldType = this.typeHint(prop.type);
|
|
968
|
-
emitter.emitln(`self.${_varName(prop.name)} = ${_varName(prop.name)} # type: ${fieldType}`, this.level);
|
|
969
|
-
});
|
|
970
|
-
if (construct.body.length > 0) {
|
|
971
|
-
construct.body.forEach(gram => {
|
|
972
|
-
this.grammer(emitter, gram, true, true);
|
|
973
|
-
});
|
|
974
|
-
}
|
|
975
|
-
|
|
976
|
-
if (construct.body.length + props.length <= 0) {
|
|
977
|
-
emitter.emitln('pass', this.level);
|
|
978
|
-
}
|
|
979
|
-
this.levelDown();
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
typeHint(fieldType) {
|
|
983
|
-
const type = _type(fieldType);
|
|
984
|
-
|
|
985
|
-
if (fieldType.objectType) {
|
|
986
|
-
if (fieldType.objectType === 'array') {
|
|
987
|
-
let itemType = this.typeHint(fieldType.itemType);
|
|
988
|
-
return `list[${itemType}]`;
|
|
989
|
-
} else if (fieldType.objectType === 'map') {
|
|
990
|
-
let keyType = this.typeHint(fieldType.keyType);
|
|
991
|
-
let valueType = this.typeHint(fieldType.valType);
|
|
992
|
-
return `dict[${keyType}, ${valueType}]`;
|
|
993
|
-
}
|
|
994
|
-
}
|
|
995
|
-
return type;
|
|
996
|
-
}
|
|
997
|
-
|
|
998
|
-
emitAnnotation(emitter, annotation, level) {
|
|
999
|
-
if (typeof level === 'undefined') {
|
|
1000
|
-
level = this.level;
|
|
1001
|
-
}
|
|
1002
|
-
if (annotation.mode === 'single') {
|
|
1003
|
-
emitter.emitln(`# ${annotation.content}`, level);
|
|
1004
|
-
} else if (annotation.mode === 'multi') {
|
|
1005
|
-
emitter.emitln('"""', level);
|
|
1006
|
-
annotation.content.forEach(c => {
|
|
1007
|
-
emitter.emitln(`${c}`, level);
|
|
1008
|
-
});
|
|
1009
|
-
emitter.emitln('"""', level);
|
|
1010
|
-
} else {
|
|
1011
|
-
debug.stack('Unsupported annotation.mode :' + annotation.mode, annotation);
|
|
1012
|
-
}
|
|
1013
|
-
}
|
|
1014
|
-
|
|
1015
|
-
emitFunc(emitter, func) {
|
|
1016
|
-
emitter.emitln();
|
|
1017
|
-
this.func_static = func.modify.indexOf('STATIC') > -1;
|
|
1018
|
-
this.func_self = this.func_static ? this.getClassName(this.config.clientName) : 'self';
|
|
1019
|
-
if (this.func_static) {
|
|
1020
|
-
emitter.emitln('@staticmethod', this.level);
|
|
1021
|
-
}
|
|
1022
|
-
if (func.params.length > 0) {
|
|
1023
|
-
let selfVar = this.func_static ? '' : 'self, ';
|
|
1024
|
-
emitter.emit(`def ${_funcName(func.name)}(${selfVar}`, this.level);
|
|
1025
|
-
if (func.params.length > 0) {
|
|
1026
|
-
let params = [];
|
|
1027
|
-
func.params.forEach(p => {
|
|
1028
|
-
params.push(`${_varName(p.key)}`);
|
|
1029
|
-
});
|
|
1030
|
-
emitter.emit(params.join(', '));
|
|
1031
|
-
}
|
|
1032
|
-
} else {
|
|
1033
|
-
let selfVar = this.func_static ? '' : 'self';
|
|
1034
|
-
emitter.emit(`def ${_funcName(func.name)}(${selfVar}`, this.level);
|
|
1035
|
-
}
|
|
1036
|
-
emitter.emitln('):');
|
|
1037
|
-
this.levelUp();
|
|
1038
|
-
this.emitFuncComment(emitter, func);
|
|
1039
|
-
func.body.forEach(gram => {
|
|
1040
|
-
this.grammer(emitter, gram);
|
|
1041
|
-
});
|
|
1042
|
-
if (func.body.filter(i => !(i instanceof AnnotationItem)).length === 0) {
|
|
1043
|
-
emitter.emitln('pass', this.level);
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
|
-
this.levelDown();
|
|
1047
|
-
}
|
|
1048
|
-
|
|
1049
|
-
emitFuncComment(emitter, func) {
|
|
1050
|
-
const commentTag = ['@param', '@return'];
|
|
1051
|
-
if (func.annotations.length > 0) {
|
|
1052
|
-
emitter.emitln('"""', this.level);
|
|
1053
|
-
func.annotations.forEach(annotation => {
|
|
1054
|
-
if (annotation.mode === 'multi') {
|
|
1055
|
-
annotation.content.forEach(c => {
|
|
1056
|
-
c = c.replace('*', '').trim('', 'left');
|
|
1057
|
-
let tagIndex = null;
|
|
1058
|
-
c.split(' ').forEach((item, index) => {
|
|
1059
|
-
if (commentTag.indexOf(item) > -1) {
|
|
1060
|
-
tagIndex = index;
|
|
1061
|
-
}
|
|
1062
|
-
});
|
|
1063
|
-
if (tagIndex !== null) {
|
|
1064
|
-
let tmp = c.split(' ');
|
|
1065
|
-
if (tmp[tagIndex] === '@param') {
|
|
1066
|
-
const param = tmp[tagIndex + 1];
|
|
1067
|
-
tmp[tagIndex + 1] = _name(param) + ':';
|
|
1068
|
-
let type = func.params.filter(p => param === p.key)[0];
|
|
1069
|
-
if (type) {
|
|
1070
|
-
type = ['base', 'complex'].indexOf(this.config.type[_type(type.type)]) !== -1 ? _type(type.type) : null;
|
|
1071
|
-
}
|
|
1072
|
-
emitter.emitln();
|
|
1073
|
-
if (type) {
|
|
1074
|
-
let typeTmp = tmp.slice(0, 2);
|
|
1075
|
-
typeTmp[tagIndex] = '@type';
|
|
1076
|
-
typeTmp.push(type);
|
|
1077
|
-
emitter.emitln(typeTmp.join(' '), this.level);
|
|
1078
|
-
}
|
|
1079
|
-
emitter.emitln(tmp.join(' '), this.level);
|
|
1080
|
-
} else if (tmp[tagIndex] === '@return') {
|
|
1081
|
-
tmp[tagIndex] = '@return:';
|
|
1082
|
-
emitter.emitln();
|
|
1083
|
-
|
|
1084
|
-
const rtype = ['base', 'complex'].indexOf(this.config.type[_type(func.return[0])]) !== -1 ? _type(func.return[0]) : null;
|
|
1085
|
-
if (rtype && rtype !== 'None') {
|
|
1086
|
-
emitter.emitln(`@rtype: ${rtype}`, this.level);
|
|
1087
|
-
}
|
|
1088
|
-
emitter.emitln(tmp.join(' '), this.level);
|
|
1089
|
-
}
|
|
1090
|
-
} else {
|
|
1091
|
-
emitter.emitln(`${c}`, this.level);
|
|
1092
|
-
}
|
|
1093
|
-
});
|
|
1094
|
-
} else {
|
|
1095
|
-
emitter.emitln(`${annotation.content}`, this.level);
|
|
1096
|
-
}
|
|
1097
|
-
});
|
|
1098
|
-
emitter.emitln('"""', this.level);
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
|
|
1102
|
-
emitInclude(emitter) {
|
|
1103
|
-
let futureList = this.includeList.filter(node => node.from === '__future__');
|
|
1104
|
-
let importList = this.includeList.filter(node => !node.from && node.import);
|
|
1105
|
-
let aliasList = this.includeList.filter(node => node.from && node.alias);
|
|
1106
|
-
let list = this.includeList.filter(node => node.from && !node.alias && node.from !== '__future__');
|
|
1107
|
-
|
|
1108
|
-
let from = {};
|
|
1109
|
-
let fromList = [];
|
|
1110
|
-
|
|
1111
|
-
list.forEach(item => {
|
|
1112
|
-
if (!from[item.from]) {
|
|
1113
|
-
from[item.from] = [item.import];
|
|
1114
|
-
} else {
|
|
1115
|
-
from[item.from].push(item.import);
|
|
1116
|
-
}
|
|
1117
|
-
});
|
|
1118
|
-
|
|
1119
|
-
Object.keys(from).forEach(key => {
|
|
1120
|
-
fromList.push({
|
|
1121
|
-
from: key,
|
|
1122
|
-
import: from[key]
|
|
1123
|
-
});
|
|
1124
|
-
});
|
|
1125
|
-
|
|
1126
|
-
if (futureList.length) {
|
|
1127
|
-
futureList.forEach(include => {
|
|
1128
|
-
this.emitIncludeRow(emitter, include);
|
|
1129
|
-
});
|
|
1130
|
-
emitter.emitln();
|
|
1131
|
-
}
|
|
1132
|
-
|
|
1133
|
-
if (importList.length) {
|
|
1134
|
-
importList.forEach(include => {
|
|
1135
|
-
this.emitIncludeRow(emitter, include);
|
|
1136
|
-
});
|
|
1137
|
-
emitter.emitln();
|
|
1138
|
-
}
|
|
1139
|
-
|
|
1140
|
-
if (fromList.length) {
|
|
1141
|
-
fromList.forEach(include => {
|
|
1142
|
-
this.emitIncludeRow(emitter, include);
|
|
1143
|
-
});
|
|
1144
|
-
emitter.emitln();
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
if (aliasList.length) {
|
|
1148
|
-
aliasList.forEach(include => {
|
|
1149
|
-
this.emitIncludeRow(emitter, include);
|
|
1150
|
-
});
|
|
1151
|
-
emitter.emitln();
|
|
1152
|
-
}
|
|
1153
|
-
|
|
1154
|
-
if (importList.length || fromList.length || aliasList.length) {
|
|
1155
|
-
emitter.emitln();
|
|
1156
|
-
}
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
emitIncludeRow(emitter, include) {
|
|
1160
|
-
if (include.from) {
|
|
1161
|
-
emitter.emit(`from ${include.from} `, this.level);
|
|
1162
|
-
if (include.import instanceof Array) {
|
|
1163
|
-
emitter.emitln(`import ${include.import.join(', ')}`);
|
|
1164
|
-
} else if (include.alias) {
|
|
1165
|
-
emitter.emitln(`import ${include.import} as ${include.alias}`);
|
|
1166
|
-
} else {
|
|
1167
|
-
emitter.emitln(`import ${include.import}`);
|
|
1168
|
-
}
|
|
1169
|
-
} else if (include.import) {
|
|
1170
|
-
emitter.emitln(`import ${include.import}`, this.level);
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
emitExec(emitter) {
|
|
1175
|
-
this.simpleImport('sys');
|
|
1176
|
-
emitter.emitln();
|
|
1177
|
-
emitter.emitln();
|
|
1178
|
-
emitter.emitln('if __name__ == \'__main__\':');
|
|
1179
|
-
this.levelUp();
|
|
1180
|
-
emitter.emitln(`${this.getClassName(this.config.clientName)}.main(sys.argv[1:])`, this.level);
|
|
1181
|
-
this.levelDown();
|
|
1182
|
-
}
|
|
1183
|
-
|
|
1184
|
-
grammerCall(emitter, gram) {
|
|
1185
|
-
// path : 'parent', 'object', 'object_static', 'call', 'call_static', 'prop', 'prop_static', 'map', 'list'
|
|
1186
|
-
var pre = '';
|
|
1187
|
-
let params = '';
|
|
1188
|
-
if (gram.params.length > 0) {
|
|
1189
|
-
let tmp = [];
|
|
1190
|
-
gram.params.forEach(p => {
|
|
1191
|
-
let emit = new Emitter();
|
|
1192
|
-
if (p.value instanceof BehaviorToMap) {
|
|
1193
|
-
if (gram.path[1].name === 'isUnset') {
|
|
1194
|
-
this.grammer(emit, p.value.grammer, false, false);
|
|
1195
|
-
} else {
|
|
1196
|
-
emit.emit(`${this.addInclude('$Core')}.${this.config.tea.core.toMap}(`);
|
|
1197
|
-
this.grammer(emit, p.value.grammer, false, false);
|
|
1198
|
-
emit.emit(')');
|
|
1199
|
-
}
|
|
1200
|
-
} else {
|
|
1201
|
-
this.grammer(emit, p, false, false);
|
|
1202
|
-
}
|
|
1203
|
-
tmp.push(emit.output);
|
|
1204
|
-
});
|
|
1205
|
-
params = tmp.join(', ');
|
|
1206
|
-
}
|
|
1207
|
-
if (gram.type === 'super') {
|
|
1208
|
-
pre = `super(${this.getClassName(this.config.clientName)}, self).__init__(${params})`;
|
|
1209
|
-
} else {
|
|
1210
|
-
gram.path.forEach((path, i) => {
|
|
1211
|
-
let pathName = path.name;
|
|
1212
|
-
if (typeof pathName === 'string') {
|
|
1213
|
-
if (path.type === 'object') {
|
|
1214
|
-
pathName = path.name.replace('@', 'self._');
|
|
1215
|
-
} else {
|
|
1216
|
-
pathName = path.name.replace('@', '_');
|
|
1217
|
-
}
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
if (path.type === 'parent' || path.type === 'parent_async') {
|
|
1221
|
-
pre += this.func_self;
|
|
1222
|
-
if (path.name) {
|
|
1223
|
-
pre += `.${_name(path.name)}`;
|
|
1224
|
-
}
|
|
1225
|
-
} else if (path.type === 'object' || path.type === 'object_async') {
|
|
1226
|
-
pre += `${_varName(_convertStaticParam(pathName))}`;
|
|
1227
|
-
} else if (path.type === 'object_static' || path.type === 'object_static_async') {
|
|
1228
|
-
pre += `${_convertStaticParam(pathName)}`;
|
|
1229
|
-
} else if (path.type === 'call' || path.type === 'call_static') {
|
|
1230
|
-
pre += `.${_funcName(pathName)}(${params})`;
|
|
1231
|
-
} else if (path.type === 'call_async' || path.type === 'call_static_async') {
|
|
1232
|
-
pre += `.${_funcName(pathName)}(${params})`;
|
|
1233
|
-
} else if (path.type === 'prop') {
|
|
1234
|
-
pre += `.${_name(pathName)}`;
|
|
1235
|
-
} else if (path.type === 'prop_static') {
|
|
1236
|
-
pre += `.${_name(pathName)}`;
|
|
1237
|
-
} else if (path.type === 'map') {
|
|
1238
|
-
pre += path.isVar ? `.get(${_varName(pathName)})` : `.get('${pathName}')`;
|
|
1239
|
-
} else if (path.type === 'map_set') {
|
|
1240
|
-
const quote = this._adaptedQuotes(pathName, emitter);
|
|
1241
|
-
pre += `[${quote}${pathName}${quote}]`;
|
|
1242
|
-
} else if (path.type === 'list') {
|
|
1243
|
-
pre += path.isVar ? `[${_varName(pathName)}]` : `[${pathName}]`;
|
|
1244
|
-
} else {
|
|
1245
|
-
debug.stack(gram);
|
|
1246
|
-
}
|
|
1247
|
-
});
|
|
1248
|
-
}
|
|
1249
|
-
if (pre[0] === '.') {
|
|
1250
|
-
pre = pre.slice(1);
|
|
1251
|
-
}
|
|
1252
|
-
|
|
1253
|
-
emitter.emit(pre);
|
|
1254
|
-
}
|
|
1255
|
-
|
|
1256
|
-
simpleImport(imp, from = null) {
|
|
1257
|
-
let existResult = this.includeList.some(item => {
|
|
1258
|
-
if (item.import === imp && item.from === from) {
|
|
1259
|
-
return true;
|
|
1260
|
-
}
|
|
1261
|
-
});
|
|
1262
|
-
if (!existResult) {
|
|
1263
|
-
this.includeList.push({
|
|
1264
|
-
from: from,
|
|
1265
|
-
import: imp
|
|
1266
|
-
});
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
grammerExpr(emitter, gram) {
|
|
1271
|
-
if (!gram.left && !gram.right) {
|
|
1272
|
-
emitter.emit(` ${_symbol(gram.opt)} `);
|
|
1273
|
-
return;
|
|
1274
|
-
}
|
|
1275
|
-
this.grammer(emitter, gram.left, false, false);
|
|
1276
|
-
emitter.emit(` ${_symbol(gram.opt)} `);
|
|
1277
|
-
this.grammer(emitter, gram.right, false, false);
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
grammerVar(emitter, gram) {
|
|
1281
|
-
if (gram.varType === 'static_class') {
|
|
1282
|
-
const name = gram.name ? gram.name : gram.key;
|
|
1283
|
-
emitter.emit(`${name}()`);
|
|
1284
|
-
} else if (gram.varType === 'var' || gram.varType === 'const') {
|
|
1285
|
-
const name = gram.name ? gram.name : gram.key;
|
|
1286
|
-
emitter.emit(`${_convertStaticParam(_varName(name))}`);
|
|
1287
|
-
} else {
|
|
1288
|
-
debug.stack(gram);
|
|
1289
|
-
}
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
|
-
grammerValue(emitter, gram, layer = 1, isparams = false) {
|
|
1293
|
-
if (gram.key) {
|
|
1294
|
-
if (!isparams) {
|
|
1295
|
-
const quote = this._adaptedQuotes(gram.key, emitter);
|
|
1296
|
-
emitter.emit(`${quote}${gram.key}${quote}: `);
|
|
1297
|
-
} else {
|
|
1298
|
-
emitter.emit(`${_varName(gram.key)}=`, this.level);
|
|
1299
|
-
}
|
|
1300
|
-
}
|
|
1301
|
-
if (gram instanceof GrammerCall) {
|
|
1302
|
-
this.grammerCall(emitter, gram);
|
|
1303
|
-
} else if (gram.type === 'array') {
|
|
1304
|
-
if (gram.needCast) {
|
|
1305
|
-
if (gram.value.length > 0) {
|
|
1306
|
-
emitter.emit(`${this.addInclude('$Core')}.${this.config.tea.core.merge}(`);
|
|
1307
|
-
let expandParams = gram.value.filter((item) => {
|
|
1308
|
-
return item.isExpand !== true;
|
|
1309
|
-
});
|
|
1310
|
-
let notExpandParams = gram.value.filter((item) => {
|
|
1311
|
-
return item.isExpand === true;
|
|
1312
|
-
});
|
|
1313
|
-
if (expandParams.length > 0) {
|
|
1314
|
-
emitter.emitln('{');
|
|
1315
|
-
for (let i = 0; i < expandParams.length; i++) {
|
|
1316
|
-
emitter.emit('', this.level + layer);
|
|
1317
|
-
let v = expandParams[i];
|
|
1318
|
-
if (v instanceof AnnotationItem) {
|
|
1319
|
-
this.emitAnnotation(emitter, v, 0);
|
|
1320
|
-
continue;
|
|
1321
|
-
}
|
|
1322
|
-
this.grammerValue(emitter, v, layer + 1);
|
|
1323
|
-
if (i < expandParams.length - 1) {
|
|
1324
|
-
emitter.emitln(',');
|
|
1325
|
-
} else {
|
|
1326
|
-
emitter.emitln('');
|
|
1327
|
-
}
|
|
1328
|
-
}
|
|
1329
|
-
emitter.emit('}', this.level + layer - 1);
|
|
1330
|
-
}
|
|
1331
|
-
if (notExpandParams.length > 0) {
|
|
1332
|
-
if (expandParams.length > 0) {
|
|
1333
|
-
emitter.emit(', ');
|
|
1334
|
-
}
|
|
1335
|
-
for (let i = 0; i < notExpandParams.length; i++) {
|
|
1336
|
-
let v = notExpandParams[i];
|
|
1337
|
-
if (v instanceof AnnotationItem) {
|
|
1338
|
-
this.emitAnnotation(emitter, v, 0);
|
|
1339
|
-
continue;
|
|
1340
|
-
}
|
|
1341
|
-
this.grammerValue(emitter, v, layer + 1);
|
|
1342
|
-
if (i < notExpandParams.length - 1) {
|
|
1343
|
-
emitter.emitln(',');
|
|
1344
|
-
emitter.emit('', this.level + layer);
|
|
1345
|
-
}
|
|
1346
|
-
}
|
|
1347
|
-
}
|
|
1348
|
-
emitter.emit(')');
|
|
1349
|
-
} else {
|
|
1350
|
-
emitter.emit('{}');
|
|
1351
|
-
}
|
|
1352
|
-
} else {
|
|
1353
|
-
if (gram.value.length > 0) {
|
|
1354
|
-
const tmp = gram.value.filter(item => !(item instanceof AnnotationItem));
|
|
1355
|
-
const isMap = tmp[0] && tmp[0].key && tmp[0].key !== '';
|
|
1356
|
-
if (isMap) {
|
|
1357
|
-
emitter.emitln('{');
|
|
1358
|
-
} else {
|
|
1359
|
-
emitter.emitln('[');
|
|
1360
|
-
}
|
|
1361
|
-
let len = gram.value.length;
|
|
1362
|
-
let i = 0;
|
|
1363
|
-
for (i = 0; i < gram.value.length; i++) {
|
|
1364
|
-
let item = gram.value[i];
|
|
1365
|
-
emitter.emit('', this.level + layer);
|
|
1366
|
-
if (item instanceof AnnotationItem) {
|
|
1367
|
-
this.emitAnnotation(emitter, item, 0);
|
|
1368
|
-
continue;
|
|
1369
|
-
}
|
|
1370
|
-
this.grammerValue(emitter, item, layer + 1);
|
|
1371
|
-
if (i < len - 1) {
|
|
1372
|
-
emitter.emitln(',');
|
|
1373
|
-
} else {
|
|
1374
|
-
emitter.emitln('');
|
|
1375
|
-
}
|
|
1376
|
-
}
|
|
1377
|
-
if (isMap) {
|
|
1378
|
-
emitter.emit('}', this.level + layer - 1);
|
|
1379
|
-
} else {
|
|
1380
|
-
emitter.emit(']', this.level + layer - 1);
|
|
1381
|
-
}
|
|
1382
|
-
|
|
1383
|
-
} else {
|
|
1384
|
-
emitter.emit('{}');
|
|
1385
|
-
}
|
|
1386
|
-
}
|
|
1387
|
-
} else if (gram.type === 'model_construct_params') {
|
|
1388
|
-
if (gram.value.length > 0) {
|
|
1389
|
-
let tmp = [];
|
|
1390
|
-
emitter.emitln();
|
|
1391
|
-
this.levelUp();
|
|
1392
|
-
gram.value.forEach(item => {
|
|
1393
|
-
let emit = new Emitter();
|
|
1394
|
-
if (item instanceof AnnotationItem) {
|
|
1395
|
-
if (item.mode === 'single') {
|
|
1396
|
-
emit.emit(`# ${item.content}`, this.level);
|
|
1397
|
-
}
|
|
1398
|
-
tmp.push(emit.output);
|
|
1399
|
-
return true;
|
|
1400
|
-
}
|
|
1401
|
-
this.grammerValue(emit, item, 1, true);
|
|
1402
|
-
tmp.push(emit.output);
|
|
1403
|
-
});
|
|
1404
|
-
this.levelDown();
|
|
1405
|
-
emitter.emit(tmp.join(',' + emitter.eol));
|
|
1406
|
-
emitter.emitln();
|
|
1407
|
-
emitter.emit('', this.level);
|
|
1408
|
-
}
|
|
1409
|
-
} else if (gram.type === 'string') {
|
|
1410
|
-
this.simpleImport('unicode_literals', '__future__');
|
|
1411
|
-
const quote = this._adaptedQuotes(gram.value, emitter);
|
|
1412
|
-
emitter.emit(`${quote}${gram.value}${quote}`);
|
|
1413
|
-
} else if (gram.type === 'param') {
|
|
1414
|
-
emitter.emit(`${_convertStaticParam(_varName(gram.value))}`);
|
|
1415
|
-
} else if (gram.type === 'call') {
|
|
1416
|
-
this.grammerCall(emitter, gram.value);
|
|
1417
|
-
} else if (gram.type === 'number') {
|
|
1418
|
-
emitter.emit(gram.value);
|
|
1419
|
-
} else if (gram.type === 'null') {
|
|
1420
|
-
emitter.emit('None');
|
|
1421
|
-
} else if (gram.type === 'behavior') {
|
|
1422
|
-
this.grammer(emitter, gram.value);
|
|
1423
|
-
} else if (gram.type === 'expr') {
|
|
1424
|
-
if (Array.isArray(gram.value)) {
|
|
1425
|
-
const isConcatString = gram.value.some(item => {
|
|
1426
|
-
return item.opt === 'CONCAT';
|
|
1427
|
-
});
|
|
1428
|
-
if (isConcatString) {
|
|
1429
|
-
gram.value.forEach(gramItem => {
|
|
1430
|
-
const needTanslate = gramItem.opt !== 'CONCAT' && gramItem.type !== 'string';
|
|
1431
|
-
if (needTanslate) {
|
|
1432
|
-
emitter.emit(`${this.addInclude('$Converter')}.${this.config.tea.converter.toUnicode}(`);
|
|
1433
|
-
this.grammer(emitter, gramItem, false, false);
|
|
1434
|
-
emitter.emit(')');
|
|
1435
|
-
} else {
|
|
1436
|
-
this.grammer(emitter, gramItem, false, false);
|
|
1437
|
-
}
|
|
1438
|
-
});
|
|
1439
|
-
} else {
|
|
1440
|
-
gram.value.forEach(gramItem => {
|
|
1441
|
-
this.grammer(emitter, gramItem, false, false);
|
|
1442
|
-
});
|
|
1443
|
-
}
|
|
1444
|
-
} else {
|
|
1445
|
-
this.grammer(emitter, gram.value, false, false);
|
|
1446
|
-
}
|
|
1447
|
-
} else if (gram.type === 'instance' || gram.type === 'module_instance') {
|
|
1448
|
-
this.grammerNewObject(emitter, gram.value);
|
|
1449
|
-
} else if (gram.type === 'bool' || gram.type === 'boolean') {
|
|
1450
|
-
emitter.emit(gram.value ? 'True' : 'False');
|
|
1451
|
-
} else if (gram.type === 'var') {
|
|
1452
|
-
this.grammerVar(emitter, gram.value);
|
|
1453
|
-
} else if (gram.type === 'class') {
|
|
1454
|
-
emitter.emit(`${gram.value.name}`);
|
|
1455
|
-
} else if (gram.type === 'not') {
|
|
1456
|
-
emitter.emit(_symbol(Symbol.reverse()));
|
|
1457
|
-
this.grammerValue(emitter, gram.value);
|
|
1458
|
-
} else if (gram.type === '') {
|
|
1459
|
-
if (gram.varType) {
|
|
1460
|
-
this.grammerVar(emitter, gram);
|
|
1461
|
-
} else {
|
|
1462
|
-
debug.stack('Unsupported GrammerValue type', gram);
|
|
1463
|
-
}
|
|
1464
|
-
} else if (Array.isArray(gram)) {
|
|
1465
|
-
let grammerValue = new GrammerValue();
|
|
1466
|
-
grammerValue.type = 'array';
|
|
1467
|
-
grammerValue.value = gram;
|
|
1468
|
-
this.grammerValue(emitter, grammerValue);
|
|
1469
|
-
} else {
|
|
1470
|
-
debug.stack('Unsupported GrammerValue type', gram);
|
|
1471
|
-
}
|
|
1472
|
-
}
|
|
1473
|
-
|
|
1474
|
-
grammerLoop(emitter, gram) {
|
|
1475
|
-
if (gram.type === 'foreach') {
|
|
1476
|
-
emitter.emit('for ');
|
|
1477
|
-
this.grammerVar(emitter, gram.item);
|
|
1478
|
-
emitter.emit(' in ');
|
|
1479
|
-
this.grammer(emitter, gram.source, false, false);
|
|
1480
|
-
emitter.emitln(':');
|
|
1481
|
-
}
|
|
1482
|
-
this.levelUp();
|
|
1483
|
-
gram.body.forEach(node => {
|
|
1484
|
-
this.grammer(emitter, node);
|
|
1485
|
-
});
|
|
1486
|
-
this.levelDown();
|
|
1487
|
-
}
|
|
1488
|
-
|
|
1489
|
-
grammerBreak(emitter, gram) {
|
|
1490
|
-
emitter.emit('break');
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1493
|
-
grammerCondition(emitter, gram) {
|
|
1494
|
-
if (gram.type === 'elseif') {
|
|
1495
|
-
emitter.emit('elif');
|
|
1496
|
-
} else {
|
|
1497
|
-
emitter.emit(`${gram.type}`);
|
|
1498
|
-
}
|
|
1499
|
-
|
|
1500
|
-
if (gram.type !== 'else') {
|
|
1501
|
-
emitter.emit(' ');
|
|
1502
|
-
let emit = new Emitter();
|
|
1503
|
-
gram.conditionBody.forEach(condition => {
|
|
1504
|
-
this.grammer(emit, condition, false, false);
|
|
1505
|
-
});
|
|
1506
|
-
emitter.emit(`${emit.output}`);
|
|
1507
|
-
}
|
|
1508
|
-
|
|
1509
|
-
emitter.emitln(':');
|
|
1510
|
-
this.levelUp();
|
|
1511
|
-
gram.body.forEach(node => {
|
|
1512
|
-
this.grammer(emitter, node);
|
|
1513
|
-
});
|
|
1514
|
-
if (gram.body.filter(i => !(i instanceof AnnotationItem)).length === 0) {
|
|
1515
|
-
emitter.emitln('pass', this.level);
|
|
1516
|
-
}
|
|
1517
|
-
|
|
1518
|
-
this.levelDown();
|
|
1519
|
-
if (gram.elseItem.length && gram.elseItem.length > 0) {
|
|
1520
|
-
gram.elseItem.forEach(e => {
|
|
1521
|
-
this.grammer(emitter, e);
|
|
1522
|
-
});
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
|
|
1526
|
-
grammerReturn(emitter, gram) {
|
|
1527
|
-
if (gram.type === 'null') {
|
|
1528
|
-
emitter.emit('return');
|
|
1529
|
-
return;
|
|
1530
|
-
}
|
|
1531
|
-
emitter.emit('return ');
|
|
1532
|
-
|
|
1533
|
-
if (gram.type === 'grammer') {
|
|
1534
|
-
this.grammer(emitter, gram.expr, false, false);
|
|
1535
|
-
} else if (gram.type === 'string') {
|
|
1536
|
-
emitter.emit('\'\'');
|
|
1537
|
-
} else {
|
|
1538
|
-
this.grammer(emitter, gram.expr, false, false);
|
|
1539
|
-
}
|
|
1540
|
-
}
|
|
1541
|
-
|
|
1542
|
-
grammerContinue(emitter, gram) {
|
|
1543
|
-
emitter.emit('continue');
|
|
1544
|
-
}
|
|
1545
|
-
|
|
1546
|
-
grammerThrows(emitter, gram) {
|
|
1547
|
-
if (gram.exception === null) {
|
|
1548
|
-
emitter.emit('raise ');
|
|
1549
|
-
this.grammerValue(emitter, gram.params[0]);
|
|
1550
|
-
} else {
|
|
1551
|
-
if (gram.params.length > 0) {
|
|
1552
|
-
emitter.emit(`raise ${_exception(gram.exception)}(`);
|
|
1553
|
-
if (gram.params.length === 1) {
|
|
1554
|
-
this.grammerValue(emitter, gram.params[0]);
|
|
1555
|
-
} else {
|
|
1556
|
-
let tmp = [];
|
|
1557
|
-
gram.params.forEach(p => {
|
|
1558
|
-
let emit = new Emitter();
|
|
1559
|
-
this.grammerValue(emit, p);
|
|
1560
|
-
tmp.push(emit.output);
|
|
1561
|
-
});
|
|
1562
|
-
emitter.emit(tmp.join(', '));
|
|
1563
|
-
}
|
|
1564
|
-
emitter.emit(')');
|
|
1565
|
-
} else {
|
|
1566
|
-
let msg = gram.message ? `'${gram.message}'` : '';
|
|
1567
|
-
emitter.emit(`raise ${_exception(gram.exception)}(${msg})`);
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
|
|
1572
|
-
grammerTryCatch(emitter, gram) {
|
|
1573
|
-
emitter.emitln('try:');
|
|
1574
|
-
this.levelUp();
|
|
1575
|
-
gram.body.forEach(node => {
|
|
1576
|
-
this.grammer(emitter, node, true, true);
|
|
1577
|
-
});
|
|
1578
|
-
this.levelDown();
|
|
1579
|
-
gram.catchBody.forEach(node => {
|
|
1580
|
-
assert.equal(true, node instanceof GrammerCatch);
|
|
1581
|
-
this.grammerCatch(emitter, node);
|
|
1582
|
-
});
|
|
1583
|
-
|
|
1584
|
-
if (gram.finallyBody) {
|
|
1585
|
-
this.grammerFinally(emitter, gram.finallyBody);
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
|
|
1589
|
-
grammerFinally(emitter, gram) {
|
|
1590
|
-
emitter.emitln('finally:', this.level);
|
|
1591
|
-
this.levelUp();
|
|
1592
|
-
gram.body.forEach(childGram => {
|
|
1593
|
-
this.grammer(emitter, childGram);
|
|
1594
|
-
});
|
|
1595
|
-
this.levelDown();
|
|
1596
|
-
}
|
|
1597
|
-
|
|
1598
|
-
grammerCatch(emitter, gram) {
|
|
1599
|
-
emitter.emit('except', this.level);
|
|
1600
|
-
if (gram.exceptions.type === 'BASE') {
|
|
1601
|
-
emitter.emit(' Exception as ');
|
|
1602
|
-
this.grammerVar(emitter, gram.exceptions.exceptionVar);
|
|
1603
|
-
} else {
|
|
1604
|
-
emitter.emit(` ${_exception(gram.exceptions.type)} as `);
|
|
1605
|
-
this.grammerVar(emitter, gram.exceptions.exceptionVar);
|
|
1606
|
-
}
|
|
1607
|
-
|
|
1608
|
-
emitter.emitln(':');
|
|
1609
|
-
this.levelUp();
|
|
1610
|
-
gram.body.forEach(childGram => {
|
|
1611
|
-
this.grammer(emitter, childGram);
|
|
1612
|
-
});
|
|
1613
|
-
this.levelDown();
|
|
1614
|
-
}
|
|
1615
|
-
|
|
1616
|
-
grammerNewObject(emitter, gram) {
|
|
1617
|
-
let objectName = gram.name;
|
|
1618
|
-
|
|
1619
|
-
emitter.emit(`${objectName}(`);
|
|
1620
|
-
if (!Array.isArray(gram.params)) {
|
|
1621
|
-
this.grammerValue(emitter, gram.params);
|
|
1622
|
-
} else {
|
|
1623
|
-
if (gram.params.length > 0) {
|
|
1624
|
-
let params = [];
|
|
1625
|
-
gram.params.forEach(p => {
|
|
1626
|
-
let emit = new Emitter();
|
|
1627
|
-
if (p.key) {
|
|
1628
|
-
emit.emit(`${_varName(p.key)}`);
|
|
1629
|
-
emit.emit('=');
|
|
1630
|
-
}
|
|
1631
|
-
if (typeof (p.value) === 'string') {
|
|
1632
|
-
if (p.value) {
|
|
1633
|
-
emit.emit(`${p.value}`);
|
|
1634
|
-
} else if (p.key) {
|
|
1635
|
-
emit.emit('\'\'');
|
|
1636
|
-
}
|
|
1637
|
-
} else {
|
|
1638
|
-
this.grammerValue(emit, p.value);
|
|
1639
|
-
}
|
|
1640
|
-
params.push(emit.output);
|
|
1641
|
-
});
|
|
1642
|
-
emitter.emit(params.join(', '));
|
|
1643
|
-
}
|
|
1644
|
-
}
|
|
1645
|
-
emitter.emit(')');
|
|
1646
|
-
}
|
|
1647
|
-
|
|
1648
|
-
behaviorTimeNow(emitter, behavior) {
|
|
1649
|
-
this.simpleImport('time');
|
|
1650
|
-
emitter.emit('time.time()');
|
|
1651
|
-
}
|
|
1652
|
-
|
|
1653
|
-
behaviorSetMapItem(emitter, behavior) {
|
|
1654
|
-
let emit = new Emitter();
|
|
1655
|
-
this.grammerCall(emit, behavior.call);
|
|
1656
|
-
|
|
1657
|
-
if (behavior.isVar) {
|
|
1658
|
-
emitter.emit(`${emit.output}[${_varName(behavior.key)}] = `, this.level);
|
|
1659
|
-
} else {
|
|
1660
|
-
const quote = this._adaptedQuotes(behavior.key, emitter);
|
|
1661
|
-
emitter.emit(`${emit.output}[${quote}${behavior.key}${quote}] = `, this.level);
|
|
1662
|
-
}
|
|
1663
|
-
|
|
1664
|
-
this.grammerValue(emitter, behavior.value);
|
|
1665
|
-
emitter.emitln('');
|
|
1666
|
-
}
|
|
1667
|
-
|
|
1668
|
-
behaviorDoAction(emitter, behavior) {
|
|
1669
|
-
emitter.emit('', this.level);
|
|
1670
|
-
this.grammerVar(emitter, behavior.var);
|
|
1671
|
-
emitter.emit(` = ${this.addInclude('$Core')}.${this.config.tea.core.doAction}(`);
|
|
1672
|
-
|
|
1673
|
-
let params = [];
|
|
1674
|
-
behavior.params.forEach(p => {
|
|
1675
|
-
let emit = new Emitter();
|
|
1676
|
-
this.grammerValue(emit, p);
|
|
1677
|
-
params.push(emit.output);
|
|
1678
|
-
});
|
|
1679
|
-
emitter.emit(params.join(', '));
|
|
1680
|
-
emitter.emitln(')');
|
|
1681
|
-
behavior.body.forEach(node => {
|
|
1682
|
-
this.grammer(emitter, node);
|
|
1683
|
-
});
|
|
1684
|
-
}
|
|
1685
|
-
|
|
1686
|
-
behaviorRetry(emitter, behavior) {
|
|
1687
|
-
emitter.emitln(`raise TeaException(${this.config.request}, ${this.config.response})`, this.level);
|
|
1688
|
-
}
|
|
1689
|
-
|
|
1690
|
-
behaviorToModel(emitter, behavior) {
|
|
1691
|
-
emitter.emitln(`${this.addInclude('$Core')}.${this.config.tea.core.fromMap}(`);
|
|
1692
|
-
this.levelUp();
|
|
1693
|
-
emitter.emitln(`${this.addModelInclude(behavior.expected)}(),`, this.level);
|
|
1694
|
-
this.grammer(emitter, behavior.grammer);
|
|
1695
|
-
this.levelDown();
|
|
1696
|
-
emitter.emit(')', this.level);
|
|
1697
|
-
}
|
|
1698
|
-
|
|
1699
|
-
behaviorToMap(emitter, behavior) {
|
|
1700
|
-
const grammer = behavior.grammer;
|
|
1701
|
-
if (grammer instanceof GrammerCall) {
|
|
1702
|
-
grammer.path.push({
|
|
1703
|
-
type: 'call',
|
|
1704
|
-
name: 'to_map'
|
|
1705
|
-
});
|
|
1706
|
-
this.grammerCall(emitter, grammer);
|
|
1707
|
-
} else if (grammer instanceof GrammerVar) {
|
|
1708
|
-
const grammerCall = new GrammerCall('method');
|
|
1709
|
-
grammerCall.path.push({
|
|
1710
|
-
type: 'object',
|
|
1711
|
-
name: grammer.name
|
|
1712
|
-
});
|
|
1713
|
-
grammerCall.path.push({
|
|
1714
|
-
type: 'call',
|
|
1715
|
-
name: 'to_map'
|
|
1716
|
-
});
|
|
1717
|
-
this.grammerCall(emitter, grammerCall);
|
|
1718
|
-
} else {
|
|
1719
|
-
debug.stack(grammer);
|
|
1720
|
-
}
|
|
1721
|
-
}
|
|
1722
|
-
|
|
1723
|
-
_adaptedQuotes(str, emit) {
|
|
1724
|
-
const line = str.split(emit.eol);
|
|
1725
|
-
let quote = '\'';
|
|
1726
|
-
if (str.indexOf('\'') !== -1 && str.indexOf('"') !== -1 || line.length > 1) {
|
|
1727
|
-
quote = '\'\'\'';
|
|
1728
|
-
} else if (str.indexOf('\'') !== -1) {
|
|
1729
|
-
quote = '"';
|
|
1730
|
-
}
|
|
1731
|
-
return quote;
|
|
1732
|
-
}
|
|
1733
|
-
|
|
1734
|
-
behaviorStrFormat(emitter, behavior) {
|
|
1735
|
-
let tmp = behavior.tmp.replace(/%/g, '%%').replace(/\${}/g, '%s').replace(/{{/g, '{').replace(/}}/g, '}');
|
|
1736
|
-
const quote = this._adaptedQuotes(tmp, emitter);
|
|
1737
|
-
this.simpleImport('unicode_literals', '__future__');
|
|
1738
|
-
if (behavior.item.length > 1) {
|
|
1739
|
-
emitter.emit(`${quote}${tmp}${quote} % (`);
|
|
1740
|
-
} else if (behavior.item.length === 1) {
|
|
1741
|
-
emitter.emit(`${quote}${tmp}${quote} % `);
|
|
1742
|
-
} else {
|
|
1743
|
-
tmp = tmp.replace(/%%/g, '%');
|
|
1744
|
-
emitter.emit(`${quote}${tmp}${quote}`);
|
|
1745
|
-
}
|
|
1746
|
-
|
|
1747
|
-
behavior.item.forEach((gram, index) => {
|
|
1748
|
-
emitter.emit(`${this.addInclude('$Converter')}.${this.config.tea.converter.toUnicode}(`);
|
|
1749
|
-
this.grammerValue(emitter, gram);
|
|
1750
|
-
emitter.emit(')');
|
|
1751
|
-
if (index + 1 < behavior.item.length) {
|
|
1752
|
-
emitter.emit(', ');
|
|
1753
|
-
}
|
|
1754
|
-
});
|
|
1755
|
-
|
|
1756
|
-
if (behavior.item.length > 1) {
|
|
1757
|
-
emitter.emit(')');
|
|
1758
|
-
}
|
|
1759
|
-
}
|
|
1760
|
-
|
|
1761
|
-
behaviorTypeInstance(emitter, behavior) {
|
|
1762
|
-
|
|
1763
|
-
}
|
|
1764
|
-
|
|
1765
|
-
grammerSymbol(emitter, gram) {
|
|
1766
|
-
emitter.emit(_symbol(gram));
|
|
1767
|
-
}
|
|
1768
|
-
}
|
|
1769
|
-
|
|
1770
|
-
module.exports = Combinator;
|