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 | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 38 | #include <fstream> |
| 39 | #include <iostream> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 40 | #include <map> |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 41 | #include <memory> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 42 | #include <ostream> |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 43 | #include <set> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 44 | #include <sstream> |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 45 | #include <tuple> |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 46 | #include <vector> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 47 | |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 48 | #include "src/compiler/config.h" |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 49 | #include "src/compiler/generator_helpers.h" |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 50 | #include "src/compiler/python_generator.h" |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 51 | |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 52 | using grpc_generator::StringReplace; |
| 53 | using grpc_generator::StripProto; |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 54 | using grpc::protobuf::Descriptor; |
| 55 | using grpc::protobuf::FileDescriptor; |
| 56 | using grpc::protobuf::MethodDescriptor; |
| 57 | using grpc::protobuf::ServiceDescriptor; |
| 58 | using grpc::protobuf::compiler::GeneratorContext; |
| 59 | using grpc::protobuf::io::CodedOutputStream; |
| 60 | using grpc::protobuf::io::Printer; |
| 61 | using grpc::protobuf::io::StringOutputStream; |
| 62 | using grpc::protobuf::io::ZeroCopyOutputStream; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 63 | using std::initializer_list; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 64 | using std::make_pair; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 65 | using std::map; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 66 | using std::pair; |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 67 | using std::replace; |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 68 | using std::tuple; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 69 | using std::vector; |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 70 | using std::set; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 71 | |
| 72 | namespace grpc_python_generator { |
Masood Malekghassemi | 3bb5215 | 2015-03-17 21:52:52 -0700 | [diff] [blame] | 73 | |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 74 | namespace { |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 75 | |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 76 | typedef vector<const Descriptor*> DescriptorVector; |
| 77 | typedef map<grpc::string, grpc::string> StringMap; |
| 78 | typedef vector<grpc::string> StringVector; |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 79 | typedef tuple<grpc::string, grpc::string> StringPair; |
| 80 | typedef set<StringPair> StringPairSet; |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 81 | |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 82 | // Provides RAII indentation handling. Use as: |
| 83 | // { |
| 84 | // IndentScope raii_my_indent_var_name_here(my_py_printer); |
| 85 | // // constructor indented my_py_printer |
| 86 | // ... |
| 87 | // // destructor called at end of scope, un-indenting my_py_printer |
| 88 | // } |
| 89 | class IndentScope { |
| 90 | public: |
| 91 | explicit IndentScope(Printer* printer) : printer_(printer) { |
| 92 | printer_->Indent(); |
| 93 | } |
| 94 | |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 95 | ~IndentScope() { printer_->Outdent(); } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | Printer* printer_; |
| 99 | }; |
| 100 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 101 | // TODO(https://github.com/google/protobuf/issues/888): |
| 102 | // Export `ModuleName` from protobuf's |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 103 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 104 | grpc::string ModuleName(const grpc::string& filename) { |
| 105 | grpc::string basename = StripProto(filename); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 106 | basename = StringReplace(basename, "-", "_"); |
| 107 | basename = StringReplace(basename, "/", "."); |
Masood Malekghassemi | 40e8cbd | 2015-02-26 08:39:50 -0800 | [diff] [blame] | 108 | return basename + "_pb2"; |
| 109 | } |
| 110 | |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 111 | // TODO(https://github.com/google/protobuf/issues/888): |
| 112 | // Export `ModuleAlias` from protobuf's |
| 113 | // `src/google/protobuf/compiler/python/python_generator.cc` file. |
| 114 | grpc::string ModuleAlias(const grpc::string& filename) { |
| 115 | grpc::string module_name = ModuleName(filename); |
| 116 | // We can't have dots in the module name, so we replace each with _dot_. |
| 117 | // But that could lead to a collision between a.b and a_dot_b, so we also |
| 118 | // duplicate each underscore. |
| 119 | module_name = StringReplace(module_name, "_", "__"); |
| 120 | module_name = StringReplace(module_name, ".", "_dot_"); |
| 121 | return module_name; |
| 122 | } |
| 123 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 124 | // Tucks all generator state in an anonymous namespace away from |
| 125 | // PythonGrpcGenerator and the header file, mostly to encourage future changes |
| 126 | // to not require updates to the grpcio-tools C++ code part. Assumes that it is |
| 127 | // only ever used from a single thread. |
| 128 | struct PrivateGenerator { |
| 129 | const GeneratorConfiguration& config; |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 130 | const FileDescriptor* file; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 131 | |
| 132 | bool generate_in_pb2_grpc; |
| 133 | |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 134 | Printer* out; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 135 | |
| 136 | PrivateGenerator(const GeneratorConfiguration& config, |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 137 | const FileDescriptor* file); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 138 | |
| 139 | std::pair<bool, grpc::string> GetGrpcServices(); |
| 140 | |
| 141 | private: |
| 142 | bool PrintPreamble(); |
| 143 | bool PrintBetaPreamble(); |
| 144 | bool PrintGAServices(); |
| 145 | bool PrintBetaServices(); |
| 146 | |
| 147 | bool PrintAddServicerToServer( |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 148 | const grpc::string& package_qualified_service_name, |
| 149 | const ServiceDescriptor* service); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 150 | bool PrintServicer(const ServiceDescriptor* service); |
| 151 | bool PrintStub(const grpc::string& package_qualified_service_name, |
| 152 | const ServiceDescriptor* service); |
| 153 | |
| 154 | bool PrintBetaServicer(const ServiceDescriptor* service); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 155 | bool PrintBetaServerFactory( |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 156 | const grpc::string& package_qualified_service_name, |
| 157 | const ServiceDescriptor* service); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 158 | bool PrintBetaStub(const ServiceDescriptor* service); |
| 159 | bool PrintBetaStubFactory(const grpc::string& package_qualified_service_name, |
| 160 | const ServiceDescriptor* service); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 161 | |
| 162 | // Get all comments (leading, leading_detached, trailing) and print them as a |
| 163 | // docstring. Any leading space of a line will be removed, but the line |
| 164 | // wrapping will not be changed. |
| 165 | template <typename DescriptorType> |
| 166 | void PrintAllComments(const DescriptorType* descriptor); |
| 167 | |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 168 | bool GetModuleAndMessagePath(const Descriptor* type, grpc::string* out); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 171 | PrivateGenerator::PrivateGenerator(const GeneratorConfiguration& config, |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 172 | const FileDescriptor* file) |
| 173 | : config(config), file(file) {} |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 174 | |
| 175 | bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type, |
| 176 | grpc::string* out) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 177 | const Descriptor* path_elem_type = type; |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 178 | DescriptorVector message_path; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 179 | do { |
| 180 | message_path.push_back(path_elem_type); |
| 181 | path_elem_type = path_elem_type->containing_type(); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [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 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 189 | grpc::string generator_file_name = file->name(); |
| 190 | grpc::string module; |
| 191 | if (generator_file_name != file_name || generate_in_pb2_grpc) { |
| 192 | module = ModuleAlias(file_name) + "."; |
| 193 | } else { |
| 194 | module = ""; |
| 195 | } |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 196 | grpc::string message_type; |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 197 | for (DescriptorVector::reverse_iterator path_iter = message_path.rbegin(); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 198 | path_iter != message_path.rend(); ++path_iter) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 199 | message_type += (*path_iter)->name() + "."; |
| 200 | } |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 201 | // no pop_back prior to C++11 |
| 202 | message_type.resize(message_type.size() - 1); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 203 | *out = module + message_type; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 204 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 205 | } |
| 206 | |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 207 | template <typename DescriptorType> |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 208 | void PrivateGenerator::PrintAllComments(const DescriptorType* descriptor) { |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 209 | StringVector comments; |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 210 | grpc_generator::GetComment( |
| 211 | descriptor, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &comments); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 212 | grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_LEADING, |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 213 | &comments); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 214 | grpc_generator::GetComment(descriptor, grpc_generator::COMMENTTYPE_TRAILING, |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 215 | &comments); |
| 216 | if (comments.empty()) { |
| 217 | return; |
| 218 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 219 | out->Print("\"\"\""); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 220 | for (StringVector::iterator it = comments.begin(); it != comments.end(); |
| 221 | ++it) { |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 222 | size_t start_pos = it->find_first_not_of(' '); |
| 223 | if (start_pos != grpc::string::npos) { |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 224 | out->Print(it->c_str() + start_pos); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 225 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 226 | out->Print("\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 227 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 228 | out->Print("\"\"\"\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 231 | bool PrivateGenerator::PrintBetaServicer(const ServiceDescriptor* service) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 232 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 233 | out->Print("class Beta$Service$Servicer(object):\n", "Service", |
| 234 | service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 235 | { |
| 236 | IndentScope raii_class_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame] | 237 | out->Print( |
| 238 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 239 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 240 | "file not marked beta) for all further purposes. This class was " |
| 241 | "generated\n" |
| 242 | "only to ease transition from grpcio<0.15.0 to " |
| 243 | "grpcio>=0.15.0.\"\"\"\n"); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 244 | PrintAllComments(service); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 245 | for (int i = 0; i < service->method_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 246 | const MethodDescriptor* method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 247 | grpc::string arg_name = |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 248 | method->client_streaming() ? "request_iterator" : "request"; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 249 | out->Print("def $Method$(self, $ArgName$, context):\n", "Method", |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 250 | method->name(), "ArgName", arg_name); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 251 | { |
| 252 | IndentScope raii_method_indent(out); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 253 | PrintAllComments(method); |
Masood Malekghassemi | 832ae81 | 2016-04-27 18:38:54 -0700 | [diff] [blame] | 254 | out->Print("context.code(beta_interfaces.StatusCode.UNIMPLEMENTED)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | } |
| 258 | return true; |
| 259 | } |
| 260 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 261 | bool PrivateGenerator::PrintBetaStub(const ServiceDescriptor* service) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 262 | out->Print("\n\n"); |
yang-g | b0de716 | 2016-05-03 15:48:19 -0700 | [diff] [blame] | 263 | out->Print("class Beta$Service$Stub(object):\n", "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 264 | { |
| 265 | IndentScope raii_class_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame] | 266 | out->Print( |
| 267 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 268 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 269 | "file not marked beta) for all further purposes. This class was " |
| 270 | "generated\n" |
| 271 | "only to ease transition from grpcio<0.15.0 to " |
| 272 | "grpcio>=0.15.0.\"\"\"\n"); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 273 | PrintAllComments(service); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 274 | for (int i = 0; i < service->method_count(); ++i) { |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 275 | const MethodDescriptor* method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 276 | grpc::string arg_name = |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 277 | method->client_streaming() ? "request_iterator" : "request"; |
| 278 | StringMap method_dict; |
| 279 | method_dict["Method"] = method->name(); |
| 280 | method_dict["ArgName"] = arg_name; |
| 281 | out->Print(method_dict, |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 282 | "def $Method$(self, $ArgName$, timeout, metadata=None, " |
| 283 | "with_call=False, protocol_options=None):\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 284 | { |
| 285 | IndentScope raii_method_indent(out); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 286 | PrintAllComments(method); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 287 | out->Print("raise NotImplementedError()\n"); |
| 288 | } |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 289 | if (!method->server_streaming()) { |
| 290 | out->Print(method_dict, "$Method$.future = None\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | return true; |
| 295 | } |
| 296 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 297 | bool PrivateGenerator::PrintBetaServerFactory( |
| 298 | const grpc::string& package_qualified_service_name, |
| 299 | const ServiceDescriptor* service) { |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 300 | out->Print("\n\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 301 | out->Print( |
| 302 | "def beta_create_$Service$_server(servicer, pool=None, " |
| 303 | "pool_size=None, default_timeout=None, maximum_timeout=None):\n", |
| 304 | "Service", service->name()); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 305 | { |
| 306 | IndentScope raii_create_server_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame] | 307 | out->Print( |
| 308 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 309 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 310 | "file not marked beta) for all further purposes. This function was\n" |
| 311 | "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" |
| 312 | "\"\"\"\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 313 | StringMap method_implementation_constructors; |
| 314 | StringMap input_message_modules_and_classes; |
| 315 | StringMap output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 316 | for (int i = 0; i < service->method_count(); ++i) { |
| 317 | const MethodDescriptor* method = service->method(i); |
| 318 | const grpc::string method_implementation_constructor = |
| 319 | grpc::string(method->client_streaming() ? "stream_" : "unary_") + |
| 320 | grpc::string(method->server_streaming() ? "stream_" : "unary_") + |
| 321 | "inline"; |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 322 | grpc::string input_message_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 323 | if (!GetModuleAndMessagePath(method->input_type(), |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 324 | &input_message_module_and_class)) { |
| 325 | return false; |
| 326 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 327 | grpc::string output_message_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 328 | if (!GetModuleAndMessagePath(method->output_type(), |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 329 | &output_message_module_and_class)) { |
| 330 | return false; |
| 331 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 332 | method_implementation_constructors.insert( |
| 333 | make_pair(method->name(), method_implementation_constructor)); |
| 334 | input_message_modules_and_classes.insert( |
| 335 | make_pair(method->name(), input_message_module_and_class)); |
| 336 | output_message_modules_and_classes.insert( |
| 337 | make_pair(method->name(), output_message_module_and_class)); |
| 338 | } |
| 339 | out->Print("request_deserializers = {\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 340 | for (StringMap::iterator name_and_input_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 341 | input_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 342 | name_and_input_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 343 | input_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 344 | name_and_input_module_class_pair++) { |
| 345 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 346 | out->Print( |
| 347 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 348 | "$InputTypeModuleAndClass$.FromString,\n", |
| 349 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 350 | "MethodName", name_and_input_module_class_pair->first, |
| 351 | "InputTypeModuleAndClass", name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 352 | } |
| 353 | out->Print("}\n"); |
| 354 | out->Print("response_serializers = {\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 355 | for (StringMap::iterator name_and_output_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 356 | output_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 357 | name_and_output_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 358 | output_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 359 | name_and_output_module_class_pair++) { |
| 360 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 361 | out->Print( |
| 362 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 363 | "$OutputTypeModuleAndClass$.SerializeToString,\n", |
| 364 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 365 | "MethodName", name_and_output_module_class_pair->first, |
| 366 | "OutputTypeModuleAndClass", |
| 367 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 368 | } |
| 369 | out->Print("}\n"); |
| 370 | out->Print("method_implementations = {\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 371 | for (StringMap::iterator name_and_implementation_constructor = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 372 | method_implementation_constructors.begin(); |
| 373 | name_and_implementation_constructor != |
| 374 | method_implementation_constructors.end(); |
| 375 | name_and_implementation_constructor++) { |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 376 | IndentScope raii_descriptions_indent(out); |
| 377 | const grpc::string method_name = |
| 378 | name_and_implementation_constructor->first; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 379 | out->Print( |
| 380 | "(\'$PackageQualifiedServiceName$\', \'$Method$\'): " |
| 381 | "face_utilities.$Constructor$(servicer.$Method$),\n", |
| 382 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 383 | "Method", name_and_implementation_constructor->first, "Constructor", |
| 384 | name_and_implementation_constructor->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 385 | } |
| 386 | out->Print("}\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 387 | out->Print( |
| 388 | "server_options = beta_implementations.server_options(" |
| 389 | "request_deserializers=request_deserializers, " |
| 390 | "response_serializers=response_serializers, " |
| 391 | "thread_pool=pool, thread_pool_size=pool_size, " |
| 392 | "default_timeout=default_timeout, " |
| 393 | "maximum_timeout=maximum_timeout)\n"); |
| 394 | out->Print( |
| 395 | "return beta_implementations.server(method_implementations, " |
| 396 | "options=server_options)\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 397 | } |
| 398 | return true; |
| 399 | } |
| 400 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 401 | bool PrivateGenerator::PrintBetaStubFactory( |
| 402 | const grpc::string& package_qualified_service_name, |
| 403 | const ServiceDescriptor* service) { |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 404 | StringMap dict; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 405 | dict["Service"] = service->name(); |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 406 | out->Print("\n\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 407 | out->Print(dict, |
| 408 | "def beta_create_$Service$_stub(channel, host=None," |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 409 | " metadata_transformer=None, pool=None, pool_size=None):\n"); |
| 410 | { |
| 411 | IndentScope raii_create_server_indent(out); |
Nicolas "Pixel" Noble | beeeddd | 2016-09-29 20:27:23 +0200 | [diff] [blame] | 412 | out->Print( |
| 413 | "\"\"\"The Beta API is deprecated for 0.15.0 and later.\n" |
| 414 | "\nIt is recommended to use the GA API (classes and functions in this\n" |
| 415 | "file not marked beta) for all further purposes. This function was\n" |
| 416 | "generated only to ease transition from grpcio<0.15.0 to grpcio>=0.15.0" |
| 417 | "\"\"\"\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 418 | StringMap method_cardinalities; |
| 419 | StringMap input_message_modules_and_classes; |
| 420 | StringMap output_message_modules_and_classes; |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 421 | for (int i = 0; i < service->method_count(); ++i) { |
| 422 | const MethodDescriptor* method = service->method(i); |
| 423 | const grpc::string method_cardinality = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 424 | grpc::string(method->client_streaming() ? "STREAM" : "UNARY") + "_" + |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 425 | grpc::string(method->server_streaming() ? "STREAM" : "UNARY"); |
| 426 | grpc::string input_message_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 427 | if (!GetModuleAndMessagePath(method->input_type(), |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 428 | &input_message_module_and_class)) { |
| 429 | return false; |
| 430 | } |
Ken Payson | be187b0 | 2016-05-06 15:32:58 -0700 | [diff] [blame] | 431 | grpc::string output_message_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 432 | if (!GetModuleAndMessagePath(method->output_type(), |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 433 | &output_message_module_and_class)) { |
| 434 | return false; |
| 435 | } |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 436 | method_cardinalities.insert( |
| 437 | make_pair(method->name(), method_cardinality)); |
| 438 | input_message_modules_and_classes.insert( |
| 439 | make_pair(method->name(), input_message_module_and_class)); |
| 440 | output_message_modules_and_classes.insert( |
| 441 | make_pair(method->name(), output_message_module_and_class)); |
| 442 | } |
| 443 | out->Print("request_serializers = {\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 444 | for (StringMap::iterator name_and_input_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 445 | input_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 446 | name_and_input_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 447 | input_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 448 | name_and_input_module_class_pair++) { |
| 449 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 450 | out->Print( |
| 451 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 452 | "$InputTypeModuleAndClass$.SerializeToString,\n", |
| 453 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 454 | "MethodName", name_and_input_module_class_pair->first, |
| 455 | "InputTypeModuleAndClass", name_and_input_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 456 | } |
| 457 | out->Print("}\n"); |
| 458 | out->Print("response_deserializers = {\n"); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 459 | for (StringMap::iterator name_and_output_module_class_pair = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 460 | output_message_modules_and_classes.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 461 | name_and_output_module_class_pair != |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 462 | output_message_modules_and_classes.end(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 463 | name_and_output_module_class_pair++) { |
| 464 | IndentScope raii_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 465 | out->Print( |
| 466 | "(\'$PackageQualifiedServiceName$\', \'$MethodName$\'): " |
| 467 | "$OutputTypeModuleAndClass$.FromString,\n", |
| 468 | "PackageQualifiedServiceName", package_qualified_service_name, |
| 469 | "MethodName", name_and_output_module_class_pair->first, |
| 470 | "OutputTypeModuleAndClass", |
| 471 | name_and_output_module_class_pair->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 472 | } |
| 473 | out->Print("}\n"); |
| 474 | out->Print("cardinalities = {\n"); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 475 | for (StringMap::iterator name_and_cardinality = |
| 476 | method_cardinalities.begin(); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 477 | name_and_cardinality != method_cardinalities.end(); |
| 478 | name_and_cardinality++) { |
| 479 | IndentScope raii_descriptions_indent(out); |
| 480 | out->Print("\'$Method$\': cardinality.Cardinality.$Cardinality$,\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 481 | "Method", name_and_cardinality->first, "Cardinality", |
| 482 | name_and_cardinality->second); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 483 | } |
| 484 | out->Print("}\n"); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 485 | out->Print( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 486 | "stub_options = beta_implementations.stub_options(" |
| 487 | "host=host, metadata_transformer=metadata_transformer, " |
| 488 | "request_serializers=request_serializers, " |
| 489 | "response_deserializers=response_deserializers, " |
| 490 | "thread_pool=pool, thread_pool_size=pool_size)\n"); |
| 491 | out->Print( |
| 492 | "return beta_implementations.dynamic_stub(channel, " |
| 493 | "\'$PackageQualifiedServiceName$\', " |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 494 | "cardinalities, options=stub_options)\n", |
| 495 | "PackageQualifiedServiceName", package_qualified_service_name); |
| 496 | } |
| 497 | return true; |
| 498 | } |
| 499 | |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 500 | bool PrivateGenerator::PrintStub( |
| 501 | const grpc::string& package_qualified_service_name, |
| 502 | const ServiceDescriptor* service) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 503 | out->Print("\n\n"); |
| 504 | out->Print("class $Service$Stub(object):\n", "Service", service->name()); |
| 505 | { |
| 506 | IndentScope raii_class_indent(out); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 507 | PrintAllComments(service); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 508 | out->Print("\n"); |
| 509 | out->Print("def __init__(self, channel):\n"); |
| 510 | { |
| 511 | IndentScope raii_init_indent(out); |
| 512 | out->Print("\"\"\"Constructor.\n"); |
| 513 | out->Print("\n"); |
| 514 | out->Print("Args:\n"); |
| 515 | { |
| 516 | IndentScope raii_args_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 517 | out->Print("channel: A grpc.Channel.\n"); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 518 | } |
| 519 | out->Print("\"\"\"\n"); |
| 520 | for (int i = 0; i < service->method_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 521 | const MethodDescriptor* method = service->method(i); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 522 | grpc::string multi_callable_constructor = |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 523 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
| 524 | "_" + grpc::string(method->server_streaming() ? "stream" : "unary"); |
| 525 | grpc::string request_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 526 | if (!GetModuleAndMessagePath(method->input_type(), |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 527 | &request_module_and_class)) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 528 | return false; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 529 | } |
| 530 | grpc::string response_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 531 | if (!GetModuleAndMessagePath(method->output_type(), |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 532 | &response_module_and_class)) { |
| 533 | return false; |
| 534 | } |
| 535 | out->Print("self.$Method$ = channel.$MultiCallableConstructor$(\n", |
| 536 | "Method", method->name(), "MultiCallableConstructor", |
| 537 | multi_callable_constructor); |
| 538 | { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 539 | IndentScope raii_first_attribute_indent(out); |
| 540 | IndentScope raii_second_attribute_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 541 | out->Print("'/$PackageQualifiedService$/$Method$',\n", |
| 542 | "PackageQualifiedService", package_qualified_service_name, |
| 543 | "Method", method->name()); |
| 544 | out->Print( |
| 545 | "request_serializer=$RequestModuleAndClass$.SerializeToString,\n", |
| 546 | "RequestModuleAndClass", request_module_and_class); |
| 547 | out->Print( |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 548 | "response_deserializer=$ResponseModuleAndClass$.FromString,\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 549 | "ResponseModuleAndClass", response_module_and_class); |
| 550 | out->Print(")\n"); |
| 551 | } |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | return true; |
| 556 | } |
| 557 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 558 | bool PrivateGenerator::PrintServicer(const ServiceDescriptor* service) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 559 | out->Print("\n\n"); |
| 560 | out->Print("class $Service$Servicer(object):\n", "Service", service->name()); |
| 561 | { |
| 562 | IndentScope raii_class_indent(out); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 563 | PrintAllComments(service); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 564 | for (int i = 0; i < service->method_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 565 | const MethodDescriptor* method = service->method(i); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 566 | grpc::string arg_name = |
| 567 | method->client_streaming() ? "request_iterator" : "request"; |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 568 | out->Print("\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 569 | out->Print("def $Method$(self, $ArgName$, context):\n", "Method", |
| 570 | method->name(), "ArgName", arg_name); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 571 | { |
| 572 | IndentScope raii_method_indent(out); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 573 | PrintAllComments(method); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 574 | out->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n"); |
| 575 | out->Print("context.set_details('Method not implemented!')\n"); |
| 576 | out->Print("raise NotImplementedError('Method not implemented!')\n"); |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 583 | bool PrivateGenerator::PrintAddServicerToServer( |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 584 | const grpc::string& package_qualified_service_name, |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 585 | const ServiceDescriptor* service) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 586 | out->Print("\n\n"); |
| 587 | out->Print("def add_$Service$Servicer_to_server(servicer, server):\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 588 | "Service", service->name()); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 589 | { |
| 590 | IndentScope raii_class_indent(out); |
| 591 | out->Print("rpc_method_handlers = {\n"); |
| 592 | { |
| 593 | IndentScope raii_dict_first_indent(out); |
| 594 | IndentScope raii_dict_second_indent(out); |
| 595 | for (int i = 0; i < service->method_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 596 | const MethodDescriptor* method = service->method(i); |
Masood Malekghassemi | 4a051cc | 2016-10-20 14:50:49 -0700 | [diff] [blame] | 597 | grpc::string method_handler_constructor = |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 598 | grpc::string(method->client_streaming() ? "stream" : "unary") + |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 599 | "_" + |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 600 | grpc::string(method->server_streaming() ? "stream" : "unary") + |
| 601 | "_rpc_method_handler"; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 602 | grpc::string request_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 603 | if (!GetModuleAndMessagePath(method->input_type(), |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 604 | &request_module_and_class)) { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 605 | return false; |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 606 | } |
| 607 | grpc::string response_module_and_class; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 608 | if (!GetModuleAndMessagePath(method->output_type(), |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 609 | &response_module_and_class)) { |
| 610 | return false; |
| 611 | } |
| 612 | out->Print("'$Method$': grpc.$MethodHandlerConstructor$(\n", "Method", |
| 613 | method->name(), "MethodHandlerConstructor", |
| 614 | method_handler_constructor); |
| 615 | { |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 616 | IndentScope raii_call_first_indent(out); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 617 | IndentScope raii_call_second_indent(out); |
| 618 | out->Print("servicer.$Method$,\n", "Method", method->name()); |
| 619 | out->Print( |
| 620 | "request_deserializer=$RequestModuleAndClass$.FromString,\n", |
| 621 | "RequestModuleAndClass", request_module_and_class); |
| 622 | out->Print( |
| 623 | "response_serializer=$ResponseModuleAndClass$.SerializeToString," |
| 624 | "\n", |
| 625 | "ResponseModuleAndClass", response_module_and_class); |
| 626 | } |
| 627 | out->Print("),\n"); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | out->Print("}\n"); |
| 631 | out->Print("generic_handler = grpc.method_handlers_generic_handler(\n"); |
| 632 | { |
| 633 | IndentScope raii_call_first_indent(out); |
| 634 | IndentScope raii_call_second_indent(out); |
| 635 | out->Print("'$PackageQualifiedServiceName$', rpc_method_handlers)\n", |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 636 | "PackageQualifiedServiceName", package_qualified_service_name); |
Nathaniel Manista | 4547940 | 2016-06-13 20:14:18 +0000 | [diff] [blame] | 637 | } |
| 638 | out->Print("server.add_generic_rpc_handlers((generic_handler,))\n"); |
| 639 | } |
| 640 | return true; |
| 641 | } |
| 642 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 643 | bool PrivateGenerator::PrintBetaPreamble() { |
Nathaniel Manista | f65d3c1 | 2015-09-05 03:55:19 +0000 | [diff] [blame] | 644 | out->Print("from $Package$ import implementations as beta_implementations\n", |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 645 | "Package", config.beta_package_root); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 646 | out->Print("from $Package$ import interfaces as beta_interfaces\n", "Package", |
| 647 | config.beta_package_root); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 648 | return true; |
| 649 | } |
| 650 | |
| 651 | bool PrivateGenerator::PrintPreamble() { |
| 652 | out->Print("import $Package$\n", "Package", config.grpc_package_root); |
Nathaniel Manista | cd9ec0e | 2015-08-31 07:49:45 +0000 | [diff] [blame] | 653 | out->Print("from grpc.framework.common import cardinality\n"); |
Masood Malekghassemi | ac59245 | 2016-07-01 11:58:04 -0700 | [diff] [blame] | 654 | out->Print( |
| 655 | "from grpc.framework.interfaces.face import utilities as " |
| 656 | "face_utilities\n"); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 657 | if (generate_in_pb2_grpc) { |
| 658 | out->Print("\n"); |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 659 | StringPairSet imports_set; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 660 | for (int i = 0; i < file->service_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 661 | const ServiceDescriptor* service = file->service(i); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 662 | for (int j = 0; j < service->method_count(); ++j) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 663 | const MethodDescriptor* method = service->method(j); |
| 664 | const Descriptor* types[2] = {method->input_type(), |
| 665 | method->output_type()}; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 666 | for (int k = 0; k < 2; ++k) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 667 | const Descriptor* type = types[k]; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 668 | grpc::string type_file_name = type->file()->name(); |
| 669 | grpc::string module_name = ModuleName(type_file_name); |
| 670 | grpc::string module_alias = ModuleAlias(type_file_name); |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 671 | imports_set.insert(std::make_tuple(module_name, module_alias)); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | } |
Bryan Blanchard | c549a3e | 2016-12-13 07:46:28 -0500 | [diff] [blame] | 675 | for (StringPairSet::iterator it = imports_set.begin(); |
| 676 | it != imports_set.end(); ++it) { |
| 677 | out->Print("import $ModuleName$ as $ModuleAlias$\n", "ModuleName", |
| 678 | std::get<0>(*it), "ModuleAlias", std::get<1>(*it)); |
| 679 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 680 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 681 | return true; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 684 | bool PrivateGenerator::PrintGAServices() { |
| 685 | grpc::string package = file->package(); |
| 686 | if (!package.empty()) { |
| 687 | package = package.append("."); |
| 688 | } |
| 689 | for (int i = 0; i < file->service_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 690 | const ServiceDescriptor* service = file->service(i); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 691 | grpc::string package_qualified_service_name = package + service->name(); |
| 692 | if (!(PrintStub(package_qualified_service_name, service) && |
| 693 | PrintServicer(service) && |
| 694 | PrintAddServicerToServer(package_qualified_service_name, service))) { |
| 695 | return false; |
| 696 | } |
| 697 | } |
| 698 | return true; |
| 699 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 700 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 701 | bool PrivateGenerator::PrintBetaServices() { |
| 702 | grpc::string package = file->package(); |
| 703 | if (!package.empty()) { |
| 704 | package = package.append("."); |
| 705 | } |
| 706 | for (int i = 0; i < file->service_count(); ++i) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 707 | const ServiceDescriptor* service = file->service(i); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 708 | grpc::string package_qualified_service_name = package + service->name(); |
| 709 | if (!(PrintBetaServicer(service) && PrintBetaStub(service) && |
| 710 | PrintBetaServerFactory(package_qualified_service_name, service) && |
| 711 | PrintBetaStubFactory(package_qualified_service_name, service))) { |
| 712 | return false; |
| 713 | } |
| 714 | } |
| 715 | return true; |
| 716 | } |
| 717 | |
| 718 | pair<bool, grpc::string> PrivateGenerator::GetGrpcServices() { |
Masood Malekghassemi | 65c803b | 2015-03-20 06:48:47 -0700 | [diff] [blame] | 719 | grpc::string output; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 720 | { |
| 721 | // Scope the output stream so it closes and finalizes output to the string. |
| 722 | StringOutputStream output_stream(&output); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 723 | Printer out_printer(&output_stream, '$'); |
| 724 | out = &out_printer; |
| 725 | |
| 726 | if (generate_in_pb2_grpc) { |
| 727 | if (!PrintPreamble()) { |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 728 | return make_pair(false, ""); |
| 729 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 730 | if (!PrintGAServices()) { |
| 731 | return make_pair(false, ""); |
| 732 | } |
| 733 | } else { |
| 734 | out->Print("try:\n"); |
| 735 | { |
| 736 | IndentScope raii_dict_try_indent(out); |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 737 | out->Print( |
| 738 | "# THESE ELEMENTS WILL BE DEPRECATED.\n" |
| 739 | "# Please use the generated *_pb2_grpc.py files instead.\n"); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 740 | if (!PrintPreamble()) { |
| 741 | return make_pair(false, ""); |
| 742 | } |
| 743 | if (!PrintBetaPreamble()) { |
| 744 | return make_pair(false, ""); |
| 745 | } |
| 746 | if (!PrintGAServices()) { |
| 747 | return make_pair(false, ""); |
| 748 | } |
| 749 | if (!PrintBetaServices()) { |
| 750 | return make_pair(false, ""); |
| 751 | } |
| 752 | } |
| 753 | out->Print("except ImportError:\n"); |
| 754 | { |
| 755 | IndentScope raii_dict_except_indent(out); |
| 756 | out->Print("pass"); |
| 757 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 758 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 759 | } |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 760 | return make_pair(true, std::move(output)); |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 763 | } // namespace |
| 764 | |
| 765 | GeneratorConfiguration::GeneratorConfiguration() |
| 766 | : grpc_package_root("grpc"), beta_package_root("grpc.beta") {} |
| 767 | |
| 768 | PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config) |
| 769 | : config_(config) {} |
| 770 | |
| 771 | PythonGrpcGenerator::~PythonGrpcGenerator() {} |
| 772 | |
Masood Malekghassemi | 561dce4 | 2016-11-30 20:20:32 -0800 | [diff] [blame] | 773 | static bool GenerateGrpc(GeneratorContext* context, PrivateGenerator& generator, |
| 774 | grpc::string file_name, bool generate_in_pb2_grpc) { |
| 775 | bool success; |
| 776 | std::unique_ptr<ZeroCopyOutputStream> output; |
| 777 | std::unique_ptr<CodedOutputStream> coded_output; |
| 778 | grpc::string grpc_code; |
| 779 | |
| 780 | if (generate_in_pb2_grpc) { |
| 781 | output.reset(context->Open(file_name)); |
| 782 | generator.generate_in_pb2_grpc = true; |
| 783 | } else { |
| 784 | output.reset(context->OpenForInsert(file_name, "module_scope")); |
| 785 | generator.generate_in_pb2_grpc = false; |
| 786 | } |
| 787 | |
| 788 | coded_output.reset(new CodedOutputStream(output.get())); |
| 789 | tie(success, grpc_code) = generator.GetGrpcServices(); |
| 790 | |
| 791 | if (success) { |
| 792 | coded_output->WriteRaw(grpc_code.data(), grpc_code.size()); |
| 793 | return true; |
| 794 | } else { |
| 795 | return false; |
| 796 | } |
| 797 | } |
| 798 | |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 799 | bool PythonGrpcGenerator::Generate(const FileDescriptor* file, |
| 800 | const grpc::string& parameter, |
| 801 | GeneratorContext* context, |
| 802 | grpc::string* error) const { |
| 803 | // Get output file name. |
| 804 | grpc::string pb2_file_name; |
| 805 | grpc::string pb2_grpc_file_name; |
| 806 | static const int proto_suffix_length = strlen(".proto"); |
| 807 | if (file->name().size() > static_cast<size_t>(proto_suffix_length) && |
| 808 | file->name().find_last_of(".proto") == file->name().size() - 1) { |
Masood Malekghassemi | 7780cc0 | 2016-10-24 15:30:05 -0700 | [diff] [blame] | 809 | grpc::string base = |
| 810 | file->name().substr(0, file->name().size() - proto_suffix_length); |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 811 | pb2_file_name = base + "_pb2.py"; |
| 812 | pb2_grpc_file_name = base + "_pb2_grpc.py"; |
| 813 | } else { |
| 814 | *error = "Invalid proto file name. Proto file must end with .proto"; |
| 815 | return false; |
| 816 | } |
| 817 | |
| 818 | PrivateGenerator generator(config_, file); |
Masood Malekghassemi | 561dce4 | 2016-11-30 20:20:32 -0800 | [diff] [blame] | 819 | if (parameter == "grpc_2_0") { |
| 820 | return GenerateGrpc(context, generator, pb2_grpc_file_name, true); |
| 821 | } else if (parameter == "") { |
| 822 | return GenerateGrpc(context, generator, pb2_grpc_file_name, true) && |
| 823 | GenerateGrpc(context, generator, pb2_file_name, false); |
| 824 | } else { |
| 825 | *error = "Invalid parameter '" + parameter + "'."; |
| 826 | return false; |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 827 | } |
Masood Malekghassemi | d953959 | 2016-10-20 14:47:14 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 830 | } // namespace grpc_python_generator |