blob: 8e76e6dce6f0abb6d145d30b09e43a7391b8d98f [file] [log] [blame]
Masood Malekghassemif8e297a2015-02-19 15:39:32 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Masood Malekghassemif8e297a2015-02-19 15:39:32 -08004 * 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 Malekghassemi40e8cbd2015-02-26 08:39:50 -080034#include <algorithm>
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080035#include <cassert>
36#include <cctype>
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080037#include <cstring>
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080038#include <map>
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070039#include <memory>
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080040#include <ostream>
41#include <sstream>
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070042#include <tuple>
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080043#include <vector>
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080044
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070045#include "src/compiler/config.h"
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010046#include "src/compiler/generator_helpers.h"
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080047#include "src/compiler/python_generator.h"
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080048
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010049using grpc_generator::StringReplace;
50using grpc_generator::StripProto;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070051using grpc::protobuf::Descriptor;
52using grpc::protobuf::FileDescriptor;
53using grpc::protobuf::MethodDescriptor;
54using grpc::protobuf::ServiceDescriptor;
55using grpc::protobuf::compiler::GeneratorContext;
56using grpc::protobuf::io::CodedOutputStream;
57using grpc::protobuf::io::Printer;
58using grpc::protobuf::io::StringOutputStream;
59using grpc::protobuf::io::ZeroCopyOutputStream;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080060using std::initializer_list;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080061using std::make_pair;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080062using std::map;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080063using std::pair;
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -080064using std::replace;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080065using std::vector;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080066
67namespace grpc_python_generator {
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070068
69PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config)
70 : config_(config) {}
71
72PythonGrpcGenerator::~PythonGrpcGenerator() {}
73
74bool PythonGrpcGenerator::Generate(
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070075 const FileDescriptor* file, const grpc::string& parameter,
76 GeneratorContext* context, grpc::string* error) const {
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070077 // Get output file name.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070078 grpc::string file_name;
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070079 static const int proto_suffix_length = strlen(".proto");
80 if (file->name().size() > static_cast<size_t>(proto_suffix_length) &&
81 file->name().find_last_of(".proto") == file->name().size() - 1) {
82 file_name = file->name().substr(
83 0, file->name().size() - proto_suffix_length) + "_pb2.py";
84 } else {
85 *error = "Invalid proto file name. Proto file must end with .proto";
86 return false;
87 }
88
89 std::unique_ptr<ZeroCopyOutputStream> output(
90 context->OpenForInsert(file_name, "module_scope"));
91 CodedOutputStream coded_out(output.get());
92 bool success = false;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070093 grpc::string code = "";
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070094 tie(success, code) = grpc_python_generator::GetServices(file, config_);
95 if (success) {
96 coded_out.WriteRaw(code.data(), code.size());
97 return true;
98 } else {
99 return false;
100 }
101}
102
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800103namespace {
104//////////////////////////////////
105// BEGIN FORMATTING BOILERPLATE //
106//////////////////////////////////
107
108// Converts an initializer list of the form { key0, value0, key1, value1, ... }
109// into a map of key* to value*. Is merely a readability helper for later code.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700110map<grpc::string, grpc::string> ListToDict(
111 const initializer_list<grpc::string>& values) {
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800112 assert(values.size() % 2 == 0);
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700113 map<grpc::string, grpc::string> value_map;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800114 auto value_iter = values.begin();
115 for (unsigned i = 0; i < values.size()/2; ++i) {
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700116 grpc::string key = *value_iter;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800117 ++value_iter;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700118 grpc::string value = *value_iter;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800119 value_map[key] = value;
120 ++value_iter;
121 }
122 return value_map;
123}
124
125// Provides RAII indentation handling. Use as:
126// {
127// IndentScope raii_my_indent_var_name_here(my_py_printer);
128// // constructor indented my_py_printer
129// ...
130// // destructor called at end of scope, un-indenting my_py_printer
131// }
132class IndentScope {
133 public:
134 explicit IndentScope(Printer* printer) : printer_(printer) {
135 printer_->Indent();
136 }
137
138 ~IndentScope() {
139 printer_->Outdent();
140 }
141
142 private:
143 Printer* printer_;
144};
145
146////////////////////////////////
147// END FORMATTING BOILERPLATE //
148////////////////////////////////
149
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -0800150// TODO(protobuf team): Export `ModuleName` from protobuf's
151// `src/google/protobuf/compiler/python/python_generator.cc` file.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700152grpc::string ModuleName(const grpc::string& filename) {
153 grpc::string basename = StripProto(filename);
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +0100154 basename = StringReplace(basename, "-", "_");
155 basename = StringReplace(basename, "/", ".");
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -0800156 return basename + "_pb2";
157}
158
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800159bool GetModuleAndMessagePath(const Descriptor* type,
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700160 pair<grpc::string, grpc::string>* out) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800161 const Descriptor* path_elem_type = type;
162 vector<const Descriptor*> message_path;
163 do {
164 message_path.push_back(path_elem_type);
165 path_elem_type = path_elem_type->containing_type();
Vijay Paic0897432015-07-27 22:18:13 +0000166 } while (path_elem_type); // implicit nullptr comparison; don't be explicit
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700167 grpc::string file_name = type->file()->name();
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800168 static const int proto_suffix_length = strlen(".proto");
169 if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) &&
170 file_name.find_last_of(".proto") == file_name.size() - 1)) {
171 return false;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800172 }
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700173 grpc::string module = ModuleName(file_name);
174 grpc::string message_type;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800175 for (auto path_iter = message_path.rbegin();
176 path_iter != message_path.rend(); ++path_iter) {
177 message_type += (*path_iter)->name() + ".";
178 }
Craig Tillercf133f42015-02-26 14:05:56 -0800179 // no pop_back prior to C++11
180 message_type.resize(message_type.size() - 1);
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800181 *out = make_pair(module, message_type);
182 return true;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800183}
184
yang-gb0de7162016-05-03 15:48:19 -0700185// Get all comments (leading, leading_detached, trailing) and print them as a
186// docstring. Any leading space of a line will be removed, but the line wrapping
187// will not be changed.
188template <typename DescriptorType>
189static void PrintAllComments(const DescriptorType* desc, Printer* printer) {
190 std::vector<grpc::string> comments;
191 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED,
192 &comments);
193 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING,
194 &comments);
195 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING,
196 &comments);
197 if (comments.empty()) {
198 return;
199 }
200 printer->Print("\"\"\"");
201 for (auto it = comments.begin(); it != comments.end(); ++it) {
202 size_t start_pos = it->find_first_not_of(' ');
203 if (start_pos != grpc::string::npos) {
204 printer->Print(it->c_str() + start_pos);
205 }
206 printer->Print("\n");
207 }
208 printer->Print("\"\"\"\n");
209}
210
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000211bool PrintBetaServicer(const ServiceDescriptor* service,
212 Printer* out) {
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000213 out->Print("\n");
yang-gb0de7162016-05-03 15:48:19 -0700214 out->Print("class Beta$Service$Servicer(object):\n", "Service",
215 service->name());
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000216 {
217 IndentScope raii_class_indent(out);
yang-gb0de7162016-05-03 15:48:19 -0700218 PrintAllComments(service, out);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000219 for (int i = 0; i < service->method_count(); ++i) {
220 auto meth = service->method(i);
221 grpc::string arg_name = meth->client_streaming() ?
222 "request_iterator" : "request";
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000223 out->Print("def $Method$(self, $ArgName$, context):\n",
224 "Method", meth->name(), "ArgName", arg_name);
225 {
226 IndentScope raii_method_indent(out);
yang-gb0de7162016-05-03 15:48:19 -0700227 PrintAllComments(meth, out);
Masood Malekghassemi832ae812016-04-27 18:38:54 -0700228 out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n");
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000229 }
230 }
231 }
232 return true;
233}
234
235bool PrintBetaStub(const ServiceDescriptor* service,
236 Printer* out) {
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000237 out->Print("\n");
yang-gb0de7162016-05-03 15:48:19 -0700238 out->Print("class Beta$Service$Stub(object):\n", "Service", service->name());
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000239 {
240 IndentScope raii_class_indent(out);
yang-gb0de7162016-05-03 15:48:19 -0700241 PrintAllComments(service, out);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000242 for (int i = 0; i < service->method_count(); ++i) {
243 const MethodDescriptor* meth = service->method(i);
244 grpc::string arg_name = meth->client_streaming() ?
245 "request_iterator" : "request";
246 auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name});
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000247 out->Print(methdict, "def $Method$(self, $ArgName$, timeout):\n");
248 {
249 IndentScope raii_method_indent(out);
yang-gb0de7162016-05-03 15:48:19 -0700250 PrintAllComments(meth, out);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000251 out->Print("raise NotImplementedError()\n");
252 }
253 if (!meth->server_streaming()) {
254 out->Print(methdict, "$Method$.future = None\n");
255 }
256 }
257 }
258 return true;
259}
260
261bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name,
262 const ServiceDescriptor* service, Printer* out) {
263 out->Print("\n");
264 out->Print("def beta_create_$Service$_server(servicer, pool=None, "
265 "pool_size=None, default_timeout=None, maximum_timeout=None):\n",
266 "Service", service->name());
267 {
268 IndentScope raii_create_server_indent(out);
269 map<grpc::string, grpc::string> method_implementation_constructors;
270 map<grpc::string, pair<grpc::string, grpc::string>>
271 input_message_modules_and_classes;
272 map<grpc::string, pair<grpc::string, grpc::string>>
273 output_message_modules_and_classes;
274 for (int i = 0; i < service->method_count(); ++i) {
275 const MethodDescriptor* method = service->method(i);
276 const grpc::string method_implementation_constructor =
277 grpc::string(method->client_streaming() ? "stream_" : "unary_") +
278 grpc::string(method->server_streaming() ? "stream_" : "unary_") +
279 "inline";
280 pair<grpc::string, grpc::string> input_message_module_and_class;
281 if (!GetModuleAndMessagePath(method->input_type(),
282 &input_message_module_and_class)) {
283 return false;
284 }
285 pair<grpc::string, grpc::string> output_message_module_and_class;
286 if (!GetModuleAndMessagePath(method->output_type(),
287 &output_message_module_and_class)) {
288 return false;
289 }
290 // Import the modules that define the messages used in RPCs.
291 out->Print("import $Module$\n", "Module",
292 input_message_module_and_class.first);
293 out->Print("import $Module$\n", "Module",
294 output_message_module_and_class.first);
295 method_implementation_constructors.insert(
296 make_pair(method->name(), method_implementation_constructor));
297 input_message_modules_and_classes.insert(
298 make_pair(method->name(), input_message_module_and_class));
299 output_message_modules_and_classes.insert(
300 make_pair(method->name(), output_message_module_and_class));
301 }
302 out->Print("request_deserializers = {\n");
303 for (auto name_and_input_module_class_pair =
304 input_message_modules_and_classes.begin();
305 name_and_input_module_class_pair !=
306 input_message_modules_and_classes.end();
307 name_and_input_module_class_pair++) {
308 IndentScope raii_indent(out);
309 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
310 "$InputTypeModule$.$InputTypeClass$.FromString,\n",
311 "PackageQualifiedServiceName", package_qualified_service_name,
312 "MethodName", name_and_input_module_class_pair->first,
313 "InputTypeModule",
314 name_and_input_module_class_pair->second.first,
315 "InputTypeClass",
316 name_and_input_module_class_pair->second.second);
317 }
318 out->Print("}\n");
319 out->Print("response_serializers = {\n");
320 for (auto name_and_output_module_class_pair =
321 output_message_modules_and_classes.begin();
322 name_and_output_module_class_pair !=
323 output_message_modules_and_classes.end();
324 name_and_output_module_class_pair++) {
325 IndentScope raii_indent(out);
326 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
327 "$OutputTypeModule$.$OutputTypeClass$.SerializeToString,\n",
328 "PackageQualifiedServiceName", package_qualified_service_name,
329 "MethodName", name_and_output_module_class_pair->first,
330 "OutputTypeModule",
331 name_and_output_module_class_pair->second.first,
332 "OutputTypeClass",
333 name_and_output_module_class_pair->second.second);
334 }
335 out->Print("}\n");
336 out->Print("method_implementations = {\n");
337 for (auto name_and_implementation_constructor =
338 method_implementation_constructors.begin();
339 name_and_implementation_constructor !=
340 method_implementation_constructors.end();
341 name_and_implementation_constructor++) {
342 IndentScope raii_descriptions_indent(out);
343 const grpc::string method_name =
344 name_and_implementation_constructor->first;
345 out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): "
346 "face_utilities.$Constructor$(servicer.$Method$),\n",
347 "PackageQualifiedServiceName", package_qualified_service_name,
348 "Method", name_and_implementation_constructor->first,
349 "Constructor", name_and_implementation_constructor->second);
350 }
351 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000352 out->Print("server_options = beta_implementations.server_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000353 "request_deserializers=request_deserializers, "
354 "response_serializers=response_serializers, "
355 "thread_pool=pool, thread_pool_size=pool_size, "
356 "default_timeout=default_timeout, "
357 "maximum_timeout=maximum_timeout)\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000358 out->Print("return beta_implementations.server(method_implementations, "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000359 "options=server_options)\n");
360 }
361 return true;
362}
363
364bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name,
365 const ServiceDescriptor* service, Printer* out) {
366 map<grpc::string, grpc::string> dict = ListToDict({
367 "Service", service->name(),
368 });
369 out->Print("\n");
370 out->Print(dict, "def beta_create_$Service$_stub(channel, host=None,"
371 " metadata_transformer=None, pool=None, pool_size=None):\n");
372 {
373 IndentScope raii_create_server_indent(out);
374 map<grpc::string, grpc::string> method_cardinalities;
375 map<grpc::string, pair<grpc::string, grpc::string>>
376 input_message_modules_and_classes;
377 map<grpc::string, pair<grpc::string, grpc::string>>
378 output_message_modules_and_classes;
379 for (int i = 0; i < service->method_count(); ++i) {
380 const MethodDescriptor* method = service->method(i);
381 const grpc::string method_cardinality =
382 grpc::string(method->client_streaming() ? "STREAM" : "UNARY") +
383 "_" +
384 grpc::string(method->server_streaming() ? "STREAM" : "UNARY");
385 pair<grpc::string, grpc::string> input_message_module_and_class;
386 if (!GetModuleAndMessagePath(method->input_type(),
387 &input_message_module_and_class)) {
388 return false;
389 }
390 pair<grpc::string, grpc::string> output_message_module_and_class;
391 if (!GetModuleAndMessagePath(method->output_type(),
392 &output_message_module_and_class)) {
393 return false;
394 }
395 // Import the modules that define the messages used in RPCs.
396 out->Print("import $Module$\n", "Module",
397 input_message_module_and_class.first);
398 out->Print("import $Module$\n", "Module",
399 output_message_module_and_class.first);
400 method_cardinalities.insert(
401 make_pair(method->name(), method_cardinality));
402 input_message_modules_and_classes.insert(
403 make_pair(method->name(), input_message_module_and_class));
404 output_message_modules_and_classes.insert(
405 make_pair(method->name(), output_message_module_and_class));
406 }
407 out->Print("request_serializers = {\n");
408 for (auto name_and_input_module_class_pair =
409 input_message_modules_and_classes.begin();
410 name_and_input_module_class_pair !=
411 input_message_modules_and_classes.end();
412 name_and_input_module_class_pair++) {
413 IndentScope raii_indent(out);
414 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
415 "$InputTypeModule$.$InputTypeClass$.SerializeToString,\n",
416 "PackageQualifiedServiceName", package_qualified_service_name,
417 "MethodName", name_and_input_module_class_pair->first,
418 "InputTypeModule",
419 name_and_input_module_class_pair->second.first,
420 "InputTypeClass",
421 name_and_input_module_class_pair->second.second);
422 }
423 out->Print("}\n");
424 out->Print("response_deserializers = {\n");
425 for (auto name_and_output_module_class_pair =
426 output_message_modules_and_classes.begin();
427 name_and_output_module_class_pair !=
428 output_message_modules_and_classes.end();
429 name_and_output_module_class_pair++) {
430 IndentScope raii_indent(out);
431 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
432 "$OutputTypeModule$.$OutputTypeClass$.FromString,\n",
433 "PackageQualifiedServiceName", package_qualified_service_name,
434 "MethodName", name_and_output_module_class_pair->first,
435 "OutputTypeModule",
436 name_and_output_module_class_pair->second.first,
437 "OutputTypeClass",
438 name_and_output_module_class_pair->second.second);
439 }
440 out->Print("}\n");
441 out->Print("cardinalities = {\n");
442 for (auto name_and_cardinality = method_cardinalities.begin();
443 name_and_cardinality != method_cardinalities.end();
444 name_and_cardinality++) {
445 IndentScope raii_descriptions_indent(out);
446 out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n",
447 "Method", name_and_cardinality->first,
448 "Cardinality", name_and_cardinality->second);
449 }
450 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000451 out->Print("stub_options = beta_implementations.stub_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000452 "host=host, metadata_transformer=metadata_transformer, "
453 "request_serializers=request_serializers, "
454 "response_deserializers=response_deserializers, "
455 "thread_pool=pool, thread_pool_size=pool_size)\n");
456 out->Print(
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000457 "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000458 "cardinalities, options=stub_options)\n",
459 "PackageQualifiedServiceName", package_qualified_service_name);
460 }
461 return true;
462}
463
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700464bool PrintPreamble(const FileDescriptor* file,
465 const GeneratorConfiguration& config, Printer* out) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800466 out->Print("import abc\n");
Leifur Halldor Asgeirsson798eeda2016-03-08 13:18:14 -0500467 out->Print("import six\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000468 out->Print("from $Package$ import implementations as beta_implementations\n",
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000469 "Package", config.beta_package_root);
Masood Malekghassemi832ae812016-04-27 18:38:54 -0700470 out->Print("from $Package$ import interfaces as beta_interfaces\n",
471 "Package", config.beta_package_root);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000472 out->Print("from grpc.framework.common import cardinality\n");
473 out->Print("from grpc.framework.interfaces.face import utilities as face_utilities\n");
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800474 return true;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800475}
476
477} // namespace
478
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700479pair<bool, grpc::string> GetServices(const FileDescriptor* file,
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000480 const GeneratorConfiguration& config) {
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700481 grpc::string output;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800482 {
483 // Scope the output stream so it closes and finalizes output to the string.
484 StringOutputStream output_stream(&output);
485 Printer out(&output_stream, '$');
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700486 if (!PrintPreamble(file, config, &out)) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800487 return make_pair(false, "");
488 }
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000489 auto package = file->package();
490 if (!package.empty()) {
491 package = package.append(".");
492 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800493 for (int i = 0; i < file->service_count(); ++i) {
494 auto service = file->service(i);
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000495 auto package_qualified_service_name = package + service->name();
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000496 if (!(PrintBetaServicer(service, &out) &&
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000497 PrintBetaStub(service, &out) &&
498 PrintBetaServerFactory(package_qualified_service_name, service, &out) &&
499 PrintBetaStubFactory(package_qualified_service_name, service, &out))) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800500 return make_pair(false, "");
501 }
502 }
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800503 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800504 return make_pair(true, std::move(output));
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800505}
506
507} // namespace grpc_python_generator