From 6217c89591014c97cca63c2b1a597f2d62a95b52 Mon Sep 17 00:00:00 2001 From: Kirill Volokh Date: Tue, 5 Dec 2023 00:11:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=81=D0=BE=D0=BB=D1=8C=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/console_client/commands.py | 183 ++++++++++++++++++++++ services/console_client/console_client.py | 41 +++++ 2 files changed, 224 insertions(+) create mode 100644 services/console_client/commands.py create mode 100644 services/console_client/console_client.py diff --git a/services/console_client/commands.py b/services/console_client/commands.py new file mode 100644 index 0000000..e008ad8 --- /dev/null +++ b/services/console_client/commands.py @@ -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"])) diff --git a/services/console_client/console_client.py b/services/console_client/console_client.py new file mode 100644 index 0000000..63dfdd8 --- /dev/null +++ b/services/console_client/console_client.py @@ -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() \ No newline at end of file