refactor: plugin code structure

This commit is contained in:
debugtalk
2022-03-05 00:34:55 +08:00
parent 1904d404fd
commit 0ede8d7989
27 changed files with 297 additions and 25 deletions

View File

@@ -0,0 +1,68 @@
# -*- coding: utf-8 -*-
# Generated by the protocol buffer compiler. DO NOT EDIT!
# source: debugtalk.proto
"""Generated protocol buffer code."""
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)
_sym_db = _symbol_database.Default()
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x0f\x64\x65\x62ugtalk.proto\x12\x05proto\"\x07\n\x05\x45mpty\"!\n\x10GetNamesResponse\x12\r\n\x05names\x18\x01 \x03(\t\")\n\x0b\x43\x61llRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x0c\n\x04\x61rgs\x18\x02 \x01(\x0c\"\x1d\n\x0c\x43\x61llResponse\x12\r\n\x05value\x18\x01 \x01(\x0c\x32o\n\tDebugTalk\x12\x31\n\x08GetNames\x12\x0c.proto.Empty\x1a\x17.proto.GetNamesResponse\x12/\n\x04\x43\x61ll\x12\x12.proto.CallRequest\x1a\x13.proto.CallResponseB\x11Z\x0fplugin/go/protob\x06proto3')
_EMPTY = DESCRIPTOR.message_types_by_name['Empty']
_GETNAMESRESPONSE = DESCRIPTOR.message_types_by_name['GetNamesResponse']
_CALLREQUEST = DESCRIPTOR.message_types_by_name['CallRequest']
_CALLRESPONSE = DESCRIPTOR.message_types_by_name['CallResponse']
Empty = _reflection.GeneratedProtocolMessageType('Empty', (_message.Message,), {
'DESCRIPTOR' : _EMPTY,
'__module__' : 'debugtalk_pb2'
# @@protoc_insertion_point(class_scope:proto.Empty)
})
_sym_db.RegisterMessage(Empty)
GetNamesResponse = _reflection.GeneratedProtocolMessageType('GetNamesResponse', (_message.Message,), {
'DESCRIPTOR' : _GETNAMESRESPONSE,
'__module__' : 'debugtalk_pb2'
# @@protoc_insertion_point(class_scope:proto.GetNamesResponse)
})
_sym_db.RegisterMessage(GetNamesResponse)
CallRequest = _reflection.GeneratedProtocolMessageType('CallRequest', (_message.Message,), {
'DESCRIPTOR' : _CALLREQUEST,
'__module__' : 'debugtalk_pb2'
# @@protoc_insertion_point(class_scope:proto.CallRequest)
})
_sym_db.RegisterMessage(CallRequest)
CallResponse = _reflection.GeneratedProtocolMessageType('CallResponse', (_message.Message,), {
'DESCRIPTOR' : _CALLRESPONSE,
'__module__' : 'debugtalk_pb2'
# @@protoc_insertion_point(class_scope:proto.CallResponse)
})
_sym_db.RegisterMessage(CallResponse)
_DEBUGTALK = DESCRIPTOR.services_by_name['DebugTalk']
if _descriptor._USE_C_DESCRIPTORS == False:
DESCRIPTOR._options = None
DESCRIPTOR._serialized_options = b'Z\017plugin/go/proto'
_EMPTY._serialized_start=26
_EMPTY._serialized_end=33
_GETNAMESRESPONSE._serialized_start=35
_GETNAMESRESPONSE._serialized_end=68
_CALLREQUEST._serialized_start=70
_CALLREQUEST._serialized_end=111
_CALLRESPONSE._serialized_start=113
_CALLRESPONSE._serialized_end=142
_DEBUGTALK._serialized_start=144
_DEBUGTALK._serialized_end=255
# @@protoc_insertion_point(module_scope)

View File

@@ -0,0 +1,99 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import debugtalk_pb2 as debugtalk__pb2
class DebugTalkStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.GetNames = channel.unary_unary(
'/proto.DebugTalk/GetNames',
request_serializer=debugtalk__pb2.Empty.SerializeToString,
response_deserializer=debugtalk__pb2.GetNamesResponse.FromString,
)
self.Call = channel.unary_unary(
'/proto.DebugTalk/Call',
request_serializer=debugtalk__pb2.CallRequest.SerializeToString,
response_deserializer=debugtalk__pb2.CallResponse.FromString,
)
class DebugTalkServicer(object):
"""Missing associated documentation comment in .proto file."""
def GetNames(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def Call(self, request, context):
"""Missing associated documentation comment in .proto file."""
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')
def add_DebugTalkServicer_to_server(servicer, server):
rpc_method_handlers = {
'GetNames': grpc.unary_unary_rpc_method_handler(
servicer.GetNames,
request_deserializer=debugtalk__pb2.Empty.FromString,
response_serializer=debugtalk__pb2.GetNamesResponse.SerializeToString,
),
'Call': grpc.unary_unary_rpc_method_handler(
servicer.Call,
request_deserializer=debugtalk__pb2.CallRequest.FromString,
response_serializer=debugtalk__pb2.CallResponse.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'proto.DebugTalk', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
# This class is part of an EXPERIMENTAL API.
class DebugTalk(object):
"""Missing associated documentation comment in .proto file."""
@staticmethod
def GetNames(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/proto.DebugTalk/GetNames',
debugtalk__pb2.Empty.SerializeToString,
debugtalk__pb2.GetNamesResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@staticmethod
def Call(request,
target,
options=(),
channel_credentials=None,
call_credentials=None,
insecure=False,
compression=None,
wait_for_ready=None,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/proto.DebugTalk/Call',
debugtalk__pb2.CallRequest.SerializeToString,
debugtalk__pb2.CallResponse.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

46
plugin/python/plugin.py Normal file
View File

@@ -0,0 +1,46 @@
from concurrent import futures
import sys
import time
import grpc
import debugtalk_pb2
import debugtalk_pb2_grpc
from grpc_health.v1.health import HealthServicer
from grpc_health.v1 import health_pb2, health_pb2_grpc
class DebugTalkServicer(debugtalk_pb2_grpc.DebugTalkServicer):
"""Implementation of DebugTalk service."""
def GetNames(self, request, context):
result = debugtalk_pb2.GetNamesResponse()
return result
def Call(self, request, context):
return
def serve():
# We need to build a health service to work with go-plugin
health = HealthServicer()
health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))
# Start the server.
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
debugtalk_pb2_grpc.add_DebugTalkServicer_to_server(DebugTalkServicer(), server)
health_pb2_grpc.add_HealthServicer_to_server(health, server)
server.add_insecure_port('127.0.0.1:1234')
server.start()
# Output information
print("1|1|tcp|127.0.0.1:1234|grpc")
sys.stdout.flush()
try:
while True:
time.sleep(60 * 60 * 24)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
serve()