blob: c754ae299b4cade4f73d6cf7b307343f1d9488b7 [file] [log] [blame]
Masood Malekghassemif8e297a2015-02-19 15:39:32 -08001/*
2 *
Nathaniel Manista2a9244a2016-01-19 16:27:51 +00003 * Copyright 2015-2016, 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
yang-g9e2f90c2015-08-21 15:35:03 -070045#include <grpc++/support/config.h>
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070046#include "src/compiler/config.h"
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010047#include "src/compiler/generator_helpers.h"
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080048#include "src/compiler/python_generator.h"
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080049
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010050using grpc_generator::StringReplace;
51using grpc_generator::StripProto;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070052using grpc::protobuf::Descriptor;
53using grpc::protobuf::FileDescriptor;
54using grpc::protobuf::MethodDescriptor;
55using grpc::protobuf::ServiceDescriptor;
56using grpc::protobuf::compiler::GeneratorContext;
57using grpc::protobuf::io::CodedOutputStream;
58using grpc::protobuf::io::Printer;
59using grpc::protobuf::io::StringOutputStream;
60using grpc::protobuf::io::ZeroCopyOutputStream;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080061using std::initializer_list;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080062using std::make_pair;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080063using std::map;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080064using std::pair;
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -080065using std::replace;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080066using std::vector;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080067
68namespace grpc_python_generator {
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070069
70PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config)
71 : config_(config) {}
72
73PythonGrpcGenerator::~PythonGrpcGenerator() {}
74
75bool PythonGrpcGenerator::Generate(
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070076 const FileDescriptor* file, const grpc::string& parameter,
77 GeneratorContext* context, grpc::string* error) const {
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070078 // Get output file name.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070079 grpc::string file_name;
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070080 static const int proto_suffix_length = strlen(".proto");
81 if (file->name().size() > static_cast<size_t>(proto_suffix_length) &&
82 file->name().find_last_of(".proto") == file->name().size() - 1) {
83 file_name = file->name().substr(
84 0, file->name().size() - proto_suffix_length) + "_pb2.py";
85 } else {
86 *error = "Invalid proto file name. Proto file must end with .proto";
87 return false;
88 }
89
90 std::unique_ptr<ZeroCopyOutputStream> output(
91 context->OpenForInsert(file_name, "module_scope"));
92 CodedOutputStream coded_out(output.get());
93 bool success = false;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -070094 grpc::string code = "";
Masood Malekghassemi3bb52152015-03-17 21:52:52 -070095 tie(success, code) = grpc_python_generator::GetServices(file, config_);
96 if (success) {
97 coded_out.WriteRaw(code.data(), code.size());
98 return true;
99 } else {
100 return false;
101 }
102}
103
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800104namespace {
105//////////////////////////////////
106// BEGIN FORMATTING BOILERPLATE //
107//////////////////////////////////
108
109// Converts an initializer list of the form { key0, value0, key1, value1, ... }
110// into a map of key* to value*. Is merely a readability helper for later code.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700111map<grpc::string, grpc::string> ListToDict(
112 const initializer_list<grpc::string>& values) {
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800113 assert(values.size() % 2 == 0);
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700114 map<grpc::string, grpc::string> value_map;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800115 auto value_iter = values.begin();
116 for (unsigned i = 0; i < values.size()/2; ++i) {
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700117 grpc::string key = *value_iter;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800118 ++value_iter;
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700119 grpc::string value = *value_iter;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800120 value_map[key] = value;
121 ++value_iter;
122 }
123 return value_map;
124}
125
126// Provides RAII indentation handling. Use as:
127// {
128// IndentScope raii_my_indent_var_name_here(my_py_printer);
129// // constructor indented my_py_printer
130// ...
131// // destructor called at end of scope, un-indenting my_py_printer
132// }
133class IndentScope {
134 public:
135 explicit IndentScope(Printer* printer) : printer_(printer) {
136 printer_->Indent();
137 }
138
139 ~IndentScope() {
140 printer_->Outdent();
141 }
142
143 private:
144 Printer* printer_;
145};
146
147////////////////////////////////
148// END FORMATTING BOILERPLATE //
149////////////////////////////////
150
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -0800151// TODO(protobuf team): Export `ModuleName` from protobuf's
152// `src/google/protobuf/compiler/python/python_generator.cc` file.
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700153grpc::string ModuleName(const grpc::string& filename) {
154 grpc::string basename = StripProto(filename);
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +0100155 basename = StringReplace(basename, "-", "_");
156 basename = StringReplace(basename, "/", ".");
Masood Malekghassemi40e8cbd2015-02-26 08:39:50 -0800157 return basename + "_pb2";
158}
159
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800160bool GetModuleAndMessagePath(const Descriptor* type,
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700161 pair<grpc::string, grpc::string>* out) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800162 const Descriptor* path_elem_type = type;
163 vector<const Descriptor*> message_path;
164 do {
165 message_path.push_back(path_elem_type);
166 path_elem_type = path_elem_type->containing_type();
Vijay Paic0897432015-07-27 22:18:13 +0000167 } while (path_elem_type); // implicit nullptr comparison; don't be explicit
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700168 grpc::string file_name = type->file()->name();
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800169 static const int proto_suffix_length = strlen(".proto");
170 if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) &&
171 file_name.find_last_of(".proto") == file_name.size() - 1)) {
172 return false;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800173 }
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700174 grpc::string module = ModuleName(file_name);
175 grpc::string message_type;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800176 for (auto path_iter = message_path.rbegin();
177 path_iter != message_path.rend(); ++path_iter) {
178 message_type += (*path_iter)->name() + ".";
179 }
Craig Tillercf133f42015-02-26 14:05:56 -0800180 // no pop_back prior to C++11
181 message_type.resize(message_type.size() - 1);
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800182 *out = make_pair(module, message_type);
183 return true;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800184}
185
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000186bool PrintBetaServicer(const ServiceDescriptor* service,
187 Printer* out) {
188 grpc::string doc = "<fill me in later!>";
189 map<grpc::string, grpc::string> dict = ListToDict({
190 "Service", service->name(),
191 "Documentation", doc,
192 });
193 out->Print("\n");
194 out->Print(dict, "class Beta$Service$Servicer(object):\n");
195 {
196 IndentScope raii_class_indent(out);
197 out->Print(dict, "\"\"\"$Documentation$\"\"\"\n");
198 out->Print("__metaclass__ = abc.ABCMeta\n");
199 for (int i = 0; i < service->method_count(); ++i) {
200 auto meth = service->method(i);
201 grpc::string arg_name = meth->client_streaming() ?
202 "request_iterator" : "request";
203 out->Print("@abc.abstractmethod\n");
204 out->Print("def $Method$(self, $ArgName$, context):\n",
205 "Method", meth->name(), "ArgName", arg_name);
206 {
207 IndentScope raii_method_indent(out);
208 out->Print("raise NotImplementedError()\n");
209 }
210 }
211 }
212 return true;
213}
214
215bool PrintBetaStub(const ServiceDescriptor* service,
216 Printer* out) {
217 grpc::string doc = "The interface to which stubs will conform.";
218 map<grpc::string, grpc::string> dict = ListToDict({
219 "Service", service->name(),
220 "Documentation", doc,
221 });
222 out->Print("\n");
223 out->Print(dict, "class Beta$Service$Stub(object):\n");
224 {
225 IndentScope raii_class_indent(out);
226 out->Print(dict, "\"\"\"$Documentation$\"\"\"\n");
227 out->Print("__metaclass__ = abc.ABCMeta\n");
228 for (int i = 0; i < service->method_count(); ++i) {
229 const MethodDescriptor* meth = service->method(i);
230 grpc::string arg_name = meth->client_streaming() ?
231 "request_iterator" : "request";
232 auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name});
233 out->Print("@abc.abstractmethod\n");
234 out->Print(methdict, "def $Method$(self, $ArgName$, timeout):\n");
235 {
236 IndentScope raii_method_indent(out);
237 out->Print("raise NotImplementedError()\n");
238 }
239 if (!meth->server_streaming()) {
240 out->Print(methdict, "$Method$.future = None\n");
241 }
242 }
243 }
244 return true;
245}
246
247bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name,
248 const ServiceDescriptor* service, Printer* out) {
249 out->Print("\n");
250 out->Print("def beta_create_$Service$_server(servicer, pool=None, "
251 "pool_size=None, default_timeout=None, maximum_timeout=None):\n",
252 "Service", service->name());
253 {
254 IndentScope raii_create_server_indent(out);
255 map<grpc::string, grpc::string> method_implementation_constructors;
256 map<grpc::string, pair<grpc::string, grpc::string>>
257 input_message_modules_and_classes;
258 map<grpc::string, pair<grpc::string, grpc::string>>
259 output_message_modules_and_classes;
260 for (int i = 0; i < service->method_count(); ++i) {
261 const MethodDescriptor* method = service->method(i);
262 const grpc::string method_implementation_constructor =
263 grpc::string(method->client_streaming() ? "stream_" : "unary_") +
264 grpc::string(method->server_streaming() ? "stream_" : "unary_") +
265 "inline";
266 pair<grpc::string, grpc::string> input_message_module_and_class;
267 if (!GetModuleAndMessagePath(method->input_type(),
268 &input_message_module_and_class)) {
269 return false;
270 }
271 pair<grpc::string, grpc::string> output_message_module_and_class;
272 if (!GetModuleAndMessagePath(method->output_type(),
273 &output_message_module_and_class)) {
274 return false;
275 }
276 // Import the modules that define the messages used in RPCs.
277 out->Print("import $Module$\n", "Module",
278 input_message_module_and_class.first);
279 out->Print("import $Module$\n", "Module",
280 output_message_module_and_class.first);
281 method_implementation_constructors.insert(
282 make_pair(method->name(), method_implementation_constructor));
283 input_message_modules_and_classes.insert(
284 make_pair(method->name(), input_message_module_and_class));
285 output_message_modules_and_classes.insert(
286 make_pair(method->name(), output_message_module_and_class));
287 }
288 out->Print("request_deserializers = {\n");
289 for (auto name_and_input_module_class_pair =
290 input_message_modules_and_classes.begin();
291 name_and_input_module_class_pair !=
292 input_message_modules_and_classes.end();
293 name_and_input_module_class_pair++) {
294 IndentScope raii_indent(out);
295 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
296 "$InputTypeModule$.$InputTypeClass$.FromString,\n",
297 "PackageQualifiedServiceName", package_qualified_service_name,
298 "MethodName", name_and_input_module_class_pair->first,
299 "InputTypeModule",
300 name_and_input_module_class_pair->second.first,
301 "InputTypeClass",
302 name_and_input_module_class_pair->second.second);
303 }
304 out->Print("}\n");
305 out->Print("response_serializers = {\n");
306 for (auto name_and_output_module_class_pair =
307 output_message_modules_and_classes.begin();
308 name_and_output_module_class_pair !=
309 output_message_modules_and_classes.end();
310 name_and_output_module_class_pair++) {
311 IndentScope raii_indent(out);
312 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
313 "$OutputTypeModule$.$OutputTypeClass$.SerializeToString,\n",
314 "PackageQualifiedServiceName", package_qualified_service_name,
315 "MethodName", name_and_output_module_class_pair->first,
316 "OutputTypeModule",
317 name_and_output_module_class_pair->second.first,
318 "OutputTypeClass",
319 name_and_output_module_class_pair->second.second);
320 }
321 out->Print("}\n");
322 out->Print("method_implementations = {\n");
323 for (auto name_and_implementation_constructor =
324 method_implementation_constructors.begin();
325 name_and_implementation_constructor !=
326 method_implementation_constructors.end();
327 name_and_implementation_constructor++) {
328 IndentScope raii_descriptions_indent(out);
329 const grpc::string method_name =
330 name_and_implementation_constructor->first;
331 out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): "
332 "face_utilities.$Constructor$(servicer.$Method$),\n",
333 "PackageQualifiedServiceName", package_qualified_service_name,
334 "Method", name_and_implementation_constructor->first,
335 "Constructor", name_and_implementation_constructor->second);
336 }
337 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000338 out->Print("server_options = beta_implementations.server_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000339 "request_deserializers=request_deserializers, "
340 "response_serializers=response_serializers, "
341 "thread_pool=pool, thread_pool_size=pool_size, "
342 "default_timeout=default_timeout, "
343 "maximum_timeout=maximum_timeout)\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000344 out->Print("return beta_implementations.server(method_implementations, "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000345 "options=server_options)\n");
346 }
347 return true;
348}
349
350bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name,
351 const ServiceDescriptor* service, Printer* out) {
352 map<grpc::string, grpc::string> dict = ListToDict({
353 "Service", service->name(),
354 });
355 out->Print("\n");
356 out->Print(dict, "def beta_create_$Service$_stub(channel, host=None,"
357 " metadata_transformer=None, pool=None, pool_size=None):\n");
358 {
359 IndentScope raii_create_server_indent(out);
360 map<grpc::string, grpc::string> method_cardinalities;
361 map<grpc::string, pair<grpc::string, grpc::string>>
362 input_message_modules_and_classes;
363 map<grpc::string, pair<grpc::string, grpc::string>>
364 output_message_modules_and_classes;
365 for (int i = 0; i < service->method_count(); ++i) {
366 const MethodDescriptor* method = service->method(i);
367 const grpc::string method_cardinality =
368 grpc::string(method->client_streaming() ? "STREAM" : "UNARY") +
369 "_" +
370 grpc::string(method->server_streaming() ? "STREAM" : "UNARY");
371 pair<grpc::string, grpc::string> input_message_module_and_class;
372 if (!GetModuleAndMessagePath(method->input_type(),
373 &input_message_module_and_class)) {
374 return false;
375 }
376 pair<grpc::string, grpc::string> output_message_module_and_class;
377 if (!GetModuleAndMessagePath(method->output_type(),
378 &output_message_module_and_class)) {
379 return false;
380 }
381 // Import the modules that define the messages used in RPCs.
382 out->Print("import $Module$\n", "Module",
383 input_message_module_and_class.first);
384 out->Print("import $Module$\n", "Module",
385 output_message_module_and_class.first);
386 method_cardinalities.insert(
387 make_pair(method->name(), method_cardinality));
388 input_message_modules_and_classes.insert(
389 make_pair(method->name(), input_message_module_and_class));
390 output_message_modules_and_classes.insert(
391 make_pair(method->name(), output_message_module_and_class));
392 }
393 out->Print("request_serializers = {\n");
394 for (auto name_and_input_module_class_pair =
395 input_message_modules_and_classes.begin();
396 name_and_input_module_class_pair !=
397 input_message_modules_and_classes.end();
398 name_and_input_module_class_pair++) {
399 IndentScope raii_indent(out);
400 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
401 "$InputTypeModule$.$InputTypeClass$.SerializeToString,\n",
402 "PackageQualifiedServiceName", package_qualified_service_name,
403 "MethodName", name_and_input_module_class_pair->first,
404 "InputTypeModule",
405 name_and_input_module_class_pair->second.first,
406 "InputTypeClass",
407 name_and_input_module_class_pair->second.second);
408 }
409 out->Print("}\n");
410 out->Print("response_deserializers = {\n");
411 for (auto name_and_output_module_class_pair =
412 output_message_modules_and_classes.begin();
413 name_and_output_module_class_pair !=
414 output_message_modules_and_classes.end();
415 name_and_output_module_class_pair++) {
416 IndentScope raii_indent(out);
417 out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): "
418 "$OutputTypeModule$.$OutputTypeClass$.FromString,\n",
419 "PackageQualifiedServiceName", package_qualified_service_name,
420 "MethodName", name_and_output_module_class_pair->first,
421 "OutputTypeModule",
422 name_and_output_module_class_pair->second.first,
423 "OutputTypeClass",
424 name_and_output_module_class_pair->second.second);
425 }
426 out->Print("}\n");
427 out->Print("cardinalities = {\n");
428 for (auto name_and_cardinality = method_cardinalities.begin();
429 name_and_cardinality != method_cardinalities.end();
430 name_and_cardinality++) {
431 IndentScope raii_descriptions_indent(out);
432 out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n",
433 "Method", name_and_cardinality->first,
434 "Cardinality", name_and_cardinality->second);
435 }
436 out->Print("}\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000437 out->Print("stub_options = beta_implementations.stub_options("
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000438 "host=host, metadata_transformer=metadata_transformer, "
439 "request_serializers=request_serializers, "
440 "response_deserializers=response_deserializers, "
441 "thread_pool=pool, thread_pool_size=pool_size)\n");
442 out->Print(
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000443 "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', "
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000444 "cardinalities, options=stub_options)\n",
445 "PackageQualifiedServiceName", package_qualified_service_name);
446 }
447 return true;
448}
449
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700450bool PrintPreamble(const FileDescriptor* file,
451 const GeneratorConfiguration& config, Printer* out) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800452 out->Print("import abc\n");
Nathaniel Manistaf65d3c12015-09-05 03:55:19 +0000453 out->Print("from $Package$ import implementations as beta_implementations\n",
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000454 "Package", config.beta_package_root);
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000455 out->Print("from grpc.framework.common import cardinality\n");
456 out->Print("from grpc.framework.interfaces.face import utilities as face_utilities\n");
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800457 return true;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800458}
459
460} // namespace
461
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700462pair<bool, grpc::string> GetServices(const FileDescriptor* file,
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000463 const GeneratorConfiguration& config) {
Masood Malekghassemi65c803b2015-03-20 06:48:47 -0700464 grpc::string output;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800465 {
466 // Scope the output stream so it closes and finalizes output to the string.
467 StringOutputStream output_stream(&output);
468 Printer out(&output_stream, '$');
Masood Malekghassemi3bb52152015-03-17 21:52:52 -0700469 if (!PrintPreamble(file, config, &out)) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800470 return make_pair(false, "");
471 }
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000472 auto package = file->package();
473 if (!package.empty()) {
474 package = package.append(".");
475 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800476 for (int i = 0; i < file->service_count(); ++i) {
477 auto service = file->service(i);
Nathaniel Manistac4fada62015-03-13 22:23:59 +0000478 auto package_qualified_service_name = package + service->name();
Nathaniel Manistadc8c3232016-01-16 18:28:45 +0000479 if (!(PrintBetaServicer(service, &out) &&
Nathaniel Manistacd9ec0e2015-08-31 07:49:45 +0000480 PrintBetaStub(service, &out) &&
481 PrintBetaServerFactory(package_qualified_service_name, service, &out) &&
482 PrintBetaStubFactory(package_qualified_service_name, service, &out))) {
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800483 return make_pair(false, "");
484 }
485 }
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800486 }
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -0800487 return make_pair(true, std::move(output));
Masood Malekghassemif8e297a2015-02-19 15:39:32 -0800488}
489
490} // namespace grpc_python_generator