Добавил консольный клиент
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import json
|
||||
|
||||
from modules.transports.client import Client
|
||||
|
||||
class Command:
|
||||
def __init__(self, name):
|
||||
"""
|
||||
Конструктор
|
||||
:param name: Название команды
|
||||
"""
|
||||
self.name = name
|
||||
|
||||
def do(self, parameters):
|
||||
"""
|
||||
Вызов команды
|
||||
:param parameters: Параметры
|
||||
:return: Результат работы
|
||||
"""
|
||||
raise Exception("calling unimplemented method")
|
||||
|
||||
class CommandManager:
|
||||
def __init__(self):
|
||||
self.commands = []
|
||||
self.command_name_to_index = {}
|
||||
|
||||
def addCommand(self, command: Command):
|
||||
self.command_name_to_index[command.name] = len(self.commands)
|
||||
self.commands.append(command)
|
||||
|
||||
def hasCommand(self, name) -> bool:
|
||||
if str.isnumeric(name):
|
||||
index = int(name)
|
||||
if index < 0 or index >= len(self.commands):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
return name in self.command_name_to_index
|
||||
|
||||
def getCommand(self, name):
|
||||
if not self.hasCommand(name):
|
||||
raise Exception("there is no command with such name")
|
||||
|
||||
if str.isnumeric(name):
|
||||
return self.commands[int(name)]
|
||||
|
||||
return self.command_name_to_index[name]
|
||||
|
||||
def DoRequest(host: str, port: str, request: dict):
|
||||
"""
|
||||
Выполнение запроса
|
||||
:param host: Хост
|
||||
:param port: Порт
|
||||
:param request: Тело запроса
|
||||
:return: Ответ
|
||||
"""
|
||||
|
||||
client = Client()
|
||||
client.connect(host, port)
|
||||
|
||||
client.send(json.dumps(request))
|
||||
|
||||
return json.loads(client.receive())
|
||||
|
||||
class GetPipes(Command):
|
||||
"""
|
||||
Получение списка пайп
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("get_pipes")
|
||||
|
||||
def do(self, parameters):
|
||||
request = {
|
||||
"handler": self.name,
|
||||
"parameters": ""
|
||||
}
|
||||
|
||||
host = parameters["host"]
|
||||
port = parameters["port"]
|
||||
|
||||
response = DoRequest(host, port, request)
|
||||
if response["status"] != "ok":
|
||||
print("error: " + response["message"])
|
||||
return
|
||||
|
||||
pipes = response["result"]
|
||||
for pipe in pipes:
|
||||
print(pipe["name"] + ":")
|
||||
print("- description: " + pipe["description"])
|
||||
print("- input data type: " + pipe["input_data_type"])
|
||||
print("- output data type: " + pipe["output_data_type"])
|
||||
print()
|
||||
|
||||
class GetPipelines(Command):
|
||||
"""
|
||||
Получение списка пайплайнов
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("get_pipelines")
|
||||
|
||||
def do(self, parameters):
|
||||
request = {
|
||||
"handler": self.name,
|
||||
"parameters": ""
|
||||
}
|
||||
|
||||
host = parameters["host"]
|
||||
port = parameters["port"]
|
||||
|
||||
response = DoRequest(host, port, request)
|
||||
if response["status"] != "ok":
|
||||
print("error: " + response["message"])
|
||||
return
|
||||
|
||||
pipelines = response["result"]
|
||||
if len(pipelines) == 0:
|
||||
print("there are no pipelines")
|
||||
else:
|
||||
for pipeline in pipelines:
|
||||
print(pipeline["name"] + ": " + str(pipeline["pipes"]))
|
||||
|
||||
|
||||
class RunPipeline(Command):
|
||||
"""
|
||||
Запуск пайплайна
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("run_pipeline")
|
||||
|
||||
def do(self, parameters):
|
||||
pipeline = input("pipeline: ")
|
||||
data = input("data: ")
|
||||
|
||||
request = {
|
||||
"handler": self.name,
|
||||
"parameters": {
|
||||
"name": pipeline,
|
||||
"data": data
|
||||
}
|
||||
}
|
||||
|
||||
host = parameters["host"]
|
||||
port = parameters["port"]
|
||||
|
||||
response = DoRequest(host, port, request)
|
||||
if response["status"] != "ok":
|
||||
print("error: " + response["message"])
|
||||
return
|
||||
|
||||
print("result: " + str(response["result"]))
|
||||
|
||||
class CreatePipeline(Command):
|
||||
"""
|
||||
Создание пайплайна
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super().__init__("create_pipeline")
|
||||
|
||||
def do(self, parameters):
|
||||
name = input("name: ")
|
||||
pipes = input("pipes (separated by spaces): ")
|
||||
|
||||
request = {
|
||||
"handler": self.name,
|
||||
"parameters": {
|
||||
"name": name,
|
||||
"pipes": pipes.split()
|
||||
}
|
||||
}
|
||||
|
||||
host = parameters["host"]
|
||||
port = parameters["port"]
|
||||
|
||||
response = DoRequest(host, port, request)
|
||||
if response["status"] != "ok":
|
||||
print("error: " + response["message"])
|
||||
return
|
||||
|
||||
print("result: " + str(response["result"]))
|
||||
@@ -0,0 +1,41 @@
|
||||
from services.console_client.commands import *
|
||||
|
||||
def CreateCommandManager():
|
||||
manager = CommandManager()
|
||||
manager.addCommand(GetPipes())
|
||||
manager.addCommand(GetPipelines())
|
||||
manager.addCommand(CreatePipeline())
|
||||
manager.addCommand(RunPipeline())
|
||||
|
||||
return manager
|
||||
|
||||
def PrintHelp(command_manager):
|
||||
for index, command in enumerate(command_manager.commands):
|
||||
print(str(index) + ". " + command.name)
|
||||
|
||||
def main():
|
||||
command_manager = CreateCommandManager()
|
||||
|
||||
parameters = {
|
||||
"host": "localhost",
|
||||
"port": 8081
|
||||
}
|
||||
|
||||
while True:
|
||||
PrintHelp(command_manager)
|
||||
|
||||
command_name = str(input("> "))
|
||||
if command_name in ["q", "quit", "exit"]:
|
||||
break
|
||||
|
||||
if not command_manager.hasCommand(command_name):
|
||||
print("there is no command with such name")
|
||||
continue
|
||||
|
||||
command = command_manager.getCommand(command_name)
|
||||
command.do(parameters)
|
||||
|
||||
print()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user