100 lines
3.5 KiB
Python
100 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from typing import Self, Callable, Any, Optional
|
|
from threading import Thread
|
|
from Interfaces.Application.AnPInterface import AnPInterface
|
|
from Utils.Checks import Check
|
|
from Utils.Common import Common
|
|
from Models.CommandModel import CommandModel
|
|
|
|
class TerminalManager:
|
|
|
|
def __init__(self:Self, anp:AnPInterface) -> None:
|
|
|
|
self.anp:AnPInterface = anp
|
|
self.__commands:list[CommandModel] = []
|
|
self.__thread:Thread = Thread(target = self.__handler)
|
|
|
|
self.update()
|
|
|
|
self.__thread.start()
|
|
|
|
def update(self:Self) -> None:
|
|
|
|
key:str
|
|
|
|
for keys, callback in (
|
|
(["close", "exit", "quit", "bye"], self.__close_command),
|
|
(["update", "upgrade"], self.__update_command),
|
|
(["reset"], self.__reset_command),
|
|
(["help", "h", "?"], self.__help_command)
|
|
):
|
|
if not any(name in command_model.names for command_model in self.__commands for name in keys):
|
|
self.__commands.append(CommandModel(keys, callback))
|
|
|
|
for key in ("default_commands_files", "commands_files", "default_commands", "commands"):
|
|
self.add(self.anp.settings.get(key), True)
|
|
|
|
def reset(self:Self) -> None:
|
|
|
|
self.__commands = []
|
|
|
|
self.update()
|
|
|
|
def __handler(self:Self) -> None:
|
|
while self.anp.working():
|
|
try:
|
|
|
|
order:str = input()
|
|
command:str = order.split(" ")[0].lower()
|
|
parameters:dict[str, Any|None] = {}
|
|
arguments:list[Any|None] = []
|
|
done:bool = False
|
|
|
|
for command_model in self.__commands:
|
|
if command in command_model.names:
|
|
command_model.callback(parameters, arguments)
|
|
done = True
|
|
break
|
|
|
|
if not done:
|
|
self.anp.print("warning", "terminal_manager_unknown_command", {
|
|
"command": command
|
|
})
|
|
|
|
except Exception as error:
|
|
self.anp.exception(error, "terminal_manager_exception")
|
|
|
|
def add(self:Self, inputs:Any|None, overwrite:bool = False) -> None:
|
|
if Check.is_array(inputs):
|
|
if len(inputs) == 2 and (
|
|
Check.is_key(inputs[0]) or
|
|
(Check.is_array(inputs[0]) and all(Check.is_key(name) for name in inputs[0]))
|
|
) and Check.is_function(inputs[1]):
|
|
|
|
i:int = 0
|
|
l:int = len(self.__commands)
|
|
keys:list[str] = Common.get_keys(inputs[0])
|
|
|
|
while i < l:
|
|
if any(name in self.__commands[i].names for name in keys):
|
|
break
|
|
i += 1
|
|
|
|
if i == l:
|
|
self.__commands.append(CommandModel(keys, inputs[1]))
|
|
else:
|
|
self.__commands[i].update(keys, inputs[1], overwrite)
|
|
|
|
def __close_command(self:Self, parameters:dict[str, Any|None], arguments:Optional[list[Any|None]]) -> None:
|
|
self.anp.close()
|
|
|
|
def __update_command(self:Self, parameters:dict[str, Any|None], arguments:Optional[list[Any|None]]) -> None:
|
|
self.anp.update()
|
|
|
|
def __reset_command(self:Self, parameters:dict[str, Any|None], arguments:Optional[list[Any|None]]) -> None:
|
|
self.anp.reset()
|
|
|
|
def __help_command(self:Self, parameters:dict[str, Any|None], arguments:Optional[list[Any|None]]) -> None:
|
|
pass |