184 lines
4.7 KiB
Python
184 lines
4.7 KiB
Python
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"]))
|