blob: 0dd2c5b834416488c6ddbec0e4782b83b055e7a9 [file] [log] [blame]
Masood Malekghassemif8e297a2015-02-19 15:39:32 -08001/*
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 Malekghassemi59d9ff42015-02-23 15:28:07 -080036#include <cstring>
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080037#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
47using google::protobuf::FileDescriptor;
48using google::protobuf::compiler::CodeGenerator;
49using google::protobuf::compiler::GeneratorContext;
50using google::protobuf::compiler::PluginMain;
51using google::protobuf::io::CodedOutputStream;
52using google::protobuf::io::ZeroCopyOutputStream;
53using std::string;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080054using std::strlen;
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080055
56class PythonGrpcGenerator : public CodeGenerator {
57 public:
58 PythonGrpcGenerator() {}
Craig Tillercf133f42015-02-26 14:05:56 -080059 ~PythonGrpcGenerator() {}
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080060
Craig Tillercf133f42015-02-26 14:05:56 -080061 bool Generate(const FileDescriptor* file, const string& parameter,
62 GeneratorContext* context, string* error) const {
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080063 // Get output file name.
64 string file_name;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080065 static const int proto_suffix_length = strlen(".proto");
vjpaic729d932015-02-20 11:40:21 -080066 if (file->name().size() > static_cast<size_t>(proto_suffix_length) &&
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080067 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 Malekghassemi59d9ff42015-02-23 15:28:07 -080078 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 Malekghassemif8e297a2015-02-19 15:39:32 -080087 }
88};
89
90int main(int argc, char* argv[]) {
91 PythonGrpcGenerator generator;
92 return PluginMain(argc, argv, &generator);
93}