Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/flatbuffers/idl.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,9 @@ struct IDLOptions {
// Whether to generate numpy helpers.
bool python_gen_numpy;

// Use snake-case for field names in the Python object API.
bool python_fields_snake_case;

bool ts_omit_entrypoint;
ProtoIdGapAction proto_id_gap_action;

Expand Down Expand Up @@ -856,6 +859,7 @@ struct IDLOptions {
python_no_type_prefix_suffix(false),
python_typing(false),
python_gen_numpy(true),
python_fields_snake_case(false),
ts_omit_entrypoint(false),
proto_id_gap_action(ProtoIdGapAction::WARNING),
mini_reflect(IDLOptions::kNone),
Expand Down
4 changes: 4 additions & 0 deletions src/flatc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ const static FlatCOption flatc_options[] = {
{"", "python-decode-obj-api-strings", "",
"Decode bytes to strings for the Python Object API"},
{"", "python-gen-numpy", "", "Whether to generate numpy helpers."},
{"", "python-fields-snake-case", "",
"Generate Python fields using snake_case naming convention."},
{"", "ts-omit-entrypoint", "",
"Omit emission of namespace entrypoint file"},
{"", "file-names-only", "",
Expand Down Expand Up @@ -708,6 +710,8 @@ FlatCOptions FlatCompiler::ParseFromCommandLineArguments(int argc,
} else if (arg == "--no-python-gen-numpy" ||
arg == "--python-gen-numpy=false") {
opts.python_gen_numpy = false;
} else if (arg == "--python-fields-snake-case") {
opts.python_fields_snake_case = true;
} else if (arg == "--ts-omit-entrypoint") {
opts.ts_omit_entrypoint = true;
} else if (arg == "--annotate-sparse-vectors") {
Expand Down
16 changes: 13 additions & 3 deletions src/idl_gen_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,22 @@ typedef std::set<ImportMapEntry> ImportMap;
static const CommentConfig def_comment = {nullptr, "#", nullptr};
static const std::string Indent = " ";

inline Namer::Config PythonNamerConfig(const Namer::Config& input,
const IDLOptions& opts,
const std::string& path) {
auto cfg = WithFlagOptions(input, opts, path);
if (opts.python_fields_snake_case) {
cfg.fields = Case::kSnake;
}
return cfg;
}

class PythonStubGenerator {
public:
PythonStubGenerator(const Parser& parser, const std::string& path,
const Version& version)
: parser_{parser},
namer_{WithFlagOptions(kStubConfig, parser.opts, path),
namer_{PythonNamerConfig(kStubConfig, parser.opts, path),
Keywords(version)},
version_(version) {}

Expand Down Expand Up @@ -698,7 +708,7 @@ class PythonGenerator : public BaseGenerator {
: BaseGenerator(parser, path, file_name, "" /* not used */,
"" /* not used */, "py"),
float_const_gen_("float('nan')", "float('inf')", "float('-inf')"),
namer_(WithFlagOptions(kConfig, parser.opts, path), Keywords(version)) {
namer_(PythonNamerConfig(kConfig, parser.opts, path), Keywords(version)) {
}

// Most field accessors need to retrieve and test the field offset first,
Expand Down Expand Up @@ -1321,7 +1331,7 @@ class PythonGenerator : public BaseGenerator {
} else {
code += IsArray(field_type) ? " " : "";
code += indent + " builder.Prepend" + GenMethod(field) + "(";
code += nameprefix + namer_.Variable(field);
code += nameprefix + namer_.Field(field);
size_t array_cnt = index + (IsArray(field_type) ? 1 : 0);
for (size_t i = 0; in_array && i < array_cnt; i++) {
code += "[_idx" + NumToString(i) + "-1]";
Expand Down