AnP/Public/py/Application/AnP.py

186 lines
7.7 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from common import Self, Optional, Any, Callable
from Utils.Utils import Utils
from Modules.ErrorsManager import ErrorsManager
from Abstracts.Base import BaseAbstract
from Drivers.PyodideJavaScriptInterface import PyodideJavaScriptInterface
from Drivers.Console import Console
from Drivers.JSON import JSON
from Drivers.URLPath import URLPath
from Managers.Settings import SettingsManager
from Managers.I18N import I18NManager
from Managers.RandomKeys import RandomKeysManager
from Managers.Threads import ThreadsManager
from Application.Attributes import Attributes
from Application.Components import Components
from Utils.Check import Check
if not Check.is_emscripten():
from Managers.Terminal import TerminalManager
class AnP:
def __tests(self:Self, *_:list[Any|None]) -> None:
self.console.print_item("test", ["jojo", 9, {"jojo" : True}])
self.request.get("https://ifconfig.me/ip", lambda data, *_:self.console.print_item("test", data))
def set_subbasics(self:Self, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None) -> None:
get:Callable[[
str|list[str]|tuple[str],
Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]],
Optional[Any]
], Any|None] = self.settings.get if hasattr(self, "settings") else Utils.get_value
self.__show_building = get(("anp_show_building_message", "show_info_message"), inputs, self.__show_building)
def set_basics(self:Self, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None) -> None:
self.set_subbasics(inputs)
self.__show_built = self.settings.get(("anp_show_built_message", "show_ok_message"), inputs, self.__show_built)
self.__show_starting = self.settings.get(("anp_show_starting_message", "show_info_message"), inputs, self.__show_starting)
self.__show_start_exception = self.settings.get(("anp_show_start_exception_message", "show_exception_message"), inputs, self.__show_start_exception)
self.__show_already_started = self.settings.get(("anp_show_already_started_message", "show_warning_message"), inputs, self.__show_already_started)
self.__show_started = self.settings.get(("anp_show_started_message", "show_ok_message"), inputs, self.__show_started)
self.__show_closing = self.settings.get(("anp_show_closing_message", "show_info_message"), inputs, self.__show_closing)
self.__show_close_exception = self.settings.get(("anp_show_close_exception_message", "show_exception_message"), inputs, self.__show_close_exception)
self.__show_already_closed = self.settings.get(("anp_show_already_closed_message", "show_warning_message"), inputs, self.__show_already_closed)
self.__show_closed = self.settings.get(("anp_show_closed_message", "show_ok_message"), inputs, self.__show_closed)
def __init__(self:Self, inputs:Optional[dict[str, Any|None]|list[Any|None]|tuple[Any|None]] = None) -> None:
self.__show_building:bool = True
self.errors:ErrorsManager = ErrorsManager()
self.console:Console = Console(self)
self.json:JSON = JSON(self, inputs)
self.pyodide:PyodideJavaScriptInterface = PyodideJavaScriptInterface(self, inputs)
self.set_subbasics()
self.__show_building and self.console.print("info", (
"The AnP Application is building...",
"anp_building"
))
is_emscripten:bool = Check.is_emscripten()
self.__started:bool = False
self.request:URLPath = URLPath(self, inputs)
self.settings:SettingsManager = SettingsManager(self, inputs)
self.i18n:I18NManager = I18NManager(self, inputs)
self.random_keys:RandomKeysManager = RandomKeysManager(self, inputs)
self.threads:ThreadsManager = ThreadsManager(self, inputs)
self.attributes:Attributes = Attributes(self, inputs)
self.components:Components = Components(self, inputs)
if not is_emscripten:
self.terminal:TerminalManager = TerminalManager(self, inputs)
self.__error:int = 0
self.__show_built:bool = True
self.__show_starting:bool = True
self.__show_start_exception:bool = True
self.__show_already_started:bool = True
self.__show_started:bool = True
self.__show_closing:bool = True
self.__show_close_exception:bool = True
self.__show_already_closed:bool = True
self.__show_closed:bool = True
self.set_basics()
self.__show_built and self.console.print("ok", (
"The AnP Application was built fully.",
"anp_built"
))
self.settings.get("autostart")[0] and self.start(self.__tests)
def __start_module(self:Self, key:str, end:Callable[[], None]) -> None:
if hasattr(self, key):
module:BaseAbstract|Any = getattr(self, key)
if isinstance(module, BaseAbstract):
module.start(lambda *_:Utils.execute(end))
return
Utils.execute(end)
def start(self, callback:Optional[Callable[[bool], None]] = None) -> bool:
ok:bool = False
is_emscripten:bool = Check.is_emscripten()
self.__show_starting and self.console.print("info", "anp_starting")
def end(status:bool) -> None:
if status:
self.__show_started and self.console.print("ok", "anp_started")
else:
self.__show_already_started and self.console.print("warn", "anp_already_started")
Utils.execute(callback, status)
return status
if self.__started:
return end(ok)
self.__started = True
ok = True
try:
Utils.execute_asynchronous((
("console", "json", "pyodide", "request", "settings", "i18n", "random_keys", "threads") +
(tuple() if is_emscripten else ("terminal",)) +
("attributes", "components")
), self.__start_module, lambda:Utils.execute(callback, True))
except Exception as exception:
self.__error |= 1 << 1
ok = False
self.__show_start_exception and self.console.exception(exception, "anp_start_exception")
end(ok)
return ok
def __close_module(self:Self, key:str, end:Callable[[], None]) -> None:
if hasattr(self, key):
module:BaseAbstract|Any = getattr(self, key)
if isinstance(module, BaseAbstract):
module.close(lambda *_:Utils.execute(end))
return
Utils.execute(end)
def close(self, callback:Optional[Callable[[bool], None]] = None) -> bool:
ok:bool = False
is_emscripten:bool = Check.is_emscripten()
self.__show_closing and self.console.print("info", "anp_closing")
def end(status:bool) -> None:
if status:
self.__show_closed and self.console.print("ok", "anp_closed")
else:
self.__show_already_closed and self.console.print("warn", "anp_already_closed")
Utils.execute(callback, status)
return status
if not self.__started:
return end(ok)
self.__started = False
ok = True
try:
Utils.execute_asynchronous((
("components", "attributes") +
(tuple() if is_emscripten else ("terminal",)) +
("i18n", "settings", "request", "pyodide", "json", "console")
), self.__close_module, lambda:Utils.execute(callback, True))
except Exception as exception:
self.__error |= 1 << 1
ok = False
self.__show_close_exception and self.console.exception(exception, "anp_close_exception")
end(ok)
return ok