blob: ed1e0494fbe8616df8265402904c24193a1a39d8 [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() {}
59 ~PythonGrpcGenerator() override {}
60
61 bool Generate(const FileDescriptor* file,
62 const string& parameter,
63 GeneratorContext* context,
64 string* error) const override {
65 // Get output file name.
66 string file_name;
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080067 static const int proto_suffix_length = strlen(".proto");
vjpaic729d932015-02-20 11:40:21 -080068 if (file->name().size() > static_cast<size_t>(proto_suffix_length) &&
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080069 file->name().find_last_of(".proto") == file->name().size() - 1) {
70 file_name = file->name().substr(
71 0, file->name().size() - proto_suffix_length) + "_pb2.py";
72 } else {
73 *error = "Invalid proto file name. Proto file must end with .proto";
74 return false;
75 }
76
77 std::unique_ptr<ZeroCopyOutputStream> output(
78 context->OpenForInsert(file_name, "module_scope"));
79 CodedOutputStream coded_out(output.get());
Masood Malekghassemi59d9ff42015-02-23 15:28:07 -080080 bool success = false;
81 string code = "";
82 tie(success, code) = grpc_python_generator::GetServices(file);
83 if (success) {
84 coded_out.WriteRaw(code.data(), code.size());
85 return true;
86 } else {
87 return false;
88 }
Masood Malekghassemif8e297a2015-02-19 15:39:32 -080089 }
90};
91
92int main(int argc, char* argv[]) {
93 PythonGrpcGenerator generator;
94 return PluginMain(argc, argv, &generator);
95}