116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
if "LibreTranslatePlus" not in globals():
|
||
|
class LibreTranslatePlus:pass
|
||
|
if not hasattr(LibreTranslatePlus, "Abstracts"):
|
||
|
class Subanonymous:pass
|
||
|
LibreTranslatePlus.Abstracts = Subanonymous
|
||
|
del globals()["Subanonymous"]
|
||
|
|
||
|
class Anonymous(LibreTranslatePlus.Abstracts.Base): # LibreTranslatePlus.Abstracts.Models
|
||
|
|
||
|
__unknown_type_name = None
|
||
|
|
||
|
def __init__(self, ltp, _input):
|
||
|
super().__init__(ltp)
|
||
|
|
||
|
if LibreTranslatePlus.Abstracts.Models.__unknown_type_name == None:
|
||
|
LibreTranslatePlus.Abstracts.Models.__unknown_type_name = self.settings("model_unknown_name")
|
||
|
|
||
|
self.__type = self.settings("model_name", _input) or LibreTranslatePlus.Abstracts.Models.__unknown_type_name
|
||
|
self.__key = self.settings("key", _input)
|
||
|
self.__timer = self.settings(("default_model_timer", "timer"), _input)
|
||
|
self.__print_variables = {
|
||
|
"key" : self.__key,
|
||
|
"model_type" : self.__type,
|
||
|
"timer" : self.__timer
|
||
|
}
|
||
|
|
||
|
self._print("info", "ltp_abstracts_models_building", self.__print_variables)
|
||
|
|
||
|
self.__started = False
|
||
|
self.__ok = False
|
||
|
self.__thread = None
|
||
|
self.__error = (
|
||
|
((
|
||
|
1 << 0 if not hasattr(self, "_build") else
|
||
|
1 << 1 if self._build == None else
|
||
|
1 << 2 if not callable(self._build) else
|
||
|
0) << 0) |
|
||
|
((
|
||
|
1 << 0 if self.__key == None else
|
||
|
1 << 1 if not isinstance(self.__key, str) else
|
||
|
1 << 2 if not self.__key else
|
||
|
0) << 3) |
|
||
|
((
|
||
|
1 << 0 if not hasattr(self, "execute") else
|
||
|
1 << 1 if self.execute == None else
|
||
|
1 << 2 if not callable(self.execute) else
|
||
|
0) << 6) |
|
||
|
0)
|
||
|
self.__error_messages = [
|
||
|
"build_not_exists",
|
||
|
"build_null",
|
||
|
"build_not_function",
|
||
|
"key_null",
|
||
|
"key_not_string",
|
||
|
"key_empty",
|
||
|
"execute_not_exists",
|
||
|
"execute_null",
|
||
|
"execute_not_function"
|
||
|
]
|
||
|
self.__show_build_error = self.settings("abstracts_models_show_build_error_message")
|
||
|
self.__show_build_ok = self.settings("abstracts_models_show_build_ok_message")
|
||
|
|
||
|
not self.__error & ~-(1 << 3) and self._build(_input)
|
||
|
|
||
|
self.__ok = self.validate(
|
||
|
self.__error,
|
||
|
self.__error_messages,
|
||
|
self.__print_variables,
|
||
|
self.__show_build_error and "ltp_abstracts_models_build_error",
|
||
|
self.__show_build_ok and "ltp_abstracts_models_build_ok"
|
||
|
)
|
||
|
|
||
|
def start(self, callback = None):
|
||
|
|
||
|
end = lambda status:callable(callback) and callback(status)
|
||
|
|
||
|
if not self.__ok:
|
||
|
end(False)
|
||
|
return False
|
||
|
|
||
|
if self.__started:
|
||
|
end(False)
|
||
|
return False
|
||
|
self.__started = True
|
||
|
|
||
|
self.__thread = self.ltp.threads.add(lambda i:self.execute(i), {
|
||
|
"timer" : self.__timer,
|
||
|
"autostart" : True,
|
||
|
"bucle" : True,
|
||
|
"start_now" : False
|
||
|
})
|
||
|
|
||
|
end(True)
|
||
|
|
||
|
return True
|
||
|
|
||
|
def stop(self, callback = None):
|
||
|
|
||
|
end = lambda status:callable(callback) and callback(status)
|
||
|
|
||
|
if not self.__started:
|
||
|
end(False)
|
||
|
return False
|
||
|
self.__started = False
|
||
|
|
||
|
self.ltp.threads.remove(self.__thread)
|
||
|
|
||
|
end(True)
|
||
|
|
||
|
return True
|
||
|
|
||
|
LibreTranslatePlus.Abstracts.Models = Anonymous
|
||
|
del globals()["Anonymous"]
|