Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 34 | #include <algorithm> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 35 | #include <cassert> |
| 36 | #include <cctype> |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 37 | #include <cstring> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 38 | #include <map> |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 39 | #include <memory> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 40 | #include <ostream> |
| 41 | #include <sstream> |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 42 | #include <tuple> |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 43 | #include <vector> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 44 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 45 | #include "src/compiler/config.h" |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 46 | #include "src/compiler/generator_helpers.h" |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 47 | #include "src/compiler/python_generator.h" |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 48 | |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 49 | using grpc_generator::StringReplace; |
| 50 | using grpc_generator::StripProto; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 51 | using grpc::protobuf::Descriptor; |
| 52 | using grpc::protobuf::FileDescriptor; |
| 53 | using grpc::protobuf::MethodDescriptor; |
| 54 | using grpc::protobuf::ServiceDescriptor; |
| 55 | using grpc::protobuf::compiler::GeneratorContext; |
| 56 | using grpc::protobuf::io::CodedOutputStream; |
| 57 | using grpc::protobuf::io::Printer; |
| 58 | using grpc::protobuf::io::StringOutputStream; |
| 59 | using grpc::protobuf::io::ZeroCopyOutputStream; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 60 | using std::initializer_list; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 61 | using std::make_pair; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 62 | using std::map; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 63 | using std::pair; |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 64 | using std::replace; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 65 | using std::vector; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 66 | |
| 67 | namespace grpc_python_generator { |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 68 | |
Masood Malekghassemi | 71448b2 | 2016-06-29 11:41:24 -0700 | [diff] [blame] | 69 | GeneratorConfiguration::GeneratorConfiguration() |
| 70 | : grpc_package_root("grpc"), beta_package_root("grpc.beta") {} |
| 71 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 72 | PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) |
| 73 | : config_(config) {} |
| 74 | |
| 75 | PythonGrpcGenerator::~PythonGrpcGenerator() {} |
| 76 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 77 | bool PythonGrpcGenerator::Generate(const FileDescriptor* file, |
| 78 | const grpc::string& parameter, |
| 79 | GeneratorContext* context, |
| 80 | grpc::string* error) const { |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 81 | // Get output file name. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 82 | grpc::string file_name; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 83 | static const int proto_suffix_length = strlen(".proto"); |
| 84 | if (file->name().size() > static_cast<size_t>(proto_suffix_length) && |
| 85 | file->name().find_last_of(".proto") == file->name().size() - 1) { |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 86 | file_name = |
| 87 | file->name().substr(0, file->name().size() - proto_suffix_length) + |
| 88 | "_pb2.py"; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 89 | } else { |
| 90 | *error = "Invalid proto file name. Proto file must end with .proto"; |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | std::unique_ptr<ZeroCopyOutputStream> output( |
| 95 | context->OpenForInsert(file_name, "module_scope")); |
| 96 | CodedOutputStream coded_out(output.get()); |
| 97 | bool success = false; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 98 | grpc::string code = ""; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 99 | tie(success, code) = grpc_python_generator::GetServices(file, config_); |
| 100 | if (success) { |
| 101 | coded_out.WriteRaw(code.data(), code.size()); |
| 102 | return true; |
| 103 | } else { |
| 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 108 | namespace { |
| 109 | ////////////////////////////////// |
| 110 | // BEGIN FORMATTING BOILERPLATE // |
| 111 | ////////////////////////////////// |
| 112 | |
| 113 | // Converts an initializer list of the form { key0, value0, key1, value1, ... } |
| 114 | // into a map of key* to value*. Is merely a readability helper for later code. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 115 | map<grpc::string, grpc::string> ListToDict( |
| 116 | const initializer_list<grpc::string>& values) { |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 117 | assert(values.size() % 2 == 0); |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 118 | map<grpc::string, grpc::string> value_map; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 119 | auto value_iter = values.begin(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 120 | for (unsigned i = 0; i < values.size() / 2; ++i) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 121 | grpc::string key = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 122 | ++value_iter; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 123 | grpc::string value = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 124 | value_map[key] = value; |
| 125 | ++value_iter; |
| 126 | } |
| 127 | return value_map; |
| 128 | } |
| 129 | |
| 130 | // Provides RAII indentation handling. Use as: |
| 131 | // { |
| 132 | // IndentScope raii_my_indent_var_name_here(my_py_printer); |
| 133 | // // constructor indented my_py_printer |
| 134 | // ... |
| 135 | // // destructor called at end of scope, un-indenting my_py_printer |
| 136 | // } |
| 137 | class IndentScope { |
| 138 | public: |
| 139 | explicit IndentScope(Printer* printer) : printer_(printer) { |
| 140 | printer_->Indent(); |
| 141 | } |
| 142 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 143 | ~IndentScope() { printer_->Outdent(); } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | Printer* printer_; |
| 147 | }; |
| 148 | |
| 149 | //////////////////////////////// |
| 150 | // END FORMATTING BOILERPLATE // |
| 151 | //////////////////////////////// |
| 152 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 153 | // TODO(https://github.com/google/protobuf/issues/888): |
| 154 | // Export `ModuleName` from protobuf's |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 155 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 156 | grpc::string ModuleName(const grpc::string& filename) { |
| 157 | grpc::string basename = StripProto(filename); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 158 | basename = StringReplace(basename, "-", "_"); |
| 159 | basename = StringReplace(basename, "/", "."); |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 160 | return basename + "_pb2"; |
| 161 | } |
| 162 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 163 | // TODO(https://github.com/google/protobuf/issues/888): |
| 164 | // Export `ModuleAlias` from protobuf's |
| 165 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
| 166 | grpc::string ModuleAlias(const grpc::string& filename) { |
| 167 | grpc::string module_name = ModuleName(filename); |
| 168 | // We can't have dots in the module name, so we replace each with _dot_. |
| 169 | // But that could lead to a collision between a.b and a_dot_b, so we also |
| 170 | // duplicate each underscore. |
| 171 | module_name = StringReplace(module_name, "_", "__"); |
| 172 | module_name = StringReplace(module_name, ".", "_dot_"); |
| 173 | return module_name; |
| 174 | } |
| 175 | |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 176 | bool GetModuleAndMessagePath(const Descriptor* type, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 177 | const ServiceDescriptor* service, |
| 178 | grpc::string* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 179 | const Descriptor* path_elem_type = type; |
| 180 | vector<const Descriptor*> message_path; |
| 181 | do { |
| 182 | message_path.push_back(path_elem_type); |
| 183 | path_elem_type = path_elem_type->containing_type(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 184 | } while (path_elem_type); // implicit nullptr comparison; don't be explicit |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 185 | grpc::string file_name = type->file()->name(); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 186 | static const int proto_suffix_length = strlen(".proto"); |
| 187 | if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) && |
| 188 | file_name.find_last_of(".proto") == file_name.size() - 1)) { |
| 189 | return false; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 190 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 191 | grpc::string service_file_name = service->file()->name(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 192 | grpc::string module = |
| 193 | service_file_name == file_name ? "" : ModuleAlias(file_name) + "."; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 194 | grpc::string message_type; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 195 | for (auto path_iter = message_path.rbegin(); path_iter != message_path.rend(); |
| 196 | ++path_iter) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 197 | message_type += (*path_iter)->name() + "."; |
| 198 | } |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 199 | // no pop_back prior to C++11 |
| 200 | message_type.resize(message_type.size() - 1); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 201 | *out = module + message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 202 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 203 | } |
| 204 | |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 205 | // Get all comments (leading, leading_detached, trailing) and print them as a |
| 206 | // docstring. Any leading space of a line will be removed, but the line wrapping |
| 207 | // will not be changed. |
| 208 | template <typename DescriptorType> |
| 209 | static void PrintAllComments(const DescriptorType* desc, Printer* printer) { |
| 210 | std::vector<grpc::string> comments; |
| 211 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, |
| 212 | &comments); |
| 213 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, |
| 214 | &comments); |
| 215 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, |
| 216 | &comments); |
| 217 | if (comments.empty()) { |
| 218 | return; |
| 219 | } |
| 220 | printer->Print("\"\"\""); |
| 221 | for (auto it = comments.begin(); it != comments.end(); ++it) { |
| 222 | size_t start_pos = it->find_first_not_of(' '); |
| 223 | if (start_pos != grpc::string::npos) { |
| 224 | printer->Print(it->c_str() + start_pos); |
| 225 | } |
| 226 | printer->Print("\n"); |
| 227 | } |
| 228 | printer->Print("\"\"\"\n"); |
| 229 | } |
| 230 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 231 | bool PrintBetaServicer(const ServiceDescriptor* service, Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 232 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 233 | out->Print("class Beta$Service$Servicer(object):\n", "Service", |
| 234 | service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 235 | { |
| 236 | IndentScope raii_class_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame^] | 237 | out->Print( |
| 238 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 239 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 240 | "file not marked beta) for all further purposes. This class was " |
| 241 | "generated\n" |
| 242 | "only to ease transition from grpcio<0.15.0 to " |
| 243 | "grpcio>=0.15.0.\"\"\"\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 244 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 245 | for (int i = 0; i < service->method_count(); ++i) { |
| 246 | auto meth = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 247 | grpc::string arg_name = |
| 248 | meth->client_streaming() ? "request_iterator" : "request"; |
| 249 | out->Print("def $Method$(self, $ArgName$, context):\n", "Method", |
| 250 | meth->name(), "ArgName", arg_name); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 251 | { |
| 252 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 253 | PrintAllComments(meth, out); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 254 | out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 261 | bool PrintBetaStub(const ServiceDescriptor* service, Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 262 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 263 | out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 264 | { |
| 265 | IndentScope raii_class_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame^] | 266 | out->Print( |
| 267 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 268 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 269 | "file not marked beta) for all further purposes. This class was " |
| 270 | "generated\n" |
| 271 | "only to ease transition from grpcio<0.15.0 to " |
| 272 | "grpcio>=0.15.0.\"\"\"\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 273 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 274 | for (int i = 0; i < service->method_count(); ++i) { |
| 275 | const MethodDescriptor* meth = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 276 | grpc::string arg_name = |
| 277 | meth->client_streaming() ? "request_iterator" : "request"; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 278 | auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 279 | out->Print(methdict, |
| 280 | "def $Method$(self, $ArgName$, timeout, metadata=None, " |
| 281 | "with_call=False, protocol_options=None):\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 282 | { |
| 283 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 284 | PrintAllComments(meth, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 285 | out->Print("raise NotImplementedError()\n"); |
| 286 | } |
| 287 | if (!meth->server_streaming()) { |
| 288 | out->Print(methdict, "$Method$.future = None\n"); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, |
| 296 | const ServiceDescriptor* service, Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 297 | out->Print("\n\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 298 | out->Print( |
| 299 | "def beta_create_$Service$_server(servicer, pool=None, " |
| 300 | "pool_size=None, default_timeout=None, maximum_timeout=None):\n", |
| 301 | "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 302 | { |
| 303 | IndentScope raii_create_server_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame^] | 304 | out->Print( |
| 305 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 306 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 307 | "file not marked beta) for all further purposes. This function was\n" |
| 308 | "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" |
| 309 | "\"\"\"\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 310 | map<grpc::string, grpc::string> method_implementation_constructors; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 311 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 312 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 313 | for (int i = 0; i < service->method_count(); ++i) { |
| 314 | const MethodDescriptor* method = service->method(i); |
| 315 | const grpc::string method_implementation_constructor = |
| 316 | grpc::string(method->client_streaming() ? "stream_" : "unary_") + |
| 317 | grpc::string(method->server_streaming() ? "stream_" : "unary_") + |
| 318 | "inline"; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 319 | grpc::string input_message_module_and_class; |
| 320 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 321 | &input_message_module_and_class)) { |
| 322 | return false; |
| 323 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 324 | grpc::string output_message_module_and_class; |
| 325 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 326 | &output_message_module_and_class)) { |
| 327 | return false; |
| 328 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 329 | method_implementation_constructors.insert( |
| 330 | make_pair(method->name(), method_implementation_constructor)); |
| 331 | input_message_modules_and_classes.insert( |
| 332 | make_pair(method->name(), input_message_module_and_class)); |
| 333 | output_message_modules_and_classes.insert( |
| 334 | make_pair(method->name(), output_message_module_and_class)); |
| 335 | } |
| 336 | out->Print("request_deserializers = {\n"); |
| 337 | for (auto name_and_input_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 338 | input_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 339 | name_and_input_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 340 | input_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 341 | name_and_input_module_class_pair++) { |
| 342 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 343 | out->Print( |
| 344 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 345 | "$InputTypeModuleAndClass$.FromString,\n", |
| 346 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 347 | "MethodName", name_and_input_module_class_pair->first, |
| 348 | "InputTypeModuleAndClass", name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 349 | } |
| 350 | out->Print("}\n"); |
| 351 | out->Print("response_serializers = {\n"); |
| 352 | for (auto name_and_output_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 353 | output_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 354 | name_and_output_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 355 | output_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 356 | name_and_output_module_class_pair++) { |
| 357 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 358 | out->Print( |
| 359 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 360 | "$OutputTypeModuleAndClass$.SerializeToString,\n", |
| 361 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 362 | "MethodName", name_and_output_module_class_pair->first, |
| 363 | "OutputTypeModuleAndClass", |
| 364 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 365 | } |
| 366 | out->Print("}\n"); |
| 367 | out->Print("method_implementations = {\n"); |
| 368 | for (auto name_and_implementation_constructor = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 369 | method_implementation_constructors.begin(); |
| 370 | name_and_implementation_constructor != |
| 371 | method_implementation_constructors.end(); |
| 372 | name_and_implementation_constructor++) { |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 373 | IndentScope raii_descriptions_indent(out); |
| 374 | const grpc::string method_name = |
| 375 | name_and_implementation_constructor->first; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 376 | out->Print( |
| 377 | "(\'$PackageQualifiedServiceName$\', \'$Method$\'): " |
| 378 | "face_utilities.$Constructor$(servicer.$Method$),\n", |
| 379 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 380 | "Method", name_and_implementation_constructor->first, "Constructor", |
| 381 | name_and_implementation_constructor->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 382 | } |
| 383 | out->Print("}\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 384 | out->Print( |
| 385 | "server_options = beta_implementations.server_options(" |
| 386 | "request_deserializers=request_deserializers, " |
| 387 | "response_serializers=response_serializers, " |
| 388 | "thread_pool=pool, thread_pool_size=pool_size, " |
| 389 | "default_timeout=default_timeout, " |
| 390 | "maximum_timeout=maximum_timeout)\n"); |
| 391 | out->Print( |
| 392 | "return beta_implementations.server(method_implementations, " |
| 393 | "options=server_options)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, |
| 399 | const ServiceDescriptor* service, Printer* out) { |
| 400 | map<grpc::string, grpc::string> dict = ListToDict({ |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 401 | "Service", service->name(), |
| 402 | }); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 403 | out->Print("\n\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 404 | out->Print(dict, |
| 405 | "def beta_create_$Service$_stub(channel, host=None," |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 406 | " metadata_transformer=None, pool=None, pool_size=None):\n"); |
| 407 | { |
| 408 | IndentScope raii_create_server_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame^] | 409 | out->Print( |
| 410 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 411 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 412 | "file not marked beta) for all further purposes. This function was\n" |
| 413 | "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" |
| 414 | "\"\"\"\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 415 | map<grpc::string, grpc::string> method_cardinalities; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 416 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 417 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 418 | for (int i = 0; i < service->method_count(); ++i) { |
| 419 | const MethodDescriptor* method = service->method(i); |
| 420 | const grpc::string method_cardinality = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 421 | grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + "_" + |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 422 | grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); |
| 423 | grpc::string input_message_module_and_class; |
| 424 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 425 | &input_message_module_and_class)) { |
| 426 | return false; |
| 427 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 428 | grpc::string output_message_module_and_class; |
| 429 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 430 | &output_message_module_and_class)) { |
| 431 | return false; |
| 432 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 433 | method_cardinalities.insert( |
| 434 | make_pair(method->name(), method_cardinality)); |
| 435 | input_message_modules_and_classes.insert( |
| 436 | make_pair(method->name(), input_message_module_and_class)); |
| 437 | output_message_modules_and_classes.insert( |
| 438 | make_pair(method->name(), output_message_module_and_class)); |
| 439 | } |
| 440 | out->Print("request_serializers = {\n"); |
| 441 | for (auto name_and_input_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 442 | input_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 443 | name_and_input_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 444 | input_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 445 | name_and_input_module_class_pair++) { |
| 446 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 447 | out->Print( |
| 448 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 449 | "$InputTypeModuleAndClass$.SerializeToString,\n", |
| 450 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 451 | "MethodName", name_and_input_module_class_pair->first, |
| 452 | "InputTypeModuleAndClass", name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 453 | } |
| 454 | out->Print("}\n"); |
| 455 | out->Print("response_deserializers = {\n"); |
| 456 | for (auto name_and_output_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 457 | output_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 458 | name_and_output_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 459 | output_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 460 | name_and_output_module_class_pair++) { |
| 461 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 462 | out->Print( |
| 463 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 464 | "$OutputTypeModuleAndClass$.FromString,\n", |
| 465 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 466 | "MethodName", name_and_output_module_class_pair->first, |
| 467 | "OutputTypeModuleAndClass", |
| 468 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 469 | } |
| 470 | out->Print("}\n"); |
| 471 | out->Print("cardinalities = {\n"); |
| 472 | for (auto name_and_cardinality = method_cardinalities.begin(); |
| 473 | name_and_cardinality != method_cardinalities.end(); |
| 474 | name_and_cardinality++) { |
| 475 | IndentScope raii_descriptions_indent(out); |
| 476 | out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 477 | "Method", name_and_cardinality->first, "Cardinality", |
| 478 | name_and_cardinality->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 479 | } |
| 480 | out->Print("}\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 481 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 482 | "stub_options = beta_implementations.stub_options(" |
| 483 | "host=host, metadata_transformer=metadata_transformer, " |
| 484 | "request_serializers=request_serializers, " |
| 485 | "response_deserializers=response_deserializers, " |
| 486 | "thread_pool=pool, thread_pool_size=pool_size)\n"); |
| 487 | out->Print( |
| 488 | "return beta_implementations.dynamic_stub(channel, " |
| 489 | "\'$PackageQualifiedServiceName$\', " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 490 | "cardinalities, options=stub_options)\n", |
| 491 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 496 | bool PrintStub(const grpc::string& package_qualified_service_name, |
| 497 | const ServiceDescriptor* service, Printer* out) { |
| 498 | out->Print("\n\n"); |
| 499 | out->Print("class $Service$Stub(object):\n", "Service", service->name()); |
| 500 | { |
| 501 | IndentScope raii_class_indent(out); |
| 502 | PrintAllComments(service, out); |
| 503 | out->Print("\n"); |
| 504 | out->Print("def __init__(self, channel):\n"); |
| 505 | { |
| 506 | IndentScope raii_init_indent(out); |
| 507 | out->Print("\"\"\"Constructor.\n"); |
| 508 | out->Print("\n"); |
| 509 | out->Print("Args:\n"); |
| 510 | { |
| 511 | IndentScope raii_args_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 512 | out->Print("channel: A grpc.Channel.\n"); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 513 | } |
| 514 | out->Print("\"\"\"\n"); |
| 515 | for (int i = 0; i < service->method_count(); ++i) { |
| 516 | auto method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 517 | auto multi_callable_constructor = |
| 518 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
| 519 | "_" + grpc::string(method->server_streaming() ? "stream" : "unary"); |
| 520 | grpc::string request_module_and_class; |
| 521 | if (!GetModuleAndMessagePath(method->input_type(), service, |
| 522 | &request_module_and_class)) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 523 | return false; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 524 | } |
| 525 | grpc::string response_module_and_class; |
| 526 | if (!GetModuleAndMessagePath(method->output_type(), service, |
| 527 | &response_module_and_class)) { |
| 528 | return false; |
| 529 | } |
| 530 | out->Print("self.$Method$ = channel.$MultiCallableConstructor$(\n", |
| 531 | "Method", method->name(), "MultiCallableConstructor", |
| 532 | multi_callable_constructor); |
| 533 | { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 534 | IndentScope raii_first_attribute_indent(out); |
| 535 | IndentScope raii_second_attribute_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 536 | out->Print("'/$PackageQualifiedService$/$Method$',\n", |
| 537 | "PackageQualifiedService", package_qualified_service_name, |
| 538 | "Method", method->name()); |
| 539 | out->Print( |
| 540 | "request_serializer=$RequestModuleAndClass$.SerializeToString,\n", |
| 541 | "RequestModuleAndClass", request_module_and_class); |
| 542 | out->Print( |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 543 | "response_deserializer=$ResponseModuleAndClass$.FromString,\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 544 | "ResponseModuleAndClass", response_module_and_class); |
| 545 | out->Print(")\n"); |
| 546 | } |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | return true; |
| 551 | } |
| 552 | |
| 553 | bool PrintServicer(const ServiceDescriptor* service, Printer* out) { |
| 554 | out->Print("\n\n"); |
| 555 | out->Print("class $Service$Servicer(object):\n", "Service", service->name()); |
| 556 | { |
| 557 | IndentScope raii_class_indent(out); |
| 558 | PrintAllComments(service, out); |
| 559 | for (int i = 0; i < service->method_count(); ++i) { |
| 560 | auto method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 561 | grpc::string arg_name = |
| 562 | method->client_streaming() ? "request_iterator" : "request"; |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 563 | out->Print("\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 564 | out->Print("def $Method$(self, $ArgName$, context):\n", "Method", |
| 565 | method->name(), "ArgName", arg_name); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 566 | { |
| 567 | IndentScope raii_method_indent(out); |
| 568 | PrintAllComments(method, out); |
| 569 | out->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n"); |
| 570 | out->Print("context.set_details('Method not implemented!')\n"); |
| 571 | out->Print("raise NotImplementedError('Method not implemented!')\n"); |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | return true; |
| 576 | } |
| 577 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 578 | bool PrintAddServicerToServer( |
| 579 | const grpc::string& package_qualified_service_name, |
| 580 | const ServiceDescriptor* service, Printer* out) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 581 | out->Print("\n\n"); |
| 582 | out->Print("def add_$Service$Servicer_to_server(servicer, server):\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 583 | "Service", service->name()); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 584 | { |
| 585 | IndentScope raii_class_indent(out); |
| 586 | out->Print("rpc_method_handlers = {\n"); |
| 587 | { |
| 588 | IndentScope raii_dict_first_indent(out); |
| 589 | IndentScope raii_dict_second_indent(out); |
| 590 | for (int i = 0; i < service->method_count(); ++i) { |
| 591 | auto method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 592 | auto method_handler_constructor = |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 593 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 594 | "_" + |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 595 | grpc::string(method->server_streaming() ? "stream" : "unary") + |
| 596 | "_rpc_method_handler"; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 597 | grpc::string request_module_and_class; |
| 598 | if (!GetModuleAndMessagePath(method->input_type(), service, |
| 599 | &request_module_and_class)) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 600 | return false; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 601 | } |
| 602 | grpc::string response_module_and_class; |
| 603 | if (!GetModuleAndMessagePath(method->output_type(), service, |
| 604 | &response_module_and_class)) { |
| 605 | return false; |
| 606 | } |
| 607 | out->Print("'$Method$': grpc.$MethodHandlerConstructor$(\n", "Method", |
| 608 | method->name(), "MethodHandlerConstructor", |
| 609 | method_handler_constructor); |
| 610 | { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 611 | IndentScope raii_call_first_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 612 | IndentScope raii_call_second_indent(out); |
| 613 | out->Print("servicer.$Method$,\n", "Method", method->name()); |
| 614 | out->Print( |
| 615 | "request_deserializer=$RequestModuleAndClass$.FromString,\n", |
| 616 | "RequestModuleAndClass", request_module_and_class); |
| 617 | out->Print( |
| 618 | "response_serializer=$ResponseModuleAndClass$.SerializeToString," |
| 619 | "\n", |
| 620 | "ResponseModuleAndClass", response_module_and_class); |
| 621 | } |
| 622 | out->Print("),\n"); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | out->Print("}\n"); |
| 626 | out->Print("generic_handler = grpc.method_handlers_generic_handler(\n"); |
| 627 | { |
| 628 | IndentScope raii_call_first_indent(out); |
| 629 | IndentScope raii_call_second_indent(out); |
| 630 | out->Print("'$PackageQualifiedServiceName$', rpc_method_handlers)\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 631 | "PackageQualifiedServiceName", package_qualified_service_name); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 632 | } |
| 633 | out->Print("server.add_generic_rpc_handlers((generic_handler,))\n"); |
| 634 | } |
| 635 | return true; |
| 636 | } |
| 637 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 638 | bool PrintPreamble(const FileDescriptor* file, |
| 639 | const GeneratorConfiguration& config, Printer* out) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 640 | out->Print("import $Package$\n", "Package", config.grpc_package_root); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 641 | out->Print("from $Package$ import implementations as beta_implementations\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 642 | "Package", config.beta_package_root); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 643 | out->Print("from $Package$ import interfaces as beta_interfaces\n", "Package", |
| 644 | config.beta_package_root); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 645 | out->Print("from grpc.framework.common import cardinality\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 646 | out->Print( |
| 647 | "from grpc.framework.interfaces.face import utilities as " |
| 648 | "face_utilities\n"); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 649 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | } // namespace |
| 653 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 654 | pair<bool, grpc::string> GetServices(const FileDescriptor* file, |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 655 | const GeneratorConfiguration& config) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 656 | grpc::string output; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 657 | { |
| 658 | // Scope the output stream so it closes and finalizes output to the string. |
| 659 | StringOutputStream output_stream(&output); |
| 660 | Printer out(&output_stream, '$'); |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 661 | if (!PrintPreamble(file, config, &out)) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 662 | return make_pair(false, ""); |
| 663 | } |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 664 | auto package = file->package(); |
| 665 | if (!package.empty()) { |
| 666 | package = package.append("."); |
| 667 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 668 | for (int i = 0; i < file->service_count(); ++i) { |
| 669 | auto service = file->service(i); |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 670 | auto package_qualified_service_name = package + service->name(); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 671 | if (!(PrintStub(package_qualified_service_name, service, &out) && |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 672 | PrintServicer(service, &out) && |
| 673 | PrintAddServicerToServer(package_qualified_service_name, service, |
| 674 | &out) && |
| 675 | PrintBetaServicer(service, &out) && PrintBetaStub(service, &out) && |
| 676 | PrintBetaServerFactory(package_qualified_service_name, service, |
| 677 | &out) && |
| 678 | PrintBetaStubFactory(package_qualified_service_name, service, |
| 679 | &out))) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 680 | return make_pair(false, ""); |
| 681 | } |
| 682 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 683 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 684 | return make_pair(true, std::move(output)); |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | } // namespace grpc_python_generator |