blob: 05c6b095d8e2916985cb9c2c6d791e8cdeb98a8e [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
36#include <memory>
37#include <string>
38
39#include "src/compiler/python_generator.h"
40#include <google/protobuf/compiler/code_generator.h>
41#include <google/protobuf/compiler/plugin.h>
42#include <google/protobuf/io/coded_stream.h>
43#include <google/protobuf/io/zero_copy_stream.h>
44#include <google/protobuf/descriptor.h>
45
46using google::protobuf::FileDescriptor;
47using google::protobuf::compiler::CodeGenerator;
48using google::protobuf::compiler::GeneratorContext;
49using google::protobuf::compiler::PluginMain;
50using google::protobuf::io::CodedOutputStream;
51using google::protobuf::io::ZeroCopyOutputStream;
52using std::string;
53
54class PythonGrpcGenerator : public CodeGenerator {
55 public:
56 PythonGrpcGenerator() {}
57 ~PythonGrpcGenerator() override {}
58
59 bool Generate(const FileDescriptor* file,
60 const string& parameter,
61 GeneratorContext* context,
62 string* error) const override {
63 // Get output file name.
64 string file_name;
65 static const int proto_suffix_length = 6; // length of ".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());
78 string code = grpc_python_generator::GetServices(file);
79 coded_out.WriteRaw(code.data(), code.size());
80 return true;
81 }
82};
83
84int main(int argc, char* argv[]) {
85 PythonGrpcGenerator generator;
86 return PluginMain(argc, argv, &generator);
87}