194 lines
5.1 KiB
Python
194 lines
5.1 KiB
Python
import json
|
|
|
|
from modules.transports.client import Client
|
|
|
|
class Settings:
|
|
def __init__(self):
|
|
self.host = None
|
|
self.port = None
|
|
|
|
class CommandException(Exception):
|
|
def __init__(self, message):
|
|
super().__init__(message)
|
|
|
|
class Command:
|
|
def __init__(self, name):
|
|
"""
|
|
Конструктор
|
|
:param name: Название команды
|
|
"""
|
|
self.name = name
|
|
self.settings = None
|
|
|
|
def do(self, parameters):
|
|
"""
|
|
Вызов команды
|
|
:param parameters: Параметры
|
|
:return: Результат работы
|
|
"""
|
|
raise Exception("calling unimplemented method")
|
|
|
|
def doRequest(self, request: dict):
|
|
"""
|
|
Выполнение запроса
|
|
:param request: Тело запроса
|
|
:return: Ответ
|
|
"""
|
|
|
|
# Создаём клиентский сокет и подключаемся к серверу
|
|
client = Client()
|
|
client.connect(self.settings.host, self.settings.port)
|
|
|
|
# Отправляем запрос
|
|
client.send(json.dumps(request))
|
|
|
|
# Получаем ответ
|
|
response = client.receive()
|
|
|
|
# Закрываем соединение
|
|
client.disconnect()
|
|
|
|
# Парсим ответ
|
|
response = json.loads(response)
|
|
|
|
# Проверяем статус обработки запроса
|
|
status = response["status"]
|
|
if status != "ok":
|
|
message = response["message"]
|
|
raise CommandException(f"status: {status}, message: {message}")
|
|
|
|
return response["result"]
|
|
|
|
class CommandManager:
|
|
def __init__(self, settings):
|
|
self.settings = settings
|
|
self.commands = []
|
|
self.command_name_to_index = {}
|
|
|
|
def addCommand(self, command: Command):
|
|
command.settings = self.settings
|
|
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.commands[self.command_name_to_index[name]]
|
|
|
|
class GetPipes(Command):
|
|
"""
|
|
Получение списка пайп
|
|
"""
|
|
|
|
def __init__(self):
|
|
super().__init__("get_pipes")
|
|
|
|
def do(self, parameters):
|
|
request = {
|
|
"handler": self.name,
|
|
"parameters": ""
|
|
}
|
|
|
|
# Ответ -- это список пайп
|
|
response = self.doRequest(request)
|
|
|
|
if len(response) == 0:
|
|
print("there are no pipes")
|
|
return
|
|
|
|
for pipe in response[:-1]:
|
|
self.printPipe(pipe)
|
|
print()
|
|
|
|
self.printPipe(response[-1])
|
|
|
|
def printPipe(self, pipe):
|
|
print(pipe["name"] + ":")
|
|
print("- description: " + pipe["description"])
|
|
print("- input data type: " + pipe["input_data_type"])
|
|
print("- output data type: " + pipe["output_data_type"])
|
|
|
|
class GetPipelines(Command):
|
|
"""
|
|
Получение списка пайплайнов
|
|
"""
|
|
|
|
def __init__(self):
|
|
super().__init__("get_pipelines")
|
|
|
|
def do(self, parameters):
|
|
request = {
|
|
"handler": self.name,
|
|
"parameters": ""
|
|
}
|
|
|
|
# Ответ -- это список пайплайнов
|
|
response = self.doRequest(request)
|
|
|
|
if len(response) == 0:
|
|
print("there are no pipelines")
|
|
return
|
|
|
|
for pipeline in response:
|
|
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
|
|
}
|
|
}
|
|
|
|
response = self.doRequest(request)
|
|
print("result: " + str(response))
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
self.doRequest(request)
|
|
print("created successfully")
|