summaryrefslogtreecommitdiff
path: root/src/idl_gen_python.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/idl_gen_python.cpp')
-rw-r--r--src/idl_gen_python.cpp128
1 files changed, 45 insertions, 83 deletions
diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp
index c8db3597..bff0d849 100644
--- a/src/idl_gen_python.cpp
+++ b/src/idl_gen_python.cpp
@@ -17,14 +17,13 @@
// independent from idl_parser, since this code is not needed for most clients
#include <string>
+#include <unordered_set>
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
-#include <unordered_set>
-
namespace flatbuffers {
namespace python {
@@ -39,40 +38,12 @@ class PythonGenerator : public BaseGenerator {
: BaseGenerator(parser, path, file_name, "" /* not used */,
"" /* not used */),
float_const_gen_("float('nan')", "float('inf')", "float('-inf')") {
- static const char * const keywords[] = {
- "False",
- "None",
- "True",
- "and",
- "as",
- "assert",
- "break",
- "class",
- "continue",
- "def",
- "del",
- "elif",
- "else",
- "except",
- "finally",
- "for",
- "from",
- "global",
- "if",
- "import",
- "in",
- "is",
- "lambda",
- "nonlocal",
- "not",
- "or",
- "pass",
- "raise",
- "return",
- "try",
- "while",
- "with",
- "yield"
+ static const char *const keywords[] = {
+ "False", "None", "True", "and", "as", "assert", "break",
+ "class", "continue", "def", "del", "elif", "else", "except",
+ "finally", "for", "from", "global", "if", "import", "in",
+ "is", "lambda", "nonlocal", "not", "or", "pass", "raise",
+ "return", "try", "while", "with", "yield"
};
keywords_.insert(std::begin(keywords), std::end(keywords));
}
@@ -81,9 +52,9 @@ class PythonGenerator : public BaseGenerator {
// this is the prefix code for that.
std::string OffsetPrefix(const FieldDef &field) {
return "\n" + Indent + Indent +
- "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
- "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
- Indent + Indent + "if o != 0:\n";
+ "o = flatbuffers.number_types.UOffsetTFlags.py_type" +
+ "(self._tab.Offset(" + NumToString(field.value.offset) + "))\n" +
+ Indent + Indent + "if o != 0:\n";
}
// Begin a class declaration.
@@ -148,8 +119,7 @@ class PythonGenerator : public BaseGenerator {
}
// Initialize an existing object with other data, to avoid an allocation.
- void InitializeExisting(const StructDef &struct_def,
- std::string *code_ptr) {
+ void InitializeExisting(const StructDef &struct_def, std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -172,8 +142,7 @@ class PythonGenerator : public BaseGenerator {
// Get the value of a struct's scalar.
void GetScalarFieldOfStruct(const StructDef &struct_def,
- const FieldDef &field,
- std::string *code_ptr) {
+ const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
std::string getter = GenGetter(field.value.type);
GenReceiver(struct_def, code_ptr);
@@ -184,8 +153,7 @@ class PythonGenerator : public BaseGenerator {
}
// Get the value of a table's scalar.
- void GetScalarFieldOfTable(const StructDef &struct_def,
- const FieldDef &field,
+ void GetScalarFieldOfTable(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
std::string getter = GenGetter(field.value.type);
@@ -195,9 +163,7 @@ class PythonGenerator : public BaseGenerator {
code += OffsetPrefix(field);
getter += "o + self._tab.Pos)";
auto is_bool = IsBool(field.value.type.base_type);
- if (is_bool) {
- getter = "bool(" + getter + ")";
- }
+ if (is_bool) { getter = "bool(" + getter + ")"; }
code += Indent + Indent + Indent + "return " + getter + "\n";
std::string default_value;
if (is_bool) {
@@ -213,8 +179,7 @@ class PythonGenerator : public BaseGenerator {
// Get a struct by initializing an existing struct.
// Specific to Struct.
void GetStructFieldOfStruct(const StructDef &struct_def,
- const FieldDef &field,
- std::string *code_ptr) {
+ const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
code += MakeCamel(NormalizedName(field));
@@ -250,8 +215,7 @@ class PythonGenerator : public BaseGenerator {
// Get a struct by initializing an existing struct.
// Specific to Table.
- void GetStructFieldOfTable(const StructDef &struct_def,
- const FieldDef &field,
+ void GetStructFieldOfTable(const StructDef &struct_def, const FieldDef &field,
std::string *code_ptr) {
std::string &code = *code_ptr;
GenReceiver(struct_def, code_ptr);
@@ -296,7 +260,8 @@ class PythonGenerator : public BaseGenerator {
// TODO(rw): this works and is not the good way to it:
bool is_native_table = TypeName(field) == "*flatbuffers.Table";
if (is_native_table) {
- code += Indent + Indent + Indent + "from flatbuffers.table import Table\n";
+ code +=
+ Indent + Indent + Indent + "from flatbuffers.table import Table\n";
} else {
code += Indent + Indent + Indent;
code += "from ." + TypeName(field) + " import " + TypeName(field) + "\n";
@@ -309,8 +274,7 @@ class PythonGenerator : public BaseGenerator {
// Get the value of a vector's struct member.
void GetMemberOfVectorOfStruct(const StructDef &struct_def,
- const FieldDef &field,
- std::string *code_ptr) {
+ const FieldDef &field, std::string *code_ptr) {
std::string &code = *code_ptr;
auto vectortype = field.value.type.VectorType();
@@ -388,8 +352,7 @@ class PythonGenerator : public BaseGenerator {
}
// Begin the creator function signature.
- void BeginBuilderArgs(const StructDef &struct_def,
- std::string *code_ptr) {
+ void BeginBuilderArgs(const StructDef &struct_def, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "\n";
@@ -399,10 +362,10 @@ class PythonGenerator : public BaseGenerator {
// Recursively generate arguments for a constructor, to deal with nested
// structs.
- void StructBuilderArgs(const StructDef &struct_def,
- const char *nameprefix, std::string *code_ptr) {
+ void StructBuilderArgs(const StructDef &struct_def, const char *nameprefix,
+ std::string *code_ptr) {
for (auto it = struct_def.fields.vec.begin();
- it != struct_def.fields.vec.end(); ++it) {
+ it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
const auto &field_type = field.value.type;
const auto &type =
@@ -439,7 +402,7 @@ class PythonGenerator : public BaseGenerator {
indent + " builder.Prep(" + NumToString(struct_def.minalign) + ", ";
code += NumToString(struct_def.bytesize) + ")\n";
for (auto it = struct_def.fields.vec.rbegin();
- it != struct_def.fields.vec.rend(); ++it) {
+ it != struct_def.fields.vec.rend(); ++it) {
auto &field = **it;
const auto &field_type = field.value.type;
const auto &type =
@@ -484,8 +447,7 @@ class PythonGenerator : public BaseGenerator {
}
// Get the value of a table's starting offset.
- void GetStartOfTable(const StructDef &struct_def,
- std::string *code_ptr) {
+ void GetStartOfTable(const StructDef &struct_def, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + NormalizedName(struct_def) + "Start";
code += "(builder): ";
@@ -495,11 +457,11 @@ class PythonGenerator : public BaseGenerator {
}
// Set the value of a table's field.
- void BuildFieldOfTable(const StructDef &struct_def,
- const FieldDef &field, const size_t offset,
- std::string *code_ptr) {
+ void BuildFieldOfTable(const StructDef &struct_def, const FieldDef &field,
+ const size_t offset, std::string *code_ptr) {
std::string &code = *code_ptr;
- code += "def " + NormalizedName(struct_def) + "Add" + MakeCamel(NormalizedName(field));
+ code += "def " + NormalizedName(struct_def) + "Add" +
+ MakeCamel(NormalizedName(field));
code += "(builder, ";
code += MakeCamel(NormalizedName(field), false);
code += "): ";
@@ -521,8 +483,8 @@ class PythonGenerator : public BaseGenerator {
}
// Set the value of one of the members of a table's vector.
- void BuildVectorOfTable(const StructDef &struct_def,
- const FieldDef &field, std::string *code_ptr) {
+ void BuildVectorOfTable(const StructDef &struct_def, const FieldDef &field,
+ std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + NormalizedName(struct_def) + "Start";
code += MakeCamel(NormalizedName(field));
@@ -536,8 +498,7 @@ class PythonGenerator : public BaseGenerator {
}
// Get the offset of the end of a table.
- void GetEndOffsetOnTable(const StructDef &struct_def,
- std::string *code_ptr) {
+ void GetEndOffsetOnTable(const StructDef &struct_def, std::string *code_ptr) {
std::string &code = *code_ptr;
code += "def " + NormalizedName(struct_def) + "End";
code += "(builder): ";
@@ -552,8 +513,8 @@ class PythonGenerator : public BaseGenerator {
}
// Generate a struct field, conditioned on its child type(s).
- void GenStructAccessor(const StructDef &struct_def,
- const FieldDef &field, std::string *code_ptr) {
+ void GenStructAccessor(const StructDef &struct_def, const FieldDef &field,
+ std::string *code_ptr) {
GenComment(field.doc_comment, code_ptr, &def_comment, Indent.c_str());
if (IsScalar(field.value.type.base_type)) {
if (struct_def.fixed) {
@@ -572,7 +533,9 @@ class PythonGenerator : public BaseGenerator {
GetStructFieldOfTable(struct_def, field, code_ptr);
}
break;
- case BASE_TYPE_STRING: GetStringField(struct_def, field, code_ptr); break;
+ case BASE_TYPE_STRING:
+ GetStringField(struct_def, field, code_ptr);
+ break;
case BASE_TYPE_VECTOR: {
auto vectortype = field.value.type.VectorType();
if (vectortype.base_type == BASE_TYPE_STRUCT) {
@@ -593,12 +556,11 @@ class PythonGenerator : public BaseGenerator {
}
// Generate table constructors, conditioned on its members' types.
- void GenTableBuilders(const StructDef &struct_def,
- std::string *code_ptr) {
+ void GenTableBuilders(const StructDef &struct_def, std::string *code_ptr) {
GetStartOfTable(struct_def, code_ptr);
for (auto it = struct_def.fields.vec.begin();
- it != struct_def.fields.vec.end(); ++it) {
+ it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -635,7 +597,7 @@ class PythonGenerator : public BaseGenerator {
code += "\", size_prefixed=size_prefixed)\n";
code += "\n";
}
-
+
// Generate struct or table methods.
void GenStruct(const StructDef &struct_def, std::string *code_ptr) {
if (struct_def.generated) return;
@@ -646,7 +608,7 @@ class PythonGenerator : public BaseGenerator {
// Generate a special accessor for the table that has been declared as
// the root type.
NewRootTypeFromBuffer(struct_def, code_ptr);
- if (parser_.file_identifier_.length()){
+ if (parser_.file_identifier_.length()) {
// Generate a special function to test file_identifier
GenHasFileIdentifier(struct_def, code_ptr);
}
@@ -655,7 +617,7 @@ class PythonGenerator : public BaseGenerator {
// accessor object. This is to allow object reuse.
InitializeExisting(struct_def, code_ptr);
for (auto it = struct_def.fields.vec.begin();
- it != struct_def.fields.vec.end(); ++it) {
+ it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
if (field.deprecated) continue;
@@ -693,7 +655,7 @@ class PythonGenerator : public BaseGenerator {
case BASE_TYPE_VECTOR: return GenGetter(type.VectorType());
default:
return "self._tab.Get(flatbuffers.number_types." +
- MakeCamel(GenTypeGet(type)) + "Flags, ";
+ MakeCamel(GenTypeGet(type)) + "Flags, ";
}
}
@@ -738,8 +700,7 @@ class PythonGenerator : public BaseGenerator {
}
// Create a struct with a builder and the struct's arguments.
- void GenStructBuilder(const StructDef &struct_def,
- std::string *code_ptr) {
+ void GenStructBuilder(const StructDef &struct_def, std::string *code_ptr) {
BeginBuilderArgs(struct_def, code_ptr);
StructBuilderArgs(struct_def, "", code_ptr);
EndBuilderArgs(code_ptr);
@@ -807,6 +768,7 @@ class PythonGenerator : public BaseGenerator {
NamespaceDir(*def.defined_namespace) + NormalizedName(def) + ".py";
return SaveFile(filename.c_str(), code, false);
}
+
private:
std::unordered_set<std::string> keywords_;
const SimpleFloatConstantGenerator float_const_gen_;