Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 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 | |
| 34 | // Generates a Python gRPC service interface out of Protobuf IDL. |
| 35 | |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 36 | #include <cstring> |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 37 | #include <memory> |
| 38 | #include <string> |
| 39 | |
| 40 | #include "src/compiler/python_generator.h" |
| 41 | #include <google/protobuf/compiler/code_generator.h> |
| 42 | #include <google/protobuf/compiler/plugin.h> |
| 43 | #include <google/protobuf/io/coded_stream.h> |
| 44 | #include <google/protobuf/io/zero_copy_stream.h> |
| 45 | #include <google/protobuf/descriptor.h> |
| 46 | |
| 47 | using google::protobuf::FileDescriptor; |
| 48 | using google::protobuf::compiler::CodeGenerator; |
| 49 | using google::protobuf::compiler::GeneratorContext; |
| 50 | using google::protobuf::compiler::PluginMain; |
| 51 | using google::protobuf::io::CodedOutputStream; |
| 52 | using google::protobuf::io::ZeroCopyOutputStream; |
| 53 | using std::string; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 54 | using std::strlen; |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 55 | |
| 56 | class PythonGrpcGenerator : public CodeGenerator { |
| 57 | public: |
| 58 | PythonGrpcGenerator() {} |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 59 | ~PythonGrpcGenerator() {} |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 60 | |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame^] | 61 | bool Generate(const FileDescriptor* file, const string& parameter, |
| 62 | GeneratorContext* context, string* error) const { |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 63 | // Get output file name. |
| 64 | string file_name; |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 65 | static const int proto_suffix_length = strlen(".proto"); |
vjpai | c729d93 | 2015-02-20 11:40:21 -0800 | [diff] [blame] | 66 | if (file->name().size() > static_cast<size_t>(proto_suffix_length) && |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 67 | file->name().find_last_of(".proto") == file->name().size() - 1) { |
| 68 | file_name = file->name().substr( |
| 69 | 0, file->name().size() - proto_suffix_length) + "_pb2.py"; |
| 70 | } else { |
| 71 | *error = "Invalid proto file name. Proto file must end with .proto"; |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | std::unique_ptr<ZeroCopyOutputStream> output( |
| 76 | context->OpenForInsert(file_name, "module_scope")); |
| 77 | CodedOutputStream coded_out(output.get()); |
Masood Malekghassemi | 59d9ff4 | 2015-02-23 15:28:07 -0800 | [diff] [blame] | 78 | bool success = false; |
| 79 | string code = ""; |
| 80 | tie(success, code) = grpc_python_generator::GetServices(file); |
| 81 | if (success) { |
| 82 | coded_out.WriteRaw(code.data(), code.size()); |
| 83 | return true; |
| 84 | } else { |
| 85 | return false; |
| 86 | } |
Masood Malekghassemi | f8e297a | 2015-02-19 15:39:32 -0800 | [diff] [blame] | 87 | } |
| 88 | }; |
| 89 | |
| 90 | int main(int argc, char* argv[]) { |
| 91 | PythonGrpcGenerator generator; |
| 92 | return PluginMain(argc, argv, &generator); |
| 93 | } |