blob: d5f69e20e71b71675f8647df3738d15c1ff707b2 [file] [log] [blame]
Austin Schuh272c6132020-11-14 16:37:52 -08001/*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
Austin Schuh272c6132020-11-14 16:37:52 -080019#include <map>
Austin Schuh272c6132020-11-14 16:37:52 -080020#include <sstream>
Austin Schuh272c6132020-11-14 16:37:52 -080021
22#include "flatbuffers/util.h"
23#include "src/compiler/python_generator.h"
Austin Schuh272c6132020-11-14 16:37:52 -080024
25namespace grpc_python_generator {
Austin Schuh2dd86a92022-09-14 21:19:23 -070026namespace {
Austin Schuh272c6132020-11-14 16:37:52 -080027
Austin Schuh2dd86a92022-09-14 21:19:23 -070028static grpc::string GenerateMethodType(const grpc_generator::Method *method) {
Austin Schuh272c6132020-11-14 16:37:52 -080029
James Kuszmaul8e62b022022-03-22 09:33:25 -070030 if (method->NoStreaming())
31 return "unary_unary";
Austin Schuh272c6132020-11-14 16:37:52 -080032
James Kuszmaul8e62b022022-03-22 09:33:25 -070033 if (method->ServerStreaming())
34 return "unary_stream";
35
36 if (method->ClientStreaming())
37 return "stream_unary";
38
39 return "stream_stream";
40}
41
42grpc::string GenerateMethodInput(const grpc_generator::Method *method) {
43
44 if (method->NoStreaming() || method->ServerStreaming())
45 return "self, request, context";
46
47 return "self, request_iterator, context";
48}
49
50void GenerateStub(const grpc_generator::Service *service,
51 grpc_generator::Printer *printer,
52 std::map<grpc::string, grpc::string> *dictonary) {
53 auto vars = *dictonary;
54 printer->Print(vars, "class $ServiceName$Stub(object):\n");
55 printer->Indent();
56 printer->Print("\"\"\" Interface exported by the server. \"\"\"");
57 printer->Print("\n\n");
58 printer->Print("def __init__(self, channel):\n");
59 printer->Indent();
60 printer->Print("\"\"\" Constructor. \n\n");
61 printer->Print("Args: \n");
62 printer->Print("channel: A grpc.Channel. \n");
63 printer->Print("\"\"\"\n\n");
64
65 for (int j = 0; j < service->method_count(); j++) {
66 auto method = service->method(j);
67 vars["MethodName"] = method->name();
68 vars["MethodType"] = GenerateMethodType(&*method);
69 printer->Print(vars, "self.$MethodName$ = channel.$MethodType$(\n");
70 printer->Indent();
71 printer->Print(vars, "\"/$PATH$$ServiceName$/$MethodName$\"\n");
72 printer->Print(")\n");
73 printer->Outdent();
74 printer->Print("\n");
Austin Schuh272c6132020-11-14 16:37:52 -080075 }
James Kuszmaul8e62b022022-03-22 09:33:25 -070076 printer->Outdent();
77 printer->Outdent();
78 printer->Print("\n");
Austin Schuh272c6132020-11-14 16:37:52 -080079}
80
James Kuszmaul8e62b022022-03-22 09:33:25 -070081void GenerateServicer(const grpc_generator::Service *service,
82 grpc_generator::Printer *printer,
83 std::map<grpc::string, grpc::string> *dictonary) {
84 auto vars = *dictonary;
85 printer->Print(vars, "class $ServiceName$Servicer(object):\n");
86 printer->Indent();
87 printer->Print("\"\"\" Interface exported by the server. \"\"\"");
88 printer->Print("\n\n");
Austin Schuh272c6132020-11-14 16:37:52 -080089
James Kuszmaul8e62b022022-03-22 09:33:25 -070090 for (int j = 0; j < service->method_count(); j++) {
91 auto method = service->method(j);
92 vars["MethodName"] = method->name();
93 vars["MethodInput"] = GenerateMethodInput(&*method);
94 printer->Print(vars, "def $MethodName$($MethodInput$):\n");
95 printer->Indent();
96 printer->Print("context.set_code(grpc.StatusCode.UNIMPLEMENTED)\n");
97 printer->Print("context.set_details('Method not implemented!')\n");
98 printer->Print("raise NotImplementedError('Method not implemented!')\n");
99 printer->Outdent();
100 printer->Print("\n\n");
Austin Schuh272c6132020-11-14 16:37:52 -0800101 }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700102 printer->Outdent();
103 printer->Print("\n");
104
Austin Schuh272c6132020-11-14 16:37:52 -0800105}
106
James Kuszmaul8e62b022022-03-22 09:33:25 -0700107void GenerateRegister(const grpc_generator::Service *service,
108 grpc_generator::Printer *printer,
109 std::map<grpc::string, grpc::string> *dictonary) {
110 auto vars = *dictonary;
111 printer->Print(vars, "def add_$ServiceName$Servicer_to_server(servicer, server):\n");
112 printer->Indent();
113 printer->Print("rpc_method_handlers = {\n");
114 printer->Indent();
115 for (int j = 0; j < service->method_count(); j++) {
116 auto method = service->method(j);
117 vars["MethodName"] = method->name();
118 vars["MethodType"] = GenerateMethodType(&*method);
119 printer->Print(vars, "'$MethodName$': grpc.$MethodType$_rpc_method_handler(\n");
120 printer->Indent();
121 printer->Print(vars, "servicer.$MethodName$\n");
122 printer->Outdent();
123 printer->Print("),\n");
Austin Schuh272c6132020-11-14 16:37:52 -0800124 }
James Kuszmaul8e62b022022-03-22 09:33:25 -0700125 printer->Outdent();
126 printer->Print("}\n");
127 printer->Print(vars, "generic_handler = grpc.method_handlers_generic_handler(\n");
128 printer->Indent();
129 printer->Print(vars, "'$PATH$$ServiceName$', rpc_method_handlers)\n");
130 printer->Outdent();
131 printer->Print("server.add_generic_rpc_handlers((generic_handler,))");
132 printer->Outdent();
133 printer->Print("\n");
Austin Schuh272c6132020-11-14 16:37:52 -0800134}
Austin Schuh2dd86a92022-09-14 21:19:23 -0700135} // namespace
Austin Schuh272c6132020-11-14 16:37:52 -0800136
James Kuszmaul8e62b022022-03-22 09:33:25 -0700137grpc::string Generate(grpc_generator::File *file,
138 const grpc_generator::Service *service) {
Austin Schuh272c6132020-11-14 16:37:52 -0800139 grpc::string output;
James Kuszmaul8e62b022022-03-22 09:33:25 -0700140 std::map<grpc::string, grpc::string> vars;
141 vars["PATH"] = file->package();
142 if (!file->package().empty()) { vars["PATH"].append("."); }
143 vars["ServiceName"] = service->name();
144 auto printer = file->CreatePrinter(&output);
145 GenerateStub(service, &*printer, &vars);
146 GenerateServicer(service, &*printer, &vars);
147 GenerateRegister(service, &*printer, &vars);
Austin Schuh272c6132020-11-14 16:37:52 -0800148 return output;
149}
150
151} // namespace grpc_python_generator