python_generator_helpers.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
20 #define GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
21 
22 #include <cstring>
23 #include <fstream>
24 #include <iostream>
25 #include <vector>
26 
27 #include "src/compiler/config.h"
31 
43 using std::vector;
44 
45 namespace grpc_python_generator {
46 
47 namespace {
48 
49 typedef vector<const Descriptor*> DescriptorVector;
50 typedef vector<std::string> StringVector;
51 
52 static std::string StripModulePrefixes(
53  const std::string& raw_module_name,
54  const std::vector<std::string>& prefixes_to_filter) {
55  for (const auto& prefix : prefixes_to_filter) {
56  if (raw_module_name.rfind(prefix, 0) == 0) {
57  return raw_module_name.substr(prefix.size(),
58  raw_module_name.size() - prefix.size());
59  }
60  }
61  return raw_module_name;
62 }
63 
64 // TODO(https://github.com/protocolbuffers/protobuf/issues/888):
65 // Export `ModuleName` from protobuf's
66 // `src/google/protobuf/compiler/python/python_generator.cc` file.
68  const std::string& import_prefix,
69  const std::vector<std::string>& prefixes_to_filter) {
70  std::string basename = StripProto(filename);
71  basename = StringReplace(basename, "-", "_");
72  basename = StringReplace(basename, "/", ".");
73  return StripModulePrefixes(import_prefix + basename + "_pb2",
74  prefixes_to_filter);
75 }
76 
77 // TODO(https://github.com/protocolbuffers/protobuf/issues/888):
78 // Export `ModuleAlias` from protobuf's
79 // `src/google/protobuf/compiler/python/python_generator.cc` file.
80 std::string ModuleAlias(const std::string& filename,
81  const std::string& import_prefix,
82  const std::vector<std::string>& prefixes_to_filter) {
83  std::string module_name =
84  ModuleName(filename, import_prefix, prefixes_to_filter);
85  // We can't have dots in the module name, so we replace each with _dot_.
86  // But that could lead to a collision between a.b and a_dot_b, so we also
87  // duplicate each underscore.
88  module_name = StringReplace(module_name, "_", "__");
89  module_name = StringReplace(module_name, ".", "_dot_");
90  return module_name;
91 }
92 
93 bool GetModuleAndMessagePath(
95  bool generate_in_pb2_grpc, std::string& import_prefix,
96  const std::vector<std::string>& prefixes_to_filter) {
97  const Descriptor* path_elem_type = type;
98  DescriptorVector message_path;
99  do {
100  message_path.push_back(path_elem_type);
101  path_elem_type = path_elem_type->containing_type();
102  } while (path_elem_type); // implicit nullptr comparison; don't be explicit
103  std::string file_name = type->file()->name();
104  static const int proto_suffix_length = strlen(".proto");
105  if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) &&
106  file_name.find_last_of(".proto") == file_name.size() - 1)) {
107  return false;
108  }
109 
111  if (generator_file_name != file_name || generate_in_pb2_grpc) {
112  module = ModuleAlias(file_name, import_prefix, prefixes_to_filter) + ".";
113  } else {
114  module = "";
115  }
117  for (DescriptorVector::reverse_iterator path_iter = message_path.rbegin();
118  path_iter != message_path.rend(); ++path_iter) {
119  message_type += (*path_iter)->name() + ".";
120  }
121  // no pop_back prior to C++11
122  message_type.resize(message_type.size() - 1);
123  *out = module + message_type;
124  return true;
125 }
126 
127 template <typename DescriptorType>
128 StringVector get_all_comments(const DescriptorType* descriptor) {
129  StringVector comments;
133  &comments);
135  &comments);
136  return comments;
137 }
138 
139 inline void Split(const std::string& s, char delim,
140  std::vector<std::string>* append_to) {
141  if (s.empty()) {
142  // splitting an empty string logically produces a single-element list
143  append_to->emplace_back();
144  } else {
145  auto current = s.begin();
146  while (current < s.end()) {
147  const auto next = std::find(current, s.end(), delim);
148  append_to->emplace_back(current, next);
149  current = next;
150  if (current != s.end()) {
151  // it was the delimiter - need to be at the start of the next entry
152  ++current;
153  }
154  }
155  }
156 }
157 
158 } // namespace
159 
160 } // namespace grpc_python_generator
161 
162 #endif // GRPC_INTERNAL_COMPILER_PYTHON_GENERATOR_HELPERS_H
filename
const char * filename
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:135
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
grpc_python_generator
Definition: src/compiler/python_generator.cc:53
find
static void ** find(grpc_chttp2_stream_map *map, uint32_t key)
Definition: stream_map.cc:99
grpc_generator::StringReplace
std::string StringReplace(std::string str, const std::string &from, const std::string &to, bool replace_all)
Definition: generator_helpers.h:61
grpc::protobuf::FileDescriptor
GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:85
grpc_python_generator::generator_file_name
std::string generator_file_name
Definition: src/compiler/python_generator.cc:55
phone_pb2.message_type
message_type
Definition: phone_pb2.py:200
config.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
grpc_generator::COMMENTTYPE_TRAILING
@ COMMENTTYPE_TRAILING
Definition: generator_helpers.h:180
grpc::protobuf::io::ZeroCopyOutputStream
GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ZeroCopyOutputStream
Definition: include/grpcpp/impl/codegen/config_protobuf.h:100
absl::FormatConversionChar::s
@ s
grpc_generator::GetComment
void GetComment(const DescriptorType *desc, CommentType type, std::vector< std::string > *out)
Definition: generator_helpers.h:186
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
grpc::protobuf::io::StringOutputStream
GRPC_CUSTOM_STRINGOUTPUTSTREAM StringOutputStream
Definition: src/compiler/config.h:56
grpc::protobuf::compiler::GeneratorContext
GRPC_CUSTOM_GENERATORCONTEXT GeneratorContext
Definition: src/compiler/config.h:42
grpc_generator::COMMENTTYPE_LEADING_DETACHED
@ COMMENTTYPE_LEADING_DETACHED
Definition: generator_helpers.h:181
python_private_generator.h
ModuleName
static std::string ModuleName(const protobuf::FileDescriptor *file)
Definition: upbc.cc:57
grpc::protobuf::MethodDescriptor
GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:87
grpc_generator::Split
void Split(const std::string &s, char, std::vector< std::string > *append_to)
Definition: generator_helpers.h:169
grpc::protobuf::io::CodedOutputStream
GRPC_CUSTOM_CODEDOUTPUTSTREAM CodedOutputStream
Definition: src/compiler/config.h:55
generate_in_pb2_grpc
bool generate_in_pb2_grpc
Definition: python_private_generator.h:40
generator_helpers.h
grpc_generator::StripProto
std::string StripProto(std::string filename)
Definition: generator_helpers.h:54
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
descriptor_database_test_wrapper.module
module
Definition: descriptor_database_test_wrapper.py:30
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
grpc_generator::COMMENTTYPE_LEADING
@ COMMENTTYPE_LEADING
Definition: generator_helpers.h:179
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
grpc::protobuf::Descriptor
GRPC_CUSTOM_DESCRIPTOR Descriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:81
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
grpc::protobuf::ServiceDescriptor
GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor
Definition: include/grpcpp/impl/codegen/config_protobuf.h:88
python_generator.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:58