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 | |
| 69 | PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) |
| 70 | : config_(config) {} |
| 71 | |
| 72 | PythonGrpcGenerator::~PythonGrpcGenerator() {} |
| 73 | |
| 74 | bool PythonGrpcGenerator::Generate( |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 75 | const FileDescriptor* file, const grpc::string& parameter, |
| 76 | GeneratorContext* context, grpc::string* error) const { |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 77 | // Get output file name. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 78 | grpc::string file_name; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 79 | 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 Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 93 | grpc::string code = ""; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 94 | 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 Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 103 | namespace { |
| 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 Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 110 | map<grpc::string, grpc::string> ListToDict( |
| 111 | const initializer_list<grpc::string>& values) { |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 112 | assert(values.size() % 2 == 0); |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 113 | map<grpc::string, grpc::string> value_map; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 114 | auto value_iter = values.begin(); |
| 115 | for (unsigned i = 0; i < values.size()/2; ++i) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 116 | grpc::string key = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 117 | ++value_iter; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 118 | grpc::string value = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 119 | 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 | // } |
| 132 | class 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 Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 150 | // TODO(protobuf team): Export `ModuleName` from protobuf's |
| 151 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 152 | grpc::string ModuleName(const grpc::string& filename) { |
| 153 | grpc::string basename = StripProto(filename); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 154 | basename = StringReplace(basename, "-", "_"); |
| 155 | basename = StringReplace(basename, "/", "."); |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 156 | return basename + "_pb2"; |
| 157 | } |
| 158 | |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 159 | bool GetModuleAndMessagePath(const Descriptor* type, |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 160 | pair<grpc::string, grpc::string>* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 161 | 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 Pai | c089743 | 2015-07-27 22:18:13 +0000 | [diff] [blame] | 166 | } while (path_elem_type); // implicit nullptr comparison; don't be explicit |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 167 | grpc::string file_name = type->file()->name(); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 168 | 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 Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 172 | } |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 173 | grpc::string module = ModuleName(file_name); |
| 174 | grpc::string message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 175 | for (auto path_iter = message_path.rbegin(); |
| 176 | path_iter != message_path.rend(); ++path_iter) { |
| 177 | message_type += (*path_iter)->name() + "."; |
| 178 | } |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 179 | // no pop_back prior to C++11 |
| 180 | message_type.resize(message_type.size() - 1); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 181 | *out = make_pair(module, message_type); |
| 182 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 185 | bool 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"); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame^] | 193 | out->Print(dict, "class Beta$Service$Servicer(object):\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 194 | { |
| 195 | IndentScope raii_class_indent(out); |
| 196 | out->Print(dict, "\"\"\"$Documentation$\"\"\"\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 197 | 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"; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 201 | out->Print("def $Method$(self, $ArgName$, context):\n", |
| 202 | "Method", meth->name(), "ArgName", arg_name); |
| 203 | { |
| 204 | IndentScope raii_method_indent(out); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame^] | 205 | out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | bool PrintBetaStub(const ServiceDescriptor* service, |
| 213 | Printer* out) { |
| 214 | grpc::string doc = "The interface to which stubs will conform."; |
| 215 | map<grpc::string, grpc::string> dict = ListToDict({ |
| 216 | "Service", service->name(), |
| 217 | "Documentation", doc, |
| 218 | }); |
| 219 | out->Print("\n"); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame^] | 220 | out->Print(dict, "class Beta$Service$Stub(object):\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 221 | { |
| 222 | IndentScope raii_class_indent(out); |
| 223 | out->Print(dict, "\"\"\"$Documentation$\"\"\"\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 224 | for (int i = 0; i < service->method_count(); ++i) { |
| 225 | const MethodDescriptor* meth = service->method(i); |
| 226 | grpc::string arg_name = meth->client_streaming() ? |
| 227 | "request_iterator" : "request"; |
| 228 | auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 229 | out->Print(methdict, "def $Method$(self, $ArgName$, timeout):\n"); |
| 230 | { |
| 231 | IndentScope raii_method_indent(out); |
| 232 | out->Print("raise NotImplementedError()\n"); |
| 233 | } |
| 234 | if (!meth->server_streaming()) { |
| 235 | out->Print(methdict, "$Method$.future = None\n"); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | return true; |
| 240 | } |
| 241 | |
| 242 | bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, |
| 243 | const ServiceDescriptor* service, Printer* out) { |
| 244 | out->Print("\n"); |
| 245 | out->Print("def beta_create_$Service$_server(servicer, pool=None, " |
| 246 | "pool_size=None, default_timeout=None, maximum_timeout=None):\n", |
| 247 | "Service", service->name()); |
| 248 | { |
| 249 | IndentScope raii_create_server_indent(out); |
| 250 | map<grpc::string, grpc::string> method_implementation_constructors; |
| 251 | map<grpc::string, pair<grpc::string, grpc::string>> |
| 252 | input_message_modules_and_classes; |
| 253 | map<grpc::string, pair<grpc::string, grpc::string>> |
| 254 | output_message_modules_and_classes; |
| 255 | for (int i = 0; i < service->method_count(); ++i) { |
| 256 | const MethodDescriptor* method = service->method(i); |
| 257 | const grpc::string method_implementation_constructor = |
| 258 | grpc::string(method->client_streaming() ? "stream_" : "unary_") + |
| 259 | grpc::string(method->server_streaming() ? "stream_" : "unary_") + |
| 260 | "inline"; |
| 261 | pair<grpc::string, grpc::string> input_message_module_and_class; |
| 262 | if (!GetModuleAndMessagePath(method->input_type(), |
| 263 | &input_message_module_and_class)) { |
| 264 | return false; |
| 265 | } |
| 266 | pair<grpc::string, grpc::string> output_message_module_and_class; |
| 267 | if (!GetModuleAndMessagePath(method->output_type(), |
| 268 | &output_message_module_and_class)) { |
| 269 | return false; |
| 270 | } |
| 271 | // Import the modules that define the messages used in RPCs. |
| 272 | out->Print("import $Module$\n", "Module", |
| 273 | input_message_module_and_class.first); |
| 274 | out->Print("import $Module$\n", "Module", |
| 275 | output_message_module_and_class.first); |
| 276 | method_implementation_constructors.insert( |
| 277 | make_pair(method->name(), method_implementation_constructor)); |
| 278 | input_message_modules_and_classes.insert( |
| 279 | make_pair(method->name(), input_message_module_and_class)); |
| 280 | output_message_modules_and_classes.insert( |
| 281 | make_pair(method->name(), output_message_module_and_class)); |
| 282 | } |
| 283 | out->Print("request_deserializers = {\n"); |
| 284 | for (auto name_and_input_module_class_pair = |
| 285 | input_message_modules_and_classes.begin(); |
| 286 | name_and_input_module_class_pair != |
| 287 | input_message_modules_and_classes.end(); |
| 288 | name_and_input_module_class_pair++) { |
| 289 | IndentScope raii_indent(out); |
| 290 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 291 | "$InputTypeModule$.$InputTypeClass$.FromString,\n", |
| 292 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 293 | "MethodName", name_and_input_module_class_pair->first, |
| 294 | "InputTypeModule", |
| 295 | name_and_input_module_class_pair->second.first, |
| 296 | "InputTypeClass", |
| 297 | name_and_input_module_class_pair->second.second); |
| 298 | } |
| 299 | out->Print("}\n"); |
| 300 | out->Print("response_serializers = {\n"); |
| 301 | for (auto name_and_output_module_class_pair = |
| 302 | output_message_modules_and_classes.begin(); |
| 303 | name_and_output_module_class_pair != |
| 304 | output_message_modules_and_classes.end(); |
| 305 | name_and_output_module_class_pair++) { |
| 306 | IndentScope raii_indent(out); |
| 307 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 308 | "$OutputTypeModule$.$OutputTypeClass$.SerializeToString,\n", |
| 309 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 310 | "MethodName", name_and_output_module_class_pair->first, |
| 311 | "OutputTypeModule", |
| 312 | name_and_output_module_class_pair->second.first, |
| 313 | "OutputTypeClass", |
| 314 | name_and_output_module_class_pair->second.second); |
| 315 | } |
| 316 | out->Print("}\n"); |
| 317 | out->Print("method_implementations = {\n"); |
| 318 | for (auto name_and_implementation_constructor = |
| 319 | method_implementation_constructors.begin(); |
| 320 | name_and_implementation_constructor != |
| 321 | method_implementation_constructors.end(); |
| 322 | name_and_implementation_constructor++) { |
| 323 | IndentScope raii_descriptions_indent(out); |
| 324 | const grpc::string method_name = |
| 325 | name_and_implementation_constructor->first; |
| 326 | out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): " |
| 327 | "face_utilities.$Constructor$(servicer.$Method$),\n", |
| 328 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 329 | "Method", name_and_implementation_constructor->first, |
| 330 | "Constructor", name_and_implementation_constructor->second); |
| 331 | } |
| 332 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 333 | out->Print("server_options = beta_implementations.server_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 334 | "request_deserializers=request_deserializers, " |
| 335 | "response_serializers=response_serializers, " |
| 336 | "thread_pool=pool, thread_pool_size=pool_size, " |
| 337 | "default_timeout=default_timeout, " |
| 338 | "maximum_timeout=maximum_timeout)\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 339 | out->Print("return beta_implementations.server(method_implementations, " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 340 | "options=server_options)\n"); |
| 341 | } |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, |
| 346 | const ServiceDescriptor* service, Printer* out) { |
| 347 | map<grpc::string, grpc::string> dict = ListToDict({ |
| 348 | "Service", service->name(), |
| 349 | }); |
| 350 | out->Print("\n"); |
| 351 | out->Print(dict, "def beta_create_$Service$_stub(channel, host=None," |
| 352 | " metadata_transformer=None, pool=None, pool_size=None):\n"); |
| 353 | { |
| 354 | IndentScope raii_create_server_indent(out); |
| 355 | map<grpc::string, grpc::string> method_cardinalities; |
| 356 | map<grpc::string, pair<grpc::string, grpc::string>> |
| 357 | input_message_modules_and_classes; |
| 358 | map<grpc::string, pair<grpc::string, grpc::string>> |
| 359 | output_message_modules_and_classes; |
| 360 | for (int i = 0; i < service->method_count(); ++i) { |
| 361 | const MethodDescriptor* method = service->method(i); |
| 362 | const grpc::string method_cardinality = |
| 363 | grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + |
| 364 | "_" + |
| 365 | grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); |
| 366 | pair<grpc::string, grpc::string> input_message_module_and_class; |
| 367 | if (!GetModuleAndMessagePath(method->input_type(), |
| 368 | &input_message_module_and_class)) { |
| 369 | return false; |
| 370 | } |
| 371 | pair<grpc::string, grpc::string> output_message_module_and_class; |
| 372 | if (!GetModuleAndMessagePath(method->output_type(), |
| 373 | &output_message_module_and_class)) { |
| 374 | return false; |
| 375 | } |
| 376 | // Import the modules that define the messages used in RPCs. |
| 377 | out->Print("import $Module$\n", "Module", |
| 378 | input_message_module_and_class.first); |
| 379 | out->Print("import $Module$\n", "Module", |
| 380 | output_message_module_and_class.first); |
| 381 | method_cardinalities.insert( |
| 382 | make_pair(method->name(), method_cardinality)); |
| 383 | input_message_modules_and_classes.insert( |
| 384 | make_pair(method->name(), input_message_module_and_class)); |
| 385 | output_message_modules_and_classes.insert( |
| 386 | make_pair(method->name(), output_message_module_and_class)); |
| 387 | } |
| 388 | out->Print("request_serializers = {\n"); |
| 389 | for (auto name_and_input_module_class_pair = |
| 390 | input_message_modules_and_classes.begin(); |
| 391 | name_and_input_module_class_pair != |
| 392 | input_message_modules_and_classes.end(); |
| 393 | name_and_input_module_class_pair++) { |
| 394 | IndentScope raii_indent(out); |
| 395 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 396 | "$InputTypeModule$.$InputTypeClass$.SerializeToString,\n", |
| 397 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 398 | "MethodName", name_and_input_module_class_pair->first, |
| 399 | "InputTypeModule", |
| 400 | name_and_input_module_class_pair->second.first, |
| 401 | "InputTypeClass", |
| 402 | name_and_input_module_class_pair->second.second); |
| 403 | } |
| 404 | out->Print("}\n"); |
| 405 | out->Print("response_deserializers = {\n"); |
| 406 | for (auto name_and_output_module_class_pair = |
| 407 | output_message_modules_and_classes.begin(); |
| 408 | name_and_output_module_class_pair != |
| 409 | output_message_modules_and_classes.end(); |
| 410 | name_and_output_module_class_pair++) { |
| 411 | IndentScope raii_indent(out); |
| 412 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 413 | "$OutputTypeModule$.$OutputTypeClass$.FromString,\n", |
| 414 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 415 | "MethodName", name_and_output_module_class_pair->first, |
| 416 | "OutputTypeModule", |
| 417 | name_and_output_module_class_pair->second.first, |
| 418 | "OutputTypeClass", |
| 419 | name_and_output_module_class_pair->second.second); |
| 420 | } |
| 421 | out->Print("}\n"); |
| 422 | out->Print("cardinalities = {\n"); |
| 423 | for (auto name_and_cardinality = method_cardinalities.begin(); |
| 424 | name_and_cardinality != method_cardinalities.end(); |
| 425 | name_and_cardinality++) { |
| 426 | IndentScope raii_descriptions_indent(out); |
| 427 | out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n", |
| 428 | "Method", name_and_cardinality->first, |
| 429 | "Cardinality", name_and_cardinality->second); |
| 430 | } |
| 431 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 432 | out->Print("stub_options = beta_implementations.stub_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 433 | "host=host, metadata_transformer=metadata_transformer, " |
| 434 | "request_serializers=request_serializers, " |
| 435 | "response_deserializers=response_deserializers, " |
| 436 | "thread_pool=pool, thread_pool_size=pool_size)\n"); |
| 437 | out->Print( |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 438 | "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 439 | "cardinalities, options=stub_options)\n", |
| 440 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 441 | } |
| 442 | return true; |
| 443 | } |
| 444 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 445 | bool PrintPreamble(const FileDescriptor* file, |
| 446 | const GeneratorConfiguration& config, Printer* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 447 | out->Print("import abc\n"); |
Leifur Halldor Asgeirsson | 798eeda | 2016-03-08 13:18:14 -0500 | [diff] [blame] | 448 | out->Print("import six\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 449 | out->Print("from $Package$ import implementations as beta_implementations\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 450 | "Package", config.beta_package_root); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame^] | 451 | out->Print("from $Package$ import interfaces as beta_interfaces\n", |
| 452 | "Package", config.beta_package_root); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 453 | out->Print("from grpc.framework.common import cardinality\n"); |
| 454 | out->Print("from grpc.framework.interfaces.face import utilities as face_utilities\n"); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 455 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | } // namespace |
| 459 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 460 | pair<bool, grpc::string> GetServices(const FileDescriptor* file, |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 461 | const GeneratorConfiguration& config) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 462 | grpc::string output; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 463 | { |
| 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 Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 467 | if (!PrintPreamble(file, config, &out)) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 468 | return make_pair(false, ""); |
| 469 | } |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 470 | auto package = file->package(); |
| 471 | if (!package.empty()) { |
| 472 | package = package.append("."); |
| 473 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 474 | for (int i = 0; i < file->service_count(); ++i) { |
| 475 | auto service = file->service(i); |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 476 | auto package_qualified_service_name = package + service->name(); |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 477 | if (!(PrintBetaServicer(service, &out) && |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 478 | PrintBetaStub(service, &out) && |
| 479 | PrintBetaServerFactory(package_qualified_service_name, service, &out) && |
| 480 | PrintBetaStubFactory(package_qualified_service_name, service, &out))) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 481 | return make_pair(false, ""); |
| 482 | } |
| 483 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 484 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 485 | return make_pair(true, std::move(output)); |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | } // namespace grpc_python_generator |