mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 20:15:34 +00:00
sharing the Flatbuffers warning
This commit is contained in:
@@ -49,18 +49,6 @@ std::string OffsetPrefix(const FieldDef &field) {
|
||||
"))\n" + Indent + Indent + "if o != 0:\n";
|
||||
}
|
||||
|
||||
// Begin by declaring namespace and imports.
|
||||
static void BeginFile(const std::string name_space_name,
|
||||
const bool needs_imports,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code += "# automatically generated, do not modify\n\n";
|
||||
code += "# namespace: " + name_space_name + "\n\n";
|
||||
if (needs_imports) {
|
||||
code += "import flatbuffers\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Begin a class declaration.
|
||||
static void BeginClass(const StructDef &struct_def, std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
@@ -558,37 +546,6 @@ static std::string GenMethod(const FieldDef &field) {
|
||||
: (IsStruct(field.value.type) ? "Struct" : "UOffsetTRelative");
|
||||
}
|
||||
|
||||
|
||||
// Save out the generated code for a Python Table type.
|
||||
static bool SaveType(const Parser &parser, const Definition &def,
|
||||
const std::string &classcode, const std::string &path,
|
||||
bool needs_imports) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string namespace_name;
|
||||
std::string namespace_dir = path;
|
||||
auto &namespaces = parser.namespaces_.back()->components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
if (namespace_name.length()) {
|
||||
namespace_name += ".";
|
||||
namespace_dir += kPathSeparator;
|
||||
}
|
||||
namespace_name = *it;
|
||||
namespace_dir += *it;
|
||||
EnsureDirExists(namespace_dir.c_str());
|
||||
|
||||
std::string init_py_filename = namespace_dir + "/__init__.py";
|
||||
SaveFile(init_py_filename.c_str(), "", false);
|
||||
}
|
||||
|
||||
|
||||
std::string code = "";
|
||||
BeginFile(namespace_name, needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = namespace_dir + kPathSeparator + def.name + ".py";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
|
||||
static std::string GenTypeBasic(const Type &type) {
|
||||
static const char *ctypename[] = {
|
||||
#define FLATBUFFERS_TD(ENUM, IDLTYPE, CTYPE, JTYPE, GTYPE, NTYPE, PTYPE) \
|
||||
@@ -653,7 +610,7 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &enum_def = **it;
|
||||
std::string enumcode;
|
||||
GenEnum(enum_def, &enumcode);
|
||||
if (!SaveType(parser_, enum_def, enumcode, path_, false)) return false;
|
||||
if (!SaveType(enum_def, enumcode, false)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -664,10 +621,49 @@ class PythonGenerator : public BaseGenerator {
|
||||
auto &struct_def = **it;
|
||||
std::string declcode;
|
||||
GenStruct(struct_def, &declcode, parser_.root_struct_def_);
|
||||
if (!SaveType(parser_, struct_def, declcode, path_, true)) return false;
|
||||
if (!SaveType(struct_def, declcode, true)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Begin by declaring namespace and imports.
|
||||
void BeginFile(const std::string name_space_name, const bool needs_imports,
|
||||
std::string *code_ptr) {
|
||||
std::string &code = *code_ptr;
|
||||
code = code + "# " + FlatBuffersGeneratedWarning();
|
||||
code += "# namespace: " + name_space_name + "\n\n";
|
||||
if (needs_imports) {
|
||||
code += "import flatbuffers\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Save out the generated code for a Python Table type.
|
||||
bool SaveType(const Definition &def, const std::string &classcode,
|
||||
bool needs_imports) {
|
||||
if (!classcode.length()) return true;
|
||||
|
||||
std::string namespace_name;
|
||||
std::string namespace_dir = path_;
|
||||
auto &namespaces = parser_.namespaces_.back()->components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
if (namespace_name.length()) {
|
||||
namespace_name += ".";
|
||||
namespace_dir += kPathSeparator;
|
||||
}
|
||||
namespace_name = *it;
|
||||
namespace_dir += *it;
|
||||
EnsureDirExists(namespace_dir.c_str());
|
||||
|
||||
std::string init_py_filename = namespace_dir + "/__init__.py";
|
||||
SaveFile(init_py_filename.c_str(), "", false);
|
||||
}
|
||||
|
||||
std::string code = "";
|
||||
BeginFile(namespace_name, needs_imports, &code);
|
||||
code += classcode;
|
||||
std::string filename = namespace_dir + kPathSeparator + def.name + ".py";
|
||||
return SaveFile(filename.c_str(), code, false);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace python
|
||||
|
||||
Reference in New Issue
Block a user