1/*
2 * Copyright 2014 Google Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// independent from idl_parser, since this code is not needed for most clients
18
19#include <string>
20
21#include "flatbuffers/code_generators.h"
22#include "flatbuffers/flatbuffers.h"
23#include "flatbuffers/idl.h"
24#include "flatbuffers/util.h"
25
26#include <unordered_set>
27
28namespace flatbuffers {
29namespace python {
30
31// Hardcode spaces per indentation.
32const std::string Indent = " ";
33
34class PythonGenerator : public BaseGenerator {
35 public:
36 PythonGenerator(const Parser &parser, const std::string &path,
37 const std::string &file_name)
38 : BaseGenerator(parser, path, file_name, "" /* not used */,
39 "" /* not used */),
40 float_const_gen_("float('nan')", "float('inf')", "float('-inf')") {
41 static const char * const keywords[] = {
42 "False",
43 "None",
44 "True",
45 "and",
46 "as",
47 "assert",
48 "break",
49 "class",
50 "continue",
51 "def",
52 "del",
53 "elif",
54 "else",
55 "except",
56 "finally",
57 "for",
58 "from",
59 "global",
60 "if",
61 "import",
62 "in",
63 "is",
64 "lambda",
65 "nonlocal",
66 "not",
67 "or",
68 "pass",
69 "raise",
70 "return",
71 "try",
72 "while",
73 "with",
74 "yield"
75 };
76 keywords_.insert(std::begin(keywords), std::end(keywords));
77 }
78
79 // Most field accessors need to retrieve and test the field offset first,
80 // this is the prefix code for that.
81 std::string OffsetPrefix(const FieldDef &field) {
82 return "\n" + Indent + Indent +
83 "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
84 "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
85 Indent + Indent + "if o != 0:\n";
86 }
87
88 // Begin a class declaration.
89 void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
90 std::string &code = *code_ptr;
91 code += "class " + NormalizedName(struct_def) + "(object):\n";
92 code += Indent + "__slots__ = ['_tab']";
93 code += "\n\n";
94 }
95
96 // Begin enum code with a class declaration.
97 void BeginEnum(const std::string class_name, std::string *code_ptr) {
98 std::string &code = *code_ptr;
99 code += "class " + class_name + "(object):\n";
100 }
101
102 std::string EscapeKeyword(const std::string &name) const {
103 return keywords_.find(name) == keywords_.end() ? name : name + "_";
104 }
105
106 std::string NormalizedName(const Definition &definition) const {
107 return EscapeKeyword(definition.name);
108 }
109
110 std::string NormalizedName(const EnumVal &ev) const {
111 return EscapeKeyword(ev.name);
112 }
113
114 // A single enum member.
115 void EnumMember(const EnumDef &enum_def, const EnumVal &ev,
116 std::string *code_ptr) {
117 std::string &code = *code_ptr;
118 code += Indent;
119 code += NormalizedName(ev);
120 code += " = ";
121 code += NumToString(ev.value) + "\n";
122 (void)enum_def;
123 }
124
125 // End enum code.
126 void EndEnum(std::string *code_ptr) {
127 std::string &code = *code_ptr;
128 code += "\n";
129 }
130
131 // Initialize a new struct or table from existing data.
132 void NewRootTypeFromBuffer(const StructDef &struct_def,
133 std::string *code_ptr) {
134 std::string &code = *code_ptr;
135
136 code += Indent + "@classmethod\n";
137 code += Indent + "def GetRootAs";
138 code += NormalizedName(struct_def);
139 code += "(cls, buf, offset):";
140 code += "\n";
141 code += Indent + Indent;
142 code += "n = flatbuffers.encode.Get";
143 code += "(flatbuffers.packer.uoffset, buf, offset)\n";
144 code += Indent + Indent + "x = " + NormalizedName(struct_def) + "()\n";
145 code += Indent + Indent + "x.Init(buf, n + offset)\n";
146 code += Indent + Indent + "return x\n";
147 code += "\n";
148 }
149
150 // Initialize an existing object with other data, to avoid an allocation.
151 void InitializeExisting(const StructDef &struct_def,
152 std::string *code_ptr) {
153 std::string &code = *code_ptr;
154
155 GenReceiver(struct_def, code_ptr);
156 code += "Init(self, buf, pos):\n";
157 code += Indent + Indent + "self._tab = flatbuffers.table.Table(buf, pos)\n";
158 code += "\n";
159 }
160
161 // Get the length of a vector.
162 void GetVectorLen(const StructDef &struct_def, const FieldDef &field,
163 std::string *code_ptr) {
164 std::string &code = *code_ptr;
165
166 GenReceiver(struct_def, code_ptr);
167 code += MakeCamel(NormalizedName(field)) + "Length(self";
168 code += "):" + OffsetPrefix(field);
169 code += Indent + Indent + Indent + "return self._tab.VectorLen(o)\n";
170 code += Indent + Indent + "return 0\n\n";
171 }
172
173 // Get the value of a struct's scalar.
174 void GetScalarFieldOfStruct(const StructDef &struct_def,
175 const FieldDef &field,
176 std::string *code_ptr) {
177 std::string &code = *code_ptr;
178 std::string getter = GenGetter(field.value.type);
179 GenReceiver(struct_def, code_ptr);
180 code += MakeCamel(NormalizedName(field));
181 code += "(self): return " + getter;
182 code += "self._tab.Pos + flatbuffers.number_types.UOffsetTFlags.py_type(";
183 code += NumToString(field.value.offset) + "))\n";
184 }
185
186 // Get the value of a table's scalar.
187 void GetScalarFieldOfTable(const StructDef &struct_def,
188 const FieldDef &field,
189 std::string *code_ptr) {
190 std::string &code = *code_ptr;
191 std::string getter = GenGetter(field.value.type);
192 GenReceiver(struct_def, code_ptr);
193 code += MakeCamel(NormalizedName(field));
194 code += "(self):";
195 code += OffsetPrefix(field);
196 getter += "o + self._tab.Pos)";
197 auto is_bool = IsBool(field.value.type.base_type);
198 if (is_bool) {
199 getter = "bool(" + getter + ")";
200 }
201 code += Indent + Indent + Indent + "return " + getter + "\n";
202 std::string default_value;
203 if (is_bool) {
204 default_value = field.value.constant == "0" ? "False" : "True";
205 } else {
206 default_value = IsFloat(field.value.type.base_type)
207 ? float_const_gen_.GenFloatConstant(field)
208 : field.value.constant;
209 }
210 code += Indent + Indent + "return " + default_value + "\n\n";
211 }
212
213 // Get a struct by initializing an existing struct.
214 // Specific to Struct.
215 void GetStructFieldOfStruct(const StructDef &struct_def,
216 const FieldDef &field,
217 std::string *code_ptr) {
218 std::string &code = *code_ptr;
219 GenReceiver(struct_def, code_ptr);
220 code += MakeCamel(NormalizedName(field));
221 code += "(self, obj):\n";
222 code += Indent + Indent + "obj.Init(self._tab.Bytes, self._tab.Pos + ";
223 code += NumToString(field.value.offset) + ")";
224 code += "\n" + Indent + Indent + "return obj\n\n";
225 }
226
227 // Get a struct by initializing an existing struct.
228 // Specific to Table.
229 void GetStructFieldOfTable(const StructDef &struct_def,
230 const FieldDef &field,
231 std::string *code_ptr) {
232 std::string &code = *code_ptr;
233 GenReceiver(struct_def, code_ptr);
234 code += MakeCamel(NormalizedName(field));
235 code += "(self):";
236 code += OffsetPrefix(field);
237 if (field.value.type.struct_def->fixed) {
238 code += Indent + Indent + Indent + "x = o + self._tab.Pos\n";
239 } else {
240 code += Indent + Indent + Indent;
241 code += "x = self._tab.Indirect(o + self._tab.Pos)\n";
242 }
243 code += Indent + Indent + Indent;
244 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
245 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
246 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
247 code += Indent + Indent + Indent + "return obj\n";
248 code += Indent + Indent + "return None\n\n";
249 }
250
251 // Get the value of a string.
252 void GetStringField(const StructDef &struct_def, const FieldDef &field,
253 std::string *code_ptr) {
254 std::string &code = *code_ptr;
255 GenReceiver(struct_def, code_ptr);
256 code += MakeCamel(NormalizedName(field));
257 code += "(self):";
258 code += OffsetPrefix(field);
259 code += Indent + Indent + Indent + "return " + GenGetter(field.value.type);
260 code += "o + self._tab.Pos)\n";
261 code += Indent + Indent + "return None\n\n";
262 }
263
264 // Get the value of a union from an object.
265 void GetUnionField(const StructDef &struct_def, const FieldDef &field,
266 std::string *code_ptr) {
267 std::string &code = *code_ptr;
268 GenReceiver(struct_def, code_ptr);
269 code += MakeCamel(NormalizedName(field)) + "(self):";
270 code += OffsetPrefix(field);
271
272 // TODO(rw): this works and is not the good way to it:
273 bool is_native_table = TypeName(field) == "*flatbuffers.Table";
274 if (is_native_table) {
275 code += Indent + Indent + Indent + "from flatbuffers.table import Table\n";
276 } else {
277 code += Indent + Indent + Indent;
278 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
279 }
280 code += Indent + Indent + Indent + "obj = Table(bytearray(), 0)\n";
281 code += Indent + Indent + Indent + GenGetter(field.value.type);
282 code += "obj, o)\n" + Indent + Indent + Indent + "return obj\n";
283 code += Indent + Indent + "return None\n\n";
284 }
285
286 // Get the value of a vector's struct member.
287 void GetMemberOfVectorOfStruct(const StructDef &struct_def,
288 const FieldDef &field,
289 std::string *code_ptr) {
290 std::string &code = *code_ptr;
291 auto vectortype = field.value.type.VectorType();
292
293 GenReceiver(struct_def, code_ptr);
294 code += MakeCamel(NormalizedName(field));
295 code += "(self, j):" + OffsetPrefix(field);
296 code += Indent + Indent + Indent + "x = self._tab.Vector(o)\n";
297 code += Indent + Indent + Indent;
298 code += "x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * ";
299 code += NumToString(InlineSize(vectortype)) + "\n";
300 if (!(vectortype.struct_def->fixed)) {
301 code += Indent + Indent + Indent + "x = self._tab.Indirect(x)\n";
302 }
303 code += Indent + Indent + Indent;
304 code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
305 code += Indent + Indent + Indent + "obj = " + TypeName(field) + "()\n";
306 code += Indent + Indent + Indent + "obj.Init(self._tab.Bytes, x)\n";
307 code += Indent + Indent + Indent + "return obj\n";
308 code += Indent + Indent + "return None\n\n";
309 }
310
311 // Get the value of a vector's non-struct member. Uses a named return
312 // argument to conveniently set the zero value for the result.
313 void GetMemberOfVectorOfNonStruct(const StructDef &struct_def,
314 const FieldDef &field,
315 std::string *code_ptr) {
316 std::string &code = *code_ptr;
317 auto vectortype = field.value.type.VectorType();
318
319 GenReceiver(struct_def, code_ptr);
320 code += MakeCamel(NormalizedName(field));
321 code += "(self, j):";
322 code += OffsetPrefix(field);
323 code += Indent + Indent + Indent + "a = self._tab.Vector(o)\n";
324 code += Indent + Indent + Indent;
325 code += "return " + GenGetter(field.value.type);
326 code += "a + flatbuffers.number_types.UOffsetTFlags.py_type(j * ";
327 code += NumToString(InlineSize(vectortype)) + "))\n";
328 if (vectortype.base_type == BASE_TYPE_STRING) {
329 code += Indent + Indent + "return \"\"\n";
330 } else {
331 code += Indent + Indent + "return 0\n";
332 }
333 code += "\n";
334 }
335
336 // Returns a non-struct vector as a numpy array. Much faster
337 // than iterating over the vector element by element.
338 void GetVectorOfNonStructAsNumpy(const StructDef &struct_def,
339 const FieldDef &field,
340 std::string *code_ptr) {
341 std::string &code = *code_ptr;
342 auto vectortype = field.value.type.VectorType();
343
344 // Currently, we only support accessing as numpy array if
345 // the vector type is a scalar.
346 if (!(IsScalar(vectortype.base_type))) { return; }
347
348 GenReceiver(struct_def, code_ptr);
349 code += MakeCamel(NormalizedName(field)) + "AsNumpy(self):";
350 code += OffsetPrefix(field);
351
352 code += Indent + Indent + Indent;
353 code += "return ";
354 code += "self._tab.GetVectorAsNumpy(flatbuffers.number_types.";
355 code += MakeCamel(GenTypeGet(field.value.type));
356 code += "Flags, o)\n";
357
358 if (vectortype.base_type == BASE_TYPE_STRING) {
359 code += Indent + Indent + "return \"\"\n";
360 } else {
361 code += Indent + Indent + "return 0\n";
362 }
363 code += "\n";
364 }
365
366 // Begin the creator function signature.
367 void BeginBuilderArgs(const StructDef &struct_def,
368 std::string *code_ptr) {
369 std::string &code = *code_ptr;
370
371 code += "\n";
372 code += "def Create" + NormalizedName(struct_def);
373 code += "(builder";
374 }
375
376 // Recursively generate arguments for a constructor, to deal with nested
377 // structs.
378 void StructBuilderArgs(const StructDef &struct_def,
379 const char *nameprefix, std::string *code_ptr) {
380 for (auto it = struct_def.fields.vec.begin();
381 it != struct_def.fields.vec.end(); ++it) {
382 auto &field = **it;
383 if (IsStruct(field.value.type)) {
384 // Generate arguments for a struct inside a struct. To ensure names
385 // don't clash, and to make it obvious these arguments are constructing
386 // a nested struct, prefix the name with the field name.
387 StructBuilderArgs(*field.value.type.struct_def,
388 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
389 } else {
390 std::string &code = *code_ptr;
391 code += std::string(", ") + nameprefix;
392 code += MakeCamel(NormalizedName(field), false);
393 }
394 }
395 }
396
397 // End the creator function signature.
398 void EndBuilderArgs(std::string *code_ptr) {
399 std::string &code = *code_ptr;
400 code += "):\n";
401 }
402
403 // Recursively generate struct construction statements and instert manual
404 // padding.
405 void StructBuilderBody(const StructDef &struct_def,
406 const char *nameprefix, std::string *code_ptr) {
407 std::string &code = *code_ptr;
408 code += " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
409 code += NumToString(struct_def.bytesize) + ")\n";
410 for (auto it = struct_def.fields.vec.rbegin();
411 it != struct_def.fields.vec.rend(); ++it) {
412 auto &field = **it;
413 if (field.padding)
414 code += " builder.Pad(" + NumToString(field.padding) + ")\n";
415 if (IsStruct(field.value.type)) {
416 StructBuilderBody(*field.value.type.struct_def,
417 (nameprefix + (NormalizedName(field) + "_")).c_str(), code_ptr);
418 } else {
419 code += " builder.Prepend" + GenMethod(field) + "(";
420 code += nameprefix + MakeCamel(NormalizedName(field), false) + ")\n";
421 }
422 }
423 }
424
425 void EndBuilderBody(std::string *code_ptr) {
426 std::string &code = *code_ptr;
427 code += " return builder.Offset()\n";
428 }
429
430 // Get the value of a table's starting offset.
431 void GetStartOfTable(const StructDef &struct_def,
432 std::string *code_ptr) {
433 std::string &code = *code_ptr;
434 code += "def " + NormalizedName(struct_def) + "Start";
435 code += "(builder): ";
436 code += "builder.StartObject(";
437 code += NumToString(struct_def.fields.vec.size());
438 code += ")\n";
439 }
440
441 // Set the value of a table's field.
442 void BuildFieldOfTable(const StructDef &struct_def,
443 const FieldDef &field, const size_t offset,
444 std::string *code_ptr) {
445 std::string &code = *code_ptr;
446 code += "def " + NormalizedName(struct_def) + "Add" + MakeCamel(NormalizedName(field));
447 code += "(builder, ";
448 code += MakeCamel(NormalizedName(field), false);
449 code += "): ";
450 code += "builder.Prepend";
451 code += GenMethod(field) + "Slot(";
452 code += NumToString(offset) + ", ";
453 if (!IsScalar(field.value.type.base_type) && (!struct_def.fixed)) {
454 code += "flatbuffers.number_types.UOffsetTFlags.py_type";
455 code += "(";
456 code += MakeCamel(NormalizedName(field), false) + ")";
457 } else {
458 code += MakeCamel(NormalizedName(field), false);
459 }
460 code += ", ";
461 code += IsFloat(field.value.type.base_type)
462 ? float_const_gen_.GenFloatConstant(field)
463 : field.value.constant;
464 code += ")\n";
465 }
466
467 // Set the value of one of the members of a table's vector.
468 void BuildVectorOfTable(const StructDef &struct_def,
469 const FieldDef &field, std::string *code_ptr) {
470 std::string &code = *code_ptr;
471 code += "def " + NormalizedName(struct_def) + "Start";
472 code += MakeCamel(NormalizedName(field));
473 code += "Vector(builder, numElems): return builder.StartVector(";
474 auto vector_type = field.value.type.VectorType();
475 auto alignment = InlineAlignment(vector_type);
476 auto elem_size = InlineSize(vector_type);
477 code += NumToString(elem_size);
478 code += ", numElems, " + NumToString(alignment);
479 code += ")\n";
480 }
481
482 // Get the offset of the end of a table.
483 void GetEndOffsetOnTable(const StructDef &struct_def,
484 std::string *code_ptr) {
485 std::string &code = *code_ptr;
486 code += "def " + NormalizedName(struct_def) + "End";
487 code += "(builder): ";
488 code += "return builder.EndObject()\n";
489 }
490
491 // Generate the receiver for function signatures.
492 void GenReceiver(const StructDef &struct_def, std::string *code_ptr) {
493 std::string &code = *code_ptr;
494 code += Indent + "# " + NormalizedName(struct_def) + "\n";
495 code += Indent + "def ";
496 }
497
498 // Generate a struct field, conditioned on its child type(s).
499 void GenStructAccessor(const StructDef &struct_def,
500 const FieldDef &field, std::string *code_ptr) {
501 GenComment(field.doc_comment, code_ptr, nullptr, "# ");
502 if (IsScalar(field.value.type.base_type)) {
503 if (struct_def.fixed) {
504 GetScalarFieldOfStruct(struct_def, field, code_ptr);
505 } else {
506 GetScalarFieldOfTable(struct_def, field, code_ptr);
507 }
508 } else {
509 switch (field.value.type.base_type) {
510 case BASE_TYPE_STRUCT:
511 if (struct_def.fixed) {
512 GetStructFieldOfStruct(struct_def, field, code_ptr);
513 } else {
514 GetStructFieldOfTable(struct_def, field, code_ptr);
515 }
516 break;
517 case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
518 case BASE_TYPE_VECTOR: {
519 auto vectortype = field.value.type.VectorType();
520 if (vectortype.base_type == BASE_TYPE_STRUCT) {
521 GetMemberOfVectorOfStruct(struct_def, field, code_ptr);
522 } else {
523 GetMemberOfVectorOfNonStruct(struct_def, field, code_ptr);
524 GetVectorOfNonStructAsNumpy(struct_def, field, code_ptr);
525 }
526 break;
527 }
528 case BASE_TYPE_UNION: GetUnionField(struct_def, field, code_ptr); break;
529 default: FLATBUFFERS_ASSERT(0);
530 }
531 }
532 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
533 GetVectorLen(struct_def, field, code_ptr);
534 }
535 }
536
537 // Generate table constructors, conditioned on its members' types.
538 void GenTableBuilders(const StructDef &struct_def,
539 std::string *code_ptr) {
540 GetStartOfTable(struct_def, code_ptr);
541
542 for (auto it = struct_def.fields.vec.begin();
543 it != struct_def.fields.vec.end(); ++it) {
544 auto &field = **it;
545 if (field.deprecated) continue;
546
547 auto offset = it - struct_def.fields.vec.begin();
548 BuildFieldOfTable(struct_def, field, offset, code_ptr);
549 if (field.value.type.base_type == BASE_TYPE_VECTOR) {
550 BuildVectorOfTable(struct_def, field, code_ptr);
551 }
552 }
553
554 GetEndOffsetOnTable(struct_def, code_ptr);
555 }
556
557 // Generate struct or table methods.
558 void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
559 if (struct_def.generated) return;
560
561 GenComment(struct_def.doc_comment, code_ptr, nullptr, "# ");
562 BeginClass(struct_def, code_ptr);
563 if (!struct_def.fixed) {
564 // Generate a special accessor for the table that has been declared as
565 // the root type.
566 NewRootTypeFromBuffer(struct_def, code_ptr);
567 }
568 // Generate the Init method that sets the field in a pre-existing
569 // accessor object. This is to allow object reuse.
570 InitializeExisting(struct_def, code_ptr);
571 for (auto it = struct_def.fields.vec.begin();
572 it != struct_def.fields.vec.end(); ++it) {
573 auto &field = **it;
574 if (field.deprecated) continue;
575
576 GenStructAccessor(struct_def, field, code_ptr);
577 }
578
579 if (struct_def.fixed) {
580 // create a struct constructor function
581 GenStructBuilder(struct_def, code_ptr);
582 } else {
583 // Create a set of functions that allow table construction.
584 GenTableBuilders(struct_def, code_ptr);
585 }
586 }
587
588 // Generate enum declarations.
589 void GenEnum(const EnumDef &enum_def, std::string *code_ptr) {
590 if (enum_def.generated) return;
591
592 GenComment(enum_def.doc_comment, code_ptr, nullptr, "# ");
593 BeginEnum(NormalizedName(enum_def), code_ptr);
594 for (auto it = enum_def.Vals().begin(); it != enum_def.Vals().end(); ++it) {
595 auto &ev = **it;
596 GenComment(ev.doc_comment, code_ptr, nullptr, "# ");
597 EnumMember(enum_def, ev, code_ptr);
598 }
599 EndEnum(code_ptr);
600 }
601
602 // Returns the function name that is able to read a value of the given type.
603 std::string GenGetter(const Type &type) {
604 switch (type.base_type) {
605 case BASE_TYPE_STRING: return "self._tab.String(";
606 case BASE_TYPE_UNION: return "self._tab.Union(";
607 case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
608 default:
609 return "self._tab.Get(flatbuffers.number_types." +
610 MakeCamel(GenTypeGet(type)) + "Flags, ";
611 }
612 }
613
614 // Returns the method name for use with add/put calls.
615 std::string GenMethod(const FieldDef &field) {
616 return IsScalar(field.value.type.base_type)
617 ? MakeCamel(GenTypeBasic(field.value.type))
618 : (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
619 }
620
621 std::string GenTypeBasic(const Type &type) {
622 static const char *ctypename[] = {
623 // clang-format off
624 #define FLATBUFFERS_TD(ENUM, IDLTYPE, \
625 CTYPE, JTYPE, GTYPE, NTYPE, PTYPE, RTYPE) \
626 #PTYPE,
627 FLATBUFFERS_GEN_TYPES(FLATBUFFERS_TD)
628 #undef FLATBUFFERS_TD
629 // clang-format on
630 };
631 return ctypename[type.base_type];
632 }
633
634 std::string GenTypePointer(const Type &type) {
635 switch (type.base_type) {
636 case BASE_TYPE_STRING: return "string";
637 case BASE_TYPE_VECTOR: return GenTypeGet(type.VectorType());
638 case BASE_TYPE_STRUCT: return type.struct_def->name;
639 case BASE_TYPE_UNION:
640 // fall through
641 default: return "*flatbuffers.Table";
642 }
643 }
644
645 std::string GenTypeGet(const Type &type) {
646 return IsScalar(type.base_type) ? GenTypeBasic(type) : GenTypePointer(type);
647 }
648
649 std::string TypeName(const FieldDef &field) {
650 return GenTypeGet(field.value.type);
651 }
652
653 // Create a struct with a builder and the struct's arguments.
654 void GenStructBuilder(const StructDef &struct_def,
655 std::string *code_ptr) {
656 BeginBuilderArgs(struct_def, code_ptr);
657 StructBuilderArgs(struct_def, "", code_ptr);
658 EndBuilderArgs(code_ptr);
659
660 StructBuilderBody(struct_def, "", code_ptr);
661 EndBuilderBody(code_ptr);
662 }
663
664 bool generate() {
665 if (!generateEnums()) return false;
666 if (!generateStructs()) return false;
667 return true;
668 }
669
670 private:
671 bool generateEnums() {
672 for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
673 ++it) {
674 auto &enum_def = **it;
675 std::string enumcode;
676 GenEnum(enum_def, &enumcode);
677 if (!SaveType(enum_def, enumcode, false)) return false;
678 }
679 return true;
680 }
681
682 bool generateStructs() {
683 for (auto it = parser_.structs_.vec.begin();
684 it != parser_.structs_.vec.end(); ++it) {
685 auto &struct_def = **it;
686 std::string declcode;
687 GenStruct(struct_def, &declcode);
688 if (!SaveType(struct_def, declcode, true)) return false;
689 }
690 return true;
691 }
692
693 // Begin by declaring namespace and imports.
694 void BeginFile(const std::string name_space_name, const bool needs_imports,
695 std::string *code_ptr) {
696 std::string &code = *code_ptr;
697 code = code + "# " + FlatBuffersGeneratedWarning() + "\n\n";
698 code += "# namespace: " + name_space_name + "\n\n";
699 if (needs_imports) { code += "import flatbuffers\n\n"; }
700 }
701
702 // Save out the generated code for a Python Table type.
703 bool SaveType(const Definition &def, const std::string &classcode,
704 bool needs_imports) {
705 if (!classcode.length()) return true;
706
707 std::string namespace_dir = path_;
708 auto &namespaces = def.defined_namespace->components;
709 for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
710 if (it != namespaces.begin()) namespace_dir += kPathSeparator;
711 namespace_dir += *it;
712 std::string init_py_filename = namespace_dir + "/__init__.py";
713 SaveFile(init_py_filename.c_str(), "", false);
714 }
715
716 std::string code = "";
717 BeginFile(LastNamespacePart(*def.defined_namespace), needs_imports, &code);
718 code += classcode;
719 std::string filename =
720 NamespaceDir(*def.defined_namespace) + NormalizedName(def) + ".py";
721 return SaveFile(filename.c_str(), code, false);
722 }
723 private:
724 std::unordered_set<std::string> keywords_;
725 const SimpleFloatConstantGenerator float_const_gen_;
726};
727
728} // namespace python
729
730bool GeneratePython(const Parser &parser, const std::string &path,
731 const std::string &file_name) {
732 python::PythonGenerator generator(parser, path, file_name);
733 return generator.generate();
734}
735
736} // namespace flatbuffers
737