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 | |
Masood Malekghassemi | 71448b2 | 2016-06-29 11:41:24 -0700 | [diff] [blame^] | 69 | GeneratorConfiguration::GeneratorConfiguration() |
| 70 | : grpc_package_root("grpc"), beta_package_root("grpc.beta") {} |
| 71 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 72 | PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) |
| 73 | : config_(config) {} |
| 74 | |
| 75 | PythonGrpcGenerator::~PythonGrpcGenerator() {} |
| 76 | |
| 77 | bool PythonGrpcGenerator::Generate( |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 78 | const FileDescriptor* file, const grpc::string& parameter, |
| 79 | GeneratorContext* context, grpc::string* error) const { |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 80 | // Get output file name. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 81 | grpc::string file_name; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 82 | static const int proto_suffix_length = strlen(".proto"); |
| 83 | if (file->name().size() > static_cast<size_t>(proto_suffix_length) && |
| 84 | file->name().find_last_of(".proto") == file->name().size() - 1) { |
| 85 | file_name = file->name().substr( |
| 86 | 0, file->name().size() - proto_suffix_length) + "_pb2.py"; |
| 87 | } else { |
| 88 | *error = "Invalid proto file name. Proto file must end with .proto"; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | std::unique_ptr<ZeroCopyOutputStream> output( |
| 93 | context->OpenForInsert(file_name, "module_scope")); |
| 94 | CodedOutputStream coded_out(output.get()); |
| 95 | bool success = false; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 96 | grpc::string code = ""; |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 97 | tie(success, code) = grpc_python_generator::GetServices(file, config_); |
| 98 | if (success) { |
| 99 | coded_out.WriteRaw(code.data(), code.size()); |
| 100 | return true; |
| 101 | } else { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 106 | namespace { |
| 107 | ////////////////////////////////// |
| 108 | // BEGIN FORMATTING BOILERPLATE // |
| 109 | ////////////////////////////////// |
| 110 | |
| 111 | // Converts an initializer list of the form { key0, value0, key1, value1, ... } |
| 112 | // 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] | 113 | map<grpc::string, grpc::string> ListToDict( |
| 114 | const initializer_list<grpc::string>& values) { |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 115 | assert(values.size() % 2 == 0); |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 116 | map<grpc::string, grpc::string> value_map; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 117 | auto value_iter = values.begin(); |
| 118 | for (unsigned i = 0; i < values.size()/2; ++i) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 119 | grpc::string key = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 120 | ++value_iter; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 121 | grpc::string value = *value_iter; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 122 | value_map[key] = value; |
| 123 | ++value_iter; |
| 124 | } |
| 125 | return value_map; |
| 126 | } |
| 127 | |
| 128 | // Provides RAII indentation handling. Use as: |
| 129 | // { |
| 130 | // IndentScope raii_my_indent_var_name_here(my_py_printer); |
| 131 | // // constructor indented my_py_printer |
| 132 | // ... |
| 133 | // // destructor called at end of scope, un-indenting my_py_printer |
| 134 | // } |
| 135 | class IndentScope { |
| 136 | public: |
| 137 | explicit IndentScope(Printer* printer) : printer_(printer) { |
| 138 | printer_->Indent(); |
| 139 | } |
| 140 | |
| 141 | ~IndentScope() { |
| 142 | printer_->Outdent(); |
| 143 | } |
| 144 | |
| 145 | private: |
| 146 | Printer* printer_; |
| 147 | }; |
| 148 | |
| 149 | //////////////////////////////// |
| 150 | // END FORMATTING BOILERPLATE // |
| 151 | //////////////////////////////// |
| 152 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 153 | // TODO(https://github.com/google/protobuf/issues/888): |
| 154 | // Export `ModuleName` from protobuf's |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 155 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 156 | grpc::string ModuleName(const grpc::string& filename) { |
| 157 | grpc::string basename = StripProto(filename); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 158 | basename = StringReplace(basename, "-", "_"); |
| 159 | basename = StringReplace(basename, "/", "."); |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 160 | return basename + "_pb2"; |
| 161 | } |
| 162 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 163 | // TODO(https://github.com/google/protobuf/issues/888): |
| 164 | // Export `ModuleAlias` from protobuf's |
| 165 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
| 166 | grpc::string ModuleAlias(const grpc::string& filename) { |
| 167 | grpc::string module_name = ModuleName(filename); |
| 168 | // We can't have dots in the module name, so we replace each with _dot_. |
| 169 | // But that could lead to a collision between a.b and a_dot_b, so we also |
| 170 | // duplicate each underscore. |
| 171 | module_name = StringReplace(module_name, "_", "__"); |
| 172 | module_name = StringReplace(module_name, ".", "_dot_"); |
| 173 | return module_name; |
| 174 | } |
| 175 | |
| 176 | |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 177 | bool GetModuleAndMessagePath(const Descriptor* type, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 178 | const ServiceDescriptor* service, |
| 179 | grpc::string* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 180 | const Descriptor* path_elem_type = type; |
| 181 | vector<const Descriptor*> message_path; |
| 182 | do { |
| 183 | message_path.push_back(path_elem_type); |
| 184 | path_elem_type = path_elem_type->containing_type(); |
Vijay Pai | c089743 | 2015-07-27 22:18:13 +0000 | [diff] [blame] | 185 | } while (path_elem_type); // implicit nullptr comparison; don't be explicit |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 186 | grpc::string file_name = type->file()->name(); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 187 | static const int proto_suffix_length = strlen(".proto"); |
| 188 | if (!(file_name.size() > static_cast<size_t>(proto_suffix_length) && |
| 189 | file_name.find_last_of(".proto") == file_name.size() - 1)) { |
| 190 | return false; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 191 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 192 | grpc::string service_file_name = service->file()->name(); |
| 193 | grpc::string module = service_file_name == file_name ? |
| 194 | "" : ModuleAlias(file_name) + "."; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 195 | grpc::string message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 196 | for (auto path_iter = message_path.rbegin(); |
| 197 | path_iter != message_path.rend(); ++path_iter) { |
| 198 | message_type += (*path_iter)->name() + "."; |
| 199 | } |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 200 | // no pop_back prior to C++11 |
| 201 | message_type.resize(message_type.size() - 1); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 202 | *out = module + message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 203 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 204 | } |
| 205 | |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 206 | // Get all comments (leading, leading_detached, trailing) and print them as a |
| 207 | // docstring. Any leading space of a line will be removed, but the line wrapping |
| 208 | // will not be changed. |
| 209 | template <typename DescriptorType> |
| 210 | static void PrintAllComments(const DescriptorType* desc, Printer* printer) { |
| 211 | std::vector<grpc::string> comments; |
| 212 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, |
| 213 | &comments); |
| 214 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING, |
| 215 | &comments); |
| 216 | grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING, |
| 217 | &comments); |
| 218 | if (comments.empty()) { |
| 219 | return; |
| 220 | } |
| 221 | printer->Print("\"\"\""); |
| 222 | for (auto it = comments.begin(); it != comments.end(); ++it) { |
| 223 | size_t start_pos = it->find_first_not_of(' '); |
| 224 | if (start_pos != grpc::string::npos) { |
| 225 | printer->Print(it->c_str() + start_pos); |
| 226 | } |
| 227 | printer->Print("\n"); |
| 228 | } |
| 229 | printer->Print("\"\"\"\n"); |
| 230 | } |
| 231 | |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 232 | bool PrintBetaServicer(const ServiceDescriptor* service, |
| 233 | Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 234 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 235 | out->Print("class Beta$Service$Servicer(object):\n", "Service", |
| 236 | service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 237 | { |
| 238 | IndentScope raii_class_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 239 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 240 | for (int i = 0; i < service->method_count(); ++i) { |
| 241 | auto meth = service->method(i); |
| 242 | grpc::string arg_name = meth->client_streaming() ? |
| 243 | "request_iterator" : "request"; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 244 | out->Print("def $Method$(self, $ArgName$, context):\n", |
| 245 | "Method", meth->name(), "ArgName", arg_name); |
| 246 | { |
| 247 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 248 | PrintAllComments(meth, out); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 249 | out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | bool PrintBetaStub(const ServiceDescriptor* service, |
| 257 | Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 258 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 259 | out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 260 | { |
| 261 | IndentScope raii_class_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 262 | PrintAllComments(service, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 263 | for (int i = 0; i < service->method_count(); ++i) { |
| 264 | const MethodDescriptor* meth = service->method(i); |
| 265 | grpc::string arg_name = meth->client_streaming() ? |
| 266 | "request_iterator" : "request"; |
| 267 | auto methdict = ListToDict({"Method", meth->name(), "ArgName", arg_name}); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 268 | 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] | 269 | { |
| 270 | IndentScope raii_method_indent(out); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 271 | PrintAllComments(meth, out); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 272 | out->Print("raise NotImplementedError()\n"); |
| 273 | } |
| 274 | if (!meth->server_streaming()) { |
| 275 | out->Print(methdict, "$Method$.future = None\n"); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | bool PrintBetaServerFactory(const grpc::string& package_qualified_service_name, |
| 283 | const ServiceDescriptor* service, Printer* out) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 284 | out->Print("\n\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 285 | out->Print("def beta_create_$Service$_server(servicer, pool=None, " |
| 286 | "pool_size=None, default_timeout=None, maximum_timeout=None):\n", |
| 287 | "Service", service->name()); |
| 288 | { |
| 289 | IndentScope raii_create_server_indent(out); |
| 290 | map<grpc::string, grpc::string> method_implementation_constructors; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 291 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 292 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 293 | for (int i = 0; i < service->method_count(); ++i) { |
| 294 | const MethodDescriptor* method = service->method(i); |
| 295 | const grpc::string method_implementation_constructor = |
| 296 | grpc::string(method->client_streaming() ? "stream_" : "unary_") + |
| 297 | grpc::string(method->server_streaming() ? "stream_" : "unary_") + |
| 298 | "inline"; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 299 | grpc::string input_message_module_and_class; |
| 300 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 301 | &input_message_module_and_class)) { |
| 302 | return false; |
| 303 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 304 | grpc::string output_message_module_and_class; |
| 305 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 306 | &output_message_module_and_class)) { |
| 307 | return false; |
| 308 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 309 | method_implementation_constructors.insert( |
| 310 | make_pair(method->name(), method_implementation_constructor)); |
| 311 | input_message_modules_and_classes.insert( |
| 312 | make_pair(method->name(), input_message_module_and_class)); |
| 313 | output_message_modules_and_classes.insert( |
| 314 | make_pair(method->name(), output_message_module_and_class)); |
| 315 | } |
| 316 | out->Print("request_deserializers = {\n"); |
| 317 | for (auto name_and_input_module_class_pair = |
| 318 | input_message_modules_and_classes.begin(); |
| 319 | name_and_input_module_class_pair != |
| 320 | input_message_modules_and_classes.end(); |
| 321 | name_and_input_module_class_pair++) { |
| 322 | IndentScope raii_indent(out); |
| 323 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 324 | "$InputTypeModuleAndClass$.FromString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 325 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 326 | "MethodName", name_and_input_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 327 | "InputTypeModuleAndClass", |
| 328 | name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 329 | } |
| 330 | out->Print("}\n"); |
| 331 | out->Print("response_serializers = {\n"); |
| 332 | for (auto name_and_output_module_class_pair = |
| 333 | output_message_modules_and_classes.begin(); |
| 334 | name_and_output_module_class_pair != |
| 335 | output_message_modules_and_classes.end(); |
| 336 | name_and_output_module_class_pair++) { |
| 337 | IndentScope raii_indent(out); |
| 338 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 339 | "$OutputTypeModuleAndClass$.SerializeToString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 340 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 341 | "MethodName", name_and_output_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 342 | "OutputTypeModuleAndClass", |
| 343 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 344 | } |
| 345 | out->Print("}\n"); |
| 346 | out->Print("method_implementations = {\n"); |
| 347 | for (auto name_and_implementation_constructor = |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 348 | method_implementation_constructors.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 349 | name_and_implementation_constructor != |
| 350 | method_implementation_constructors.end(); |
| 351 | name_and_implementation_constructor++) { |
| 352 | IndentScope raii_descriptions_indent(out); |
| 353 | const grpc::string method_name = |
| 354 | name_and_implementation_constructor->first; |
| 355 | out->Print("(\'$PackageQualifiedServiceName$\', \'$Method$\'): " |
| 356 | "face_utilities.$Constructor$(servicer.$Method$),\n", |
| 357 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 358 | "Method", name_and_implementation_constructor->first, |
| 359 | "Constructor", name_and_implementation_constructor->second); |
| 360 | } |
| 361 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 362 | out->Print("server_options = beta_implementations.server_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 363 | "request_deserializers=request_deserializers, " |
| 364 | "response_serializers=response_serializers, " |
| 365 | "thread_pool=pool, thread_pool_size=pool_size, " |
| 366 | "default_timeout=default_timeout, " |
| 367 | "maximum_timeout=maximum_timeout)\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 368 | out->Print("return beta_implementations.server(method_implementations, " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 369 | "options=server_options)\n"); |
| 370 | } |
| 371 | return true; |
| 372 | } |
| 373 | |
| 374 | bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, |
| 375 | const ServiceDescriptor* service, Printer* out) { |
| 376 | map<grpc::string, grpc::string> dict = ListToDict({ |
| 377 | "Service", service->name(), |
| 378 | }); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 379 | out->Print("\n\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 380 | out->Print(dict, "def beta_create_$Service$_stub(channel, host=None," |
| 381 | " metadata_transformer=None, pool=None, pool_size=None):\n"); |
| 382 | { |
| 383 | IndentScope raii_create_server_indent(out); |
| 384 | map<grpc::string, grpc::string> method_cardinalities; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 385 | map<grpc::string, grpc::string> input_message_modules_and_classes; |
| 386 | map<grpc::string, grpc::string> output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 387 | for (int i = 0; i < service->method_count(); ++i) { |
| 388 | const MethodDescriptor* method = service->method(i); |
| 389 | const grpc::string method_cardinality = |
| 390 | grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + |
| 391 | "_" + |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 392 | grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); |
| 393 | grpc::string input_message_module_and_class; |
| 394 | if (!GetModuleAndMessagePath(method->input_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 395 | &input_message_module_and_class)) { |
| 396 | return false; |
| 397 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 398 | grpc::string output_message_module_and_class; |
| 399 | if (!GetModuleAndMessagePath(method->output_type(), service, |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 400 | &output_message_module_and_class)) { |
| 401 | return false; |
| 402 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 403 | method_cardinalities.insert( |
| 404 | make_pair(method->name(), method_cardinality)); |
| 405 | input_message_modules_and_classes.insert( |
| 406 | make_pair(method->name(), input_message_module_and_class)); |
| 407 | output_message_modules_and_classes.insert( |
| 408 | make_pair(method->name(), output_message_module_and_class)); |
| 409 | } |
| 410 | out->Print("request_serializers = {\n"); |
| 411 | for (auto name_and_input_module_class_pair = |
| 412 | input_message_modules_and_classes.begin(); |
| 413 | name_and_input_module_class_pair != |
| 414 | input_message_modules_and_classes.end(); |
| 415 | name_and_input_module_class_pair++) { |
| 416 | IndentScope raii_indent(out); |
| 417 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 418 | "$InputTypeModuleAndClass$.SerializeToString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 419 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 420 | "MethodName", name_and_input_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 421 | "InputTypeModuleAndClass", |
| 422 | name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 423 | } |
| 424 | out->Print("}\n"); |
| 425 | out->Print("response_deserializers = {\n"); |
| 426 | for (auto name_and_output_module_class_pair = |
| 427 | output_message_modules_and_classes.begin(); |
| 428 | name_and_output_module_class_pair != |
| 429 | output_message_modules_and_classes.end(); |
| 430 | name_and_output_module_class_pair++) { |
| 431 | IndentScope raii_indent(out); |
| 432 | out->Print("(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 433 | "$OutputTypeModuleAndClass$.FromString,\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 434 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 435 | "MethodName", name_and_output_module_class_pair->first, |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 436 | "OutputTypeModuleAndClass", |
| 437 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 438 | } |
| 439 | out->Print("}\n"); |
| 440 | out->Print("cardinalities = {\n"); |
| 441 | for (auto name_and_cardinality = method_cardinalities.begin(); |
| 442 | name_and_cardinality != method_cardinalities.end(); |
| 443 | name_and_cardinality++) { |
| 444 | IndentScope raii_descriptions_indent(out); |
| 445 | out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n", |
| 446 | "Method", name_and_cardinality->first, |
| 447 | "Cardinality", name_and_cardinality->second); |
| 448 | } |
| 449 | out->Print("}\n"); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 450 | out->Print("stub_options = beta_implementations.stub_options(" |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 451 | "host=host, metadata_transformer=metadata_transformer, " |
| 452 | "request_serializers=request_serializers, " |
| 453 | "response_deserializers=response_deserializers, " |
| 454 | "thread_pool=pool, thread_pool_size=pool_size)\n"); |
| 455 | out->Print( |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 456 | "return beta_implementations.dynamic_stub(channel, \'$PackageQualifiedServiceName$\', " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 457 | "cardinalities, options=stub_options)\n", |
| 458 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 463 | bool PrintStub(const grpc::string& package_qualified_service_name, |
| 464 | const ServiceDescriptor* service, Printer* out) { |
| 465 | out->Print("\n\n"); |
| 466 | out->Print("class $Service$Stub(object):\n", "Service", service->name()); |
| 467 | { |
| 468 | IndentScope raii_class_indent(out); |
| 469 | PrintAllComments(service, out); |
| 470 | out->Print("\n"); |
| 471 | out->Print("def __init__(self, channel):\n"); |
| 472 | { |
| 473 | IndentScope raii_init_indent(out); |
| 474 | out->Print("\"\"\"Constructor.\n"); |
| 475 | out->Print("\n"); |
| 476 | out->Print("Args:\n"); |
| 477 | { |
| 478 | IndentScope raii_args_indent(out); |
| 479 | out->Print("channel: A grpc.Channel.\n"); |
| 480 | } |
| 481 | out->Print("\"\"\"\n"); |
| 482 | for (int i = 0; i < service->method_count(); ++i) { |
| 483 | auto method = service->method(i); |
| 484 | auto multi_callable_constructor = |
| 485 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
| 486 | "_" + |
| 487 | grpc::string(method->server_streaming() ? "stream" : "unary"); |
| 488 | grpc::string request_module_and_class; |
| 489 | if (!GetModuleAndMessagePath(method->input_type(), service, |
| 490 | &request_module_and_class)) { |
| 491 | return false; |
| 492 | } |
| 493 | grpc::string response_module_and_class; |
| 494 | if (!GetModuleAndMessagePath(method->output_type(), service, |
| 495 | &response_module_and_class)) { |
| 496 | return false; |
| 497 | } |
| 498 | out->Print("self.$Method$ = channel.$MultiCallableConstructor$(\n", |
| 499 | "Method", method->name(), |
| 500 | "MultiCallableConstructor", multi_callable_constructor); |
| 501 | { |
| 502 | IndentScope raii_first_attribute_indent(out); |
| 503 | IndentScope raii_second_attribute_indent(out); |
| 504 | out->Print( |
| 505 | "'/$PackageQualifiedService$/$Method$',\n", |
| 506 | "PackageQualifiedService", package_qualified_service_name, |
| 507 | "Method", method->name()); |
| 508 | out->Print( |
| 509 | "request_serializer=$RequestModuleAndClass$.SerializeToString,\n", |
| 510 | "RequestModuleAndClass", request_module_and_class); |
| 511 | out->Print( |
| 512 | "response_deserializer=$ResponseModuleAndClass$.FromString,\n", |
| 513 | "ResponseModuleAndClass", response_module_and_class); |
| 514 | out->Print(")\n"); |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | return true; |
| 520 | } |
| 521 | |
| 522 | bool PrintServicer(const ServiceDescriptor* service, Printer* out) { |
| 523 | out->Print("\n\n"); |
| 524 | out->Print("class $Service$Servicer(object):\n", "Service", service->name()); |
| 525 | { |
| 526 | IndentScope raii_class_indent(out); |
| 527 | PrintAllComments(service, out); |
| 528 | for (int i = 0; i < service->method_count(); ++i) { |
| 529 | auto method = service->method(i); |
| 530 | grpc::string arg_name = method->client_streaming() ? |
| 531 | "request_iterator" : "request"; |
| 532 | out->Print("\n"); |
| 533 | out->Print("def $Method$(self, $ArgName$, context):\n", |
| 534 | "Method", method->name(), "ArgName", arg_name); |
| 535 | { |
| 536 | IndentScope raii_method_indent(out); |
| 537 | PrintAllComments(method, out); |
| 538 | out->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n"); |
| 539 | out->Print("context.set_details('Method not implemented!')\n"); |
| 540 | out->Print("raise NotImplementedError('Method not implemented!')\n"); |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | bool PrintAddServicerToServer(const grpc::string& package_qualified_service_name, |
| 548 | const ServiceDescriptor* service, Printer* out) { |
| 549 | out->Print("\n\n"); |
| 550 | out->Print("def add_$Service$Servicer_to_server(servicer, server):\n", |
| 551 | "Service", service->name()); |
| 552 | { |
| 553 | IndentScope raii_class_indent(out); |
| 554 | out->Print("rpc_method_handlers = {\n"); |
| 555 | { |
| 556 | IndentScope raii_dict_first_indent(out); |
| 557 | IndentScope raii_dict_second_indent(out); |
| 558 | for (int i = 0; i < service->method_count(); ++i) { |
| 559 | auto method = service->method(i); |
| 560 | auto method_handler_constructor = |
| 561 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
| 562 | "_" + |
| 563 | grpc::string(method->server_streaming() ? "stream" : "unary") + |
| 564 | "_rpc_method_handler"; |
| 565 | grpc::string request_module_and_class; |
| 566 | if (!GetModuleAndMessagePath(method->input_type(), service, |
| 567 | &request_module_and_class)) { |
| 568 | return false; |
| 569 | } |
| 570 | grpc::string response_module_and_class; |
| 571 | if (!GetModuleAndMessagePath(method->output_type(), service, |
| 572 | &response_module_and_class)) { |
| 573 | return false; |
| 574 | } |
| 575 | out->Print("'$Method$': grpc.$MethodHandlerConstructor$(\n", |
| 576 | "Method", method->name(), |
| 577 | "MethodHandlerConstructor", method_handler_constructor); |
| 578 | { |
| 579 | IndentScope raii_call_first_indent(out); |
| 580 | IndentScope raii_call_second_indent(out); |
| 581 | out->Print("servicer.$Method$,\n", "Method", method->name()); |
| 582 | out->Print("request_deserializer=$RequestModuleAndClass$.FromString,\n", |
| 583 | "RequestModuleAndClass", request_module_and_class); |
| 584 | out->Print("response_serializer=$ResponseModuleAndClass$.SerializeToString,\n", |
| 585 | "ResponseModuleAndClass", response_module_and_class); |
| 586 | } |
| 587 | out->Print("),\n"); |
| 588 | } |
| 589 | } |
| 590 | out->Print("}\n"); |
| 591 | out->Print("generic_handler = grpc.method_handlers_generic_handler(\n"); |
| 592 | { |
| 593 | IndentScope raii_call_first_indent(out); |
| 594 | IndentScope raii_call_second_indent(out); |
| 595 | out->Print("'$PackageQualifiedServiceName$', rpc_method_handlers)\n", |
| 596 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 597 | } |
| 598 | out->Print("server.add_generic_rpc_handlers((generic_handler,))\n"); |
| 599 | } |
| 600 | return true; |
| 601 | } |
| 602 | |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 603 | bool PrintPreamble(const FileDescriptor* file, |
| 604 | const GeneratorConfiguration& config, Printer* out) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 605 | out->Print("import $Package$\n", "Package", config.grpc_package_root); |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 606 | out->Print("from $Package$ import implementations as beta_implementations\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 607 | "Package", config.beta_package_root); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 608 | out->Print("from $Package$ import interfaces as beta_interfaces\n", |
| 609 | "Package", config.beta_package_root); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 610 | out->Print("from grpc.framework.common import cardinality\n"); |
| 611 | 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] | 612 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | } // namespace |
| 616 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 617 | pair<bool, grpc::string> GetServices(const FileDescriptor* file, |
Nathaniel Manista | dc8c323 | 2016-01-16 18:28:45 +0000 | [diff] [blame] | 618 | const GeneratorConfiguration& config) { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 619 | grpc::string output; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 620 | { |
| 621 | // Scope the output stream so it closes and finalizes output to the string. |
| 622 | StringOutputStream output_stream(&output); |
| 623 | Printer out(&output_stream, '$'); |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 624 | if (!PrintPreamble(file, config, &out)) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 625 | return make_pair(false, ""); |
| 626 | } |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 627 | auto package = file->package(); |
| 628 | if (!package.empty()) { |
| 629 | package = package.append("."); |
| 630 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 631 | for (int i = 0; i < file->service_count(); ++i) { |
| 632 | auto service = file->service(i); |
Nathaniel Manista | c4fada6 | 2015-03-13 22:23:59 +0000 | [diff] [blame] | 633 | auto package_qualified_service_name = package + service->name(); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 634 | if (!(PrintStub(package_qualified_service_name, service, &out) && |
| 635 | PrintServicer(service, &out) && |
| 636 | PrintAddServicerToServer(package_qualified_service_name, service, &out) && |
| 637 | PrintBetaServicer(service, &out) && |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 638 | PrintBetaStub(service, &out) && |
| 639 | PrintBetaServerFactory(package_qualified_service_name, service, &out) && |
| 640 | PrintBetaStubFactory(package_qualified_service_name, service, &out))) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 641 | return make_pair(false, ""); |
| 642 | } |
| 643 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 644 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 645 | return make_pair(true, std::move(output)); |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | } // namespace grpc_python_generator |