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 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 150 | // TODO(https://github.com/google/protobuf/issues/888): |
| 151 | // Export `ModuleName` from protobuf's |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 152 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 153 | grpc::string ModuleName(const grpc::string& filename) { |
| 154 | grpc::string basename = StripProto(filename); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 155 | basename = StringReplace(basename, "-", "_"); |
| 156 | basename = StringReplace(basename, "/", "."); |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 157 | return basename + "_pb2"; |
| 158 | } |
| 159 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 160 | // TODO(https://github.com/google/protobuf/issues/888): |
| 161 | // Export `ModuleAlias` from protobuf's |
| 162 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
| 163 | grpc::string ModuleAlias(const grpc::string& filename) { |
| 164 | grpc::string module_name = ModuleName(filename); |
| 165 | // We can't have dots in the module name, so we replace each with _dot_. |
| 166 | // But that could lead to a collision between a.b and a_dot_b, so we also |
| 167 | // duplicate each underscore. |
| 168 | module_name = StringReplace(module_name, "_", "__"); |
| 169 | module_name = StringReplace(module_name, ".", "_dot_"); |
| 170 | return module_name; |
| 171 | } |
| 172 | |
| 173 | |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 174 | bool GetModuleAndMessagePath(const Descriptor* type, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 175 | const ServiceDescriptor* service, |
| 176 | grpc::string* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 177 | const Descriptor* path_elem_type = type; |
| 178 | vector<const Descriptor*> message_path; |
| 179 | do { |
| 180 | message_path.push_back(path_elem_type); |
| 181 | path_elem_type = path_elem_type->containing_type(); |
Vijay Pai | c089743 | 2015-07-27 22:18:13 +0000 | [diff] [blame] | 182 | } while (path_elem_type); // implicit nullptr comparison; don't be explicit |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 183 | grpc::string file_name = type->file()->name(); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 184 | static const int proto_suffix_length = strlen(".proto"); |
| 185 | if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) && |
| 186 | file_name.find_last_of(".proto") == file_name.size() - 1)) { |
| 187 | return false; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 188 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 189 | grpc::string service_file_name = service->file()->name(); |
| 190 | grpc::string module = service_file_name == file_name ? |
| 191 | "" : ModuleAlias(file_name) + "."; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 192 | grpc::string message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 193 | for (auto path_iter = message_path.rbegin(); |
| 194 | path_iter != message_path.rend(); ++path_iter) { |
| 195 | message_type += (*path_iter)->name() + "."; |
| 196 | } |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 197 | // no pop_back prior to C++11 |
| 198 | message_type.resize(message_type.size() - 1); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 199 | *out = module + message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 200 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 201 | } |
| 202 | |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 203 | // Get all comments (leading, leading_detached, trailing) and print them as a |
| 204 | // docstring. Any leading space of a line will be removed, but the line wrapping |
| 205 | // will not be changed. |
| 206 | template <typename DescriptorType> |
| 207 | static void PrintAllComments(const DescriptorType* desc, Printer* printer) { |
| 208 | std::vector<grpc::string> comments; |
| 209 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, |
| 210 | &comments); |
| 211 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, |
| 212 | &comments); |
| 213 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, |
| 214 | &comments); |
| 215 | if (comments.empty()) { |
| 216 | return; |
| 217 | } |
| 218 | printer->Print("\"\"\""); |
| 219 | for (auto it = comments.begin(); it != comments.end(); ++it) { |
| 220 | size_t start_pos = it->find_first_not_of(' '); |
| 221 | if (start_pos != grpc::string::npos) { |
| 222 | printer->Print(it->c_str() + start_pos); |
| 223 | } |
| 224 | printer->Print("\n"); |
| 225 | } |
| 226 | printer->Print("\"\"\"\n"); |
| 227 | } |
| 228 | |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 229 | bool PrintBetaServicer(const ServiceDescriptor* service, |
| 230 | Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 231 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 232 | out->Print("class Beta$Service$Servicer(object):\n", "Service", |
| 233 | service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 234 | { |
| 235 | IndentScope raii_class_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 236 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 237 | for (int i = 0; i < service->method_count(); ++i) { |
| 238 | auto meth = service->method(i); |
| 239 | grpc::string arg_name = meth->client_streaming() ? |
| 240 | "request_iterator" : "request"; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 241 | out->Print("def $Method$(self, $ArgName$, context):\n", |
| 242 | "Method", meth->name(), "ArgName", arg_name); |
| 243 | { |
| 244 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 245 | PrintAllComments(meth, out); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 246 | out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | bool PrintBetaStub(const ServiceDescriptor* service, |
| 254 | Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 255 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 256 | out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 257 | { |
| 258 | IndentScope raii_class_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 259 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 260 | for (int i = 0; i < service->method_count(); ++i) { |
| 261 | const MethodDescriptor* meth = service->method(i); |
| 262 | grpc::string arg_name = meth->client_streaming() ? |
| 263 | "request_iterator" : "request"; |
| 264 | auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 265 | out->Print(methdict, "def $Method$(self, $ArgName$, timeout, metadata=None, with_call=False, protocol_options=None):\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 266 | { |
| 267 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 268 | PrintAllComments(meth, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 269 | out->Print("raise NotImplementedError()\n"); |
| 270 | } |
| 271 | if (!meth->server_streaming()) { |
| 272 | out->Print(methdict, "$Method$.future = None\n"); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, |
| 280 | const ServiceDescriptor* service, Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 281 | out->Print("\n\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 282 | out->Print("def beta_create_$Service$_server(servicer, pool=None, " |
| 283 | "pool_size=None, default_timeout=None, maximum_timeout=None):\n", |
| 284 | "Service", service->name()); |
| 285 | { |
| 286 | IndentScope raii_create_server_indent(out); |
| 287 | map<grpc::string, grpc::string> method_implementation_constructors; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 288 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 289 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 290 | for (int i = 0; i < service->method_count(); ++i) { |
| 291 | const MethodDescriptor* method = service->method(i); |
| 292 | const grpc::string method_implementation_constructor = |
| 293 | grpc::string(method->client_streaming() ? "stream_" : "unary_") + |
| 294 | grpc::string(method->server_streaming() ? "stream_" : "unary_") + |
| 295 | "inline"; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 296 | grpc::string input_message_module_and_class; |
| 297 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 298 | &input_message_module_and_class)) { |
| 299 | return false; |
| 300 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 301 | grpc::string output_message_module_and_class; |
| 302 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 303 | &output_message_module_and_class)) { |
| 304 | return false; |
| 305 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 306 | method_implementation_constructors.insert( |
| 307 | make_pair(method->name(), method_implementation_constructor)); |
| 308 | input_message_modules_and_classes.insert( |
| 309 | make_pair(method->name(), input_message_module_and_class)); |
| 310 | output_message_modules_and_classes.insert( |
| 311 | make_pair(method->name(), output_message_module_and_class)); |
| 312 | } |
| 313 | out->Print("request_deserializers = {\n"); |
| 314 | for (auto name_and_input_module_class_pair = |
| 315 | input_message_modules_and_classes.begin(); |
| 316 | name_and_input_module_class_pair != |
| 317 | input_message_modules_and_classes.end(); |
| 318 | name_and_input_module_class_pair++) { |
| 319 | IndentScope raii_indent(out); |
| 320 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 321 | "$InputTypeModuleAndClass$.FromString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 322 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 323 | "MethodName", name_and_input_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 324 | "InputTypeModuleAndClass", |
| 325 | name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 326 | } |
| 327 | out->Print("}\n"); |
| 328 | out->Print("response_serializers = {\n"); |
| 329 | for (auto name_and_output_module_class_pair = |
| 330 | output_message_modules_and_classes.begin(); |
| 331 | name_and_output_module_class_pair != |
| 332 | output_message_modules_and_classes.end(); |
| 333 | name_and_output_module_class_pair++) { |
| 334 | IndentScope raii_indent(out); |
| 335 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 336 | "$OutputTypeModuleAndClass$.SerializeToString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 337 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 338 | "MethodName", name_and_output_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 339 | "OutputTypeModuleAndClass", |
| 340 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 341 | } |
| 342 | out->Print("}\n"); |
| 343 | out->Print("method_implementations = {\n"); |
| 344 | for (auto name_and_implementation_constructor = |
| 345 | method_implementation_constructors.begin(); |
| 346 | name_and_implementation_constructor != |
| 347 | method_implementation_constructors.end(); |
| 348 | name_and_implementation_constructor++) { |
| 349 | IndentScope raii_descriptions_indent(out); |
| 350 | const grpc::string method_name = |
| 351 | name_and_implementation_constructor->first; |
| 352 | out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): " |
| 353 | "face_utilities.$Constructor$(servicer.$Method$),\n", |
| 354 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 355 | "Method", name_and_implementation_constructor->first, |
| 356 | "Constructor", name_and_implementation_constructor->second); |
| 357 | } |
| 358 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 359 | out->Print("server_options = beta_implementations.server_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 360 | "request_deserializers=request_deserializers, " |
| 361 | "response_serializers=response_serializers, " |
| 362 | "thread_pool=pool, thread_pool_size=pool_size, " |
| 363 | "default_timeout=default_timeout, " |
| 364 | "maximum_timeout=maximum_timeout)\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 365 | out->Print("return beta_implementations.server(method_implementations, " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 366 | "options=server_options)\n"); |
| 367 | } |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, |
| 372 | const ServiceDescriptor* service, Printer* out) { |
| 373 | map<grpc::string, grpc::string> dict = ListToDict({ |
| 374 | "Service", service->name(), |
| 375 | }); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 376 | out->Print("\n\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 377 | out->Print(dict, "def beta_create_$Service$_stub(channel, host=None," |
| 378 | " metadata_transformer=None, pool=None, pool_size=None):\n"); |
| 379 | { |
| 380 | IndentScope raii_create_server_indent(out); |
| 381 | map<grpc::string, grpc::string> method_cardinalities; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 382 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 383 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 384 | for (int i = 0; i < service->method_count(); ++i) { |
| 385 | const MethodDescriptor* method = service->method(i); |
| 386 | const grpc::string method_cardinality = |
| 387 | grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + |
| 388 | "_" + |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 389 | grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); |
| 390 | grpc::string input_message_module_and_class; |
| 391 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 392 | &input_message_module_and_class)) { |
| 393 | return false; |
| 394 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 395 | grpc::string output_message_module_and_class; |
| 396 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 397 | &output_message_module_and_class)) { |
| 398 | return false; |
| 399 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 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$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 415 | "$InputTypeModuleAndClass$.SerializeToString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 416 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 417 | "MethodName", name_and_input_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 418 | "InputTypeModuleAndClass", |
| 419 | name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 420 | } |
| 421 | out->Print("}\n"); |
| 422 | out->Print("response_deserializers = {\n"); |
| 423 | for (auto name_and_output_module_class_pair = |
| 424 | output_message_modules_and_classes.begin(); |
| 425 | name_and_output_module_class_pair != |
| 426 | output_message_modules_and_classes.end(); |
| 427 | name_and_output_module_class_pair++) { |
| 428 | IndentScope raii_indent(out); |
| 429 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 430 | "$OutputTypeModuleAndClass$.FromString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 431 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 432 | "MethodName", name_and_output_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame^] | 433 | "OutputTypeModuleAndClass", |
| 434 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 435 | } |
| 436 | out->Print("}\n"); |
| 437 | out->Print("cardinalities = {\n"); |
| 438 | for (auto name_and_cardinality = method_cardinalities.begin(); |
| 439 | name_and_cardinality != method_cardinalities.end(); |
| 440 | name_and_cardinality++) { |
| 441 | IndentScope raii_descriptions_indent(out); |
| 442 | out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n", |
| 443 | "Method", name_and_cardinality->first, |
| 444 | "Cardinality", name_and_cardinality->second); |
| 445 | } |
| 446 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 447 | out->Print("stub_options = beta_implementations.stub_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 448 | "host=host, metadata_transformer=metadata_transformer, " |
| 449 | "request_serializers=request_serializers, " |
| 450 | "response_deserializers=response_deserializers, " |
| 451 | "thread_pool=pool, thread_pool_size=pool_size)\n"); |
| 452 | out->Print( |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 453 | "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 454 | "cardinalities, options=stub_options)\n", |
| 455 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 456 | } |
| 457 | return true; |
| 458 | } |
| 459 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 460 | bool PrintPreamble(const FileDescriptor* file, |
| 461 | const GeneratorConfiguration& config, Printer* out) { |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 462 | out->Print("from $Package$ import implementations as beta_implementations\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 463 | "Package", config.beta_package_root); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 464 | out->Print("from $Package$ import interfaces as beta_interfaces\n", |
| 465 | "Package", config.beta_package_root); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 466 | out->Print("from grpc.framework.common import cardinality\n"); |
| 467 | 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] | 468 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | } // namespace |
| 472 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 473 | pair<bool, grpc::string> GetServices(const FileDescriptor* file, |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 474 | const GeneratorConfiguration& config) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 475 | grpc::string output; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 476 | { |
| 477 | // Scope the output stream so it closes and finalizes output to the string. |
| 478 | StringOutputStream output_stream(&output); |
| 479 | Printer out(&output_stream, '$'); |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 480 | if (!PrintPreamble(file, config, &out)) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 481 | return make_pair(false, ""); |
| 482 | } |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 483 | auto package = file->package(); |
| 484 | if (!package.empty()) { |
| 485 | package = package.append("."); |
| 486 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 487 | for (int i = 0; i < file->service_count(); ++i) { |
| 488 | auto service = file->service(i); |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 489 | auto package_qualified_service_name = package + service->name(); |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 490 | if (!(PrintBetaServicer(service, &out) && |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 491 | PrintBetaStub(service, &out) && |
| 492 | PrintBetaServerFactory(package_qualified_service_name, service, &out) && |
| 493 | PrintBetaStubFactory(package_qualified_service_name, service, &out))) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 494 | return make_pair(false, ""); |
| 495 | } |
| 496 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 497 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 498 | return make_pair(true, std::move(output)); |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | } // namespace grpc_python_generator |