blob: 02c032800b1bd4068f02e8b98bccc0af6dea2f41 [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
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000185bool PrintBetaServicer(const ServiceDescriptor* service,
186 Printer* out) {
187 grpc::string doc = "<fill me in later!>";
188 map<grpc::string, grpc::string> dict = ListToDict({
189 "Service", service->name(),
190 "Documentation", doc,
191 });
192 out->Print("\n");
Leifur Halldor Asgeirsson798eeda2016-03-08 13:18:14 -0500193 out->Print(dict, "class Beta$Service$Servicer(six.with_metaclass(abc.ABCMeta, object)):\n");
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000194 {
195 IndentScope raii_class_indent(out);
196 out->Print(dict, "\"\"\"$Documentation$\"\"\"\n");
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000197 for (int i = 0; i < service->method_count(); ++i) {
198 auto meth = service->method(i);
199 grpc::string arg_name = meth->client_streaming() ?
200 "request_iterator" : "request";
201 out->Print("@abc.abstractmethod\n");
202 out->Print("def $Method$(self, $ArgName$, context):\n",
203 "Method", meth->name(), "ArgName", arg_name);
204 {
205 IndentScope raii_method_indent(out);
206 out->Print("raise NotImplementedError()\n");
207 }
208 }
209 }
210 return true;
211}
212
213bool PrintBetaStub(const ServiceDescriptor* service,
214 Printer* out) {
215 grpc::string doc = "The interface to which stubs will conform.";
216 map<grpc::string, grpc::string> dict = ListToDict({
217 "Service", service->name(),
218 "Documentation", doc,
219 });
220 out->Print("\n");
Leifur Halldor Asgeirsson798eeda2016-03-08 13:18:14 -0500221 out->Print(dict, "class Beta$Service$Stub(six.with_metaclass(abc.ABCMeta, object)):\n");
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000222 {
223 IndentScope raii_class_indent(out);
224 out->Print(dict, "\"\"\"$Documentation$\"\"\"\n");
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000225 for (int i = 0; i < service->method_count(); ++i) {
226 const MethodDescriptor* meth = service->method(i);
227 grpc::string arg_name = meth->client_streaming() ?
228 "request_iterator" : "request";
229 auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name});
230 out->Print("@abc.abstractmethod\n");
231 out->Print(methdict, "def $Method$(self, $ArgName$, timeout):\n");
232 {
233 IndentScope raii_method_indent(out);
234 out->Print("raise NotImplementedError()\n");
235 }
236 if (!meth->server_streaming()) {
237 out->Print(methdict, "$Method$.future = None\n");
238 }
239 }
240 }
241 return true;
242}
243
244bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name,
245 const ServiceDescriptor* service, Printer* out) {
246 out->Print("\n");
247 out->Print("def beta_create_$Service$_server(servicer, pool=None, "
248 "pool_size=None, default_timeout=None, maximum_timeout=None):\n",
249 "Service", service->name());
250 {
251 IndentScope raii_create_server_indent(out);
252 map<grpc::string, grpc::string> method_implementation_constructors;
253 map<grpc::string, pair<grpc::string, grpc::string>>
254 input_message_modules_and_classes;
255 map<grpc::string, pair<grpc::string, grpc::string>>
256 output_message_modules_and_classes;
257 for (int i = 0; i < service->method_count(); ++i) {
258 const MethodDescriptor* method = service->method(i);
259 const grpc::string method_implementation_constructor =
260 grpc::string(method->client_streaming() ? "stream_" : "unary_") +
261 grpc::string(method->server_streaming() ? "stream_" : "unary_") +
262 "inline";
263 pair<grpc::string, grpc::string> input_message_module_and_class;
264 if (!GetModuleAndMessagePath(method->input_type(),
265 &input_message_module_and_class)) {
266 return false;
267 }
268 pair<grpc::string, grpc::string> output_message_module_and_class;
269 if (!GetModuleAndMessagePath(method->output_type(),
270 &output_message_module_and_class)) {
271 return false;
272 }
273 // Import the modules that define the messages used in RPCs.
274 out->Print("import $Module$\n", "Module",
275 input_message_module_and_class.first);
276 out->Print("import $Module$\n", "Module",
277 output_message_module_and_class.first);
278 method_implementation_constructors.insert(
279 make_pair(method->name(), method_implementation_constructor));
280 input_message_modules_and_classes.insert(
281 make_pair(method->name(), input_message_module_and_class));
282 output_message_modules_and_classes.insert(
283 make_pair(method->name(), output_message_module_and_class));
284 }
285 out->Print("request_deserializers = {\n");
286 for (auto name_and_input_module_class_pair =
287 input_message_modules_and_classes.begin();
288 name_and_input_module_class_pair !=
289 input_message_modules_and_classes.end();
290 name_and_input_module_class_pair++) {
291 IndentScope raii_indent(out);
292 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
293 "$InputTypeModule$.$InputTypeClass$.FromString,\n",
294 "PackageQualifiedServiceName", package_qualified_service_name,
295 "MethodName", name_and_input_module_class_pair->first,
296 "InputTypeModule",
297 name_and_input_module_class_pair->second.first,
298 "InputTypeClass",
299 name_and_input_module_class_pair->second.second);
300 }
301 out->Print("}\n");
302 out->Print("response_serializers = {\n");
303 for (auto name_and_output_module_class_pair =
304 output_message_modules_and_classes.begin();
305 name_and_output_module_class_pair !=
306 output_message_modules_and_classes.end();
307 name_and_output_module_class_pair++) {
308 IndentScope raii_indent(out);
309 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
310 "$OutputTypeModule$.$OutputTypeClass$.SerializeToString,\n",
311 "PackageQualifiedServiceName", package_qualified_service_name,
312 "MethodName", name_and_output_module_class_pair->first,
313 "OutputTypeModule",
314 name_and_output_module_class_pair->second.first,
315 "OutputTypeClass",
316 name_and_output_module_class_pair->second.second);
317 }
318 out->Print("}\n");
319 out->Print("method_implementations = {\n");
320 for (auto name_and_implementation_constructor =
321 method_implementation_constructors.begin();
322 name_and_implementation_constructor !=
323 method_implementation_constructors.end();
324 name_and_implementation_constructor++) {
325 IndentScope raii_descriptions_indent(out);
326 const grpc::string method_name =
327 name_and_implementation_constructor->first;
328 out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): "
329 "face_utilities.$Constructor$(servicer.$Method$),\n",
330 "PackageQualifiedServiceName", package_qualified_service_name,
331 "Method", name_and_implementation_constructor->first,
332 "Constructor", name_and_implementation_constructor->second);
333 }
334 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000335 out->Print("server_options = beta_implementations.server_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000336 "request_deserializers=request_deserializers, "
337 "response_serializers=response_serializers, "
338 "thread_pool=pool, thread_pool_size=pool_size, "
339 "default_timeout=default_timeout, "
340 "maximum_timeout=maximum_timeout)\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000341 out->Print("return beta_implementations.server(method_implementations, "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000342 "options=server_options)\n");
343 }
344 return true;
345}
346
347bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name,
348 const ServiceDescriptor* service, Printer* out) {
349 map<grpc::string, grpc::string> dict = ListToDict({
350 "Service", service->name(),
351 });
352 out->Print("\n");
353 out->Print(dict, "def beta_create_$Service$_stub(channel, host=None,"
354 " metadata_transformer=None, pool=None, pool_size=None):\n");
355 {
356 IndentScope raii_create_server_indent(out);
357 map<grpc::string, grpc::string> method_cardinalities;
358 map<grpc::string, pair<grpc::string, grpc::string>>
359 input_message_modules_and_classes;
360 map<grpc::string, pair<grpc::string, grpc::string>>
361 output_message_modules_and_classes;
362 for (int i = 0; i < service->method_count(); ++i) {
363 const MethodDescriptor* method = service->method(i);
364 const grpc::string method_cardinality =
365 grpc::string(method->client_streaming() ? "STREAM" : "UNARY") +
366 "_" +
367 grpc::string(method->server_streaming() ? "STREAM" : "UNARY");
368 pair<grpc::string, grpc::string> input_message_module_and_class;
369 if (!GetModuleAndMessagePath(method->input_type(),
370 &input_message_module_and_class)) {
371 return false;
372 }
373 pair<grpc::string, grpc::string> output_message_module_and_class;
374 if (!GetModuleAndMessagePath(method->output_type(),
375 &output_message_module_and_class)) {
376 return false;
377 }
378 // Import the modules that define the messages used in RPCs.
379 out->Print("import $Module$\n", "Module",
380 input_message_module_and_class.first);
381 out->Print("import $Module$\n", "Module",
382 output_message_module_and_class.first);
383 method_cardinalities.insert(
384 make_pair(method->name(), method_cardinality));
385 input_message_modules_and_classes.insert(
386 make_pair(method->name(), input_message_module_and_class));
387 output_message_modules_and_classes.insert(
388 make_pair(method->name(), output_message_module_and_class));
389 }
390 out->Print("request_serializers = {\n");
391 for (auto name_and_input_module_class_pair =
392 input_message_modules_and_classes.begin();
393 name_and_input_module_class_pair !=
394 input_message_modules_and_classes.end();
395 name_and_input_module_class_pair++) {
396 IndentScope raii_indent(out);
397 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
398 "$InputTypeModule$.$InputTypeClass$.SerializeToString,\n",
399 "PackageQualifiedServiceName", package_qualified_service_name,
400 "MethodName", name_and_input_module_class_pair->first,
401 "InputTypeModule",
402 name_and_input_module_class_pair->second.first,
403 "InputTypeClass",
404 name_and_input_module_class_pair->second.second);
405 }
406 out->Print("}\n");
407 out->Print("response_deserializers = {\n");
408 for (auto name_and_output_module_class_pair =
409 output_message_modules_and_classes.begin();
410 name_and_output_module_class_pair !=
411 output_message_modules_and_classes.end();
412 name_and_output_module_class_pair++) {
413 IndentScope raii_indent(out);
414 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
415 "$OutputTypeModule$.$OutputTypeClass$.FromString,\n",
416 "PackageQualifiedServiceName", package_qualified_service_name,
417 "MethodName", name_and_output_module_class_pair->first,
418 "OutputTypeModule",
419 name_and_output_module_class_pair->second.first,
420 "OutputTypeClass",
421 name_and_output_module_class_pair->second.second);
422 }
423 out->Print("}\n");
424 out->Print("cardinalities = {\n");
425 for (auto name_and_cardinality = method_cardinalities.begin();
426 name_and_cardinality != method_cardinalities.end();
427 name_and_cardinality++) {
428 IndentScope raii_descriptions_indent(out);
429 out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n",
430 "Method", name_and_cardinality->first,
431 "Cardinality", name_and_cardinality->second);
432 }
433 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000434 out->Print("stub_options = beta_implementations.stub_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000435 "host=host, metadata_transformer=metadata_transformer, "
436 "request_serializers=request_serializers, "
437 "response_deserializers=response_deserializers, "
438 "thread_pool=pool, thread_pool_size=pool_size)\n");
439 out->Print(
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000440 "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000441 "cardinalities, options=stub_options)\n",
442 "PackageQualifiedServiceName", package_qualified_service_name);
443 }
444 return true;
445}
446
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700447bool PrintPreamble(const FileDescriptor* file,
448 const GeneratorConfiguration& config, Printer* out) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800449 out->Print("import abc\n");
Leifur Halldor Asgeirsson798eeda2016-03-08 13:18:14 -0500450 out->Print("import six\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000451 out->Print("from $Package$ import implementations as beta_implementations\n",
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000452 "Package", config.beta_package_root);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000453 out->Print("from grpc.framework.common import cardinality\n");
454 out->Print("from grpc.framework.interfaces.face import utilities as face_utilities\n");
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800455 return true;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800456}
457
458} // namespace
459
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700460pair<bool, grpc::string> GetServices(const FileDescriptor* file,
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000461 const GeneratorConfiguration& config) {
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700462 grpc::string output;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800463 {
464 // Scope the output stream so it closes and finalizes output to the string.
465 StringOutputStream output_stream(&output);
466 Printer out(&output_stream, '$');
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700467 if (!PrintPreamble(file, config, &out)) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800468 return make_pair(false, "");
469 }
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000470 auto package = file->package();
471 if (!package.empty()) {
472 package = package.append(".");
473 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800474 for (int i = 0; i < file->service_count(); ++i) {
475 auto service = file->service(i);
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000476 auto package_qualified_service_name = package + service->name();
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000477 if (!(PrintBetaServicer(service, &out) &&
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000478 PrintBetaStub(service, &out) &&
479 PrintBetaServerFactory(package_qualified_service_name, service, &out) &&
480 PrintBetaStubFactory(package_qualified_service_name, service, &out))) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800481 return make_pair(false, "");
482 }
483 }
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800484 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800485 return make_pair(true, std::move(output));
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800486}
487
488} // namespace grpc_python_generator