LibreTranslatePlus/Python/Application/LibreTranslatePlus.Models.py

290 lines
11 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os.path import exists as path_exists
if "LibreTranslatePlus" not in globals():
class LibreTranslatePlus:pass
class Anonymous(LibreTranslatePlus.Abstracts.Base): # LibreTranslatePlus.Models
def __init__(self, ltp, _input = None):
super().__init__(ltp)
self._print("info", "ltp_models_building")
self.__started = False
self.__closed = False
self.__on = False
self.__models = {}
self.__set_common_variables()
self._print("ok", "ltp_models_built")
def start(self, callback = None):
self._print("info", "ltp_models_starting")
end = lambda status:callable(callback) and callback(status)
if self.__started:
self._print("warn", "ltp_models_already_started")
end(False)
return False
self.__started = True
self.__set_common_variables()
self.set(self.settings("models_files"), True)
self.settings("models_autolaunch") and self.launch()
self._print("ok", "ltp_models_started")
end(True)
def __set_common_variables(self):
self.__show_exception = self.settings(("models_show_exception", "show_exception_message"))
self.__show_validate_paths_error = self.settings(("models_show_validate_paths_error_message", "show_error_message"))
self.__show_validate_paths_ok = self.settings(("models_show_validate_paths_ok_message", "show_ok_message"))
self.__show_validate_path_error = self.settings(("models_show_validate_path_error_message", "show_error_message"))
self.__show_validate_path_ok = self.settings(("models_show_validate_path_ok_message", "show_ok_message"))
self.__show_set_error = self.settings(("models_show_set_error_message", "show_error_message"))
self.__show_set_ok = self.settings(("models_show_set_ok_message", "show_ok_message"))
self.__show_set_exception = self.settings(("models_show_set_exception_message", "show_exception_message"))
def validate_paths(self, paths, main_language, show_error = None):
error = (
1 << 0 if paths == None else
1 << 1 if not isinstance(paths, (str, list, tuple)) else
1 << 2 if not len(paths) else
0)
suberrors = []
custom_check = isinstance(show_error, bool)
if not error:
paths = [paths] if isinstance(paths, str) else list(paths)
for i, path in enumerate(paths):
suberrors.append(
1 << 0 if path == None else
1 << 1 if not isinstance(path, str) else
1 << 2 if not path else
0)
if not suberrors[i]:
paths[i] = self.ltp.fix_path(path)
suberrors[i] |= 1 << 3 if not path_exists(LibreTranslatePlus.string_variables(path, {
"key" : main_language
})) else 0
if not self.validate(
suberrors[i],
(
"path_null",
"path_not_string",
"path_empty",
"path_not_exists"
),
{
"path" : path
},
(show_error if custom_check else self.__show_validate_path_error) and "ltp_models_validate_path_error",
(show_error if custom_check else True) and self.__show_validate_path_ok and "ltp_models_validate_path_ok"
) and not error & (1 << 3):
error |= 1 << 3
return (paths, error, suberrors, self.validate(
error,
(
"paths_null",
"paths_bad_type",
"paths_empty",
"paths_with_errors"
),
{},
(show_error if custom_check else self.__show_validate_paths_error) and "ltp_models_validate_paths_error",
(show_error if custom_check else True) and self.__show_validate_paths_ok and "ltp_models_validate_paths_ok"
))
def set(self, inputs, overwrite = None):
if isinstance(inputs, (list, tuple)):
for subinputs in inputs:
self.set(subinputs, overwrite)
elif isinstance(inputs, dict):
if not isinstance(overwrite, bool):
overwrite = self.settings(("models_overwrite", "overwrite"))
for key, model in inputs.items():
error = 0
messages_variables = {
"key" : key,
**(model if isinstance(model, dict) else {})
}
# if overwrite or key not in self.__models:
# self.__models = model
try:
error |= (
((
1 << 0 if key == None else
1 << 1 if not isinstance(key, str) else
1 << 2 if not key else
1 << 3 if not overwrite and key in self.__models else
0) << 0) |
((
1 << 0 if model == None else
1 << 1 if not isinstance(model, dict) else
1 << 2 if not len(list(model.keys())) else
(
((
1 << 0 if "model" not in model else
1 << 1 if model["model"] == None else
1 << 2 if not isinstance(model["model"], str) else
1 << 3 if not model["model"] else
1 << 4 if not hasattr(LibreTranslatePlus.Models, model["model"]) else
0) << 3) |
(0 if "paths" in model else 1 << 8) |
(0 if "main_language" in model else 1 << 9) |
0)) << 4) |
0) << 1
if not error:
(main_language_code, main_language_error, main_language_language_ok) = self.ltp.i18n.validate_language(model["main_language"], self.__show_set_error)
(paths, paths_error, each_path_error, paths_ok) = self.validate_paths(model["paths"], self.ltp.i18n.get_key(model["main_language"])[0], self.__show_set_error)
error |= (
(paths_error << 0) |
(main_language_error << 4) |
0) << 15
if not error:
self.__models[key] = {
"model_name" : model["model"],
"paths" : paths,
"main_language" : main_language_code,
"key" : key,
"model" : None,
"timer" : model["timer"] if "timer" in model else self.settings("models_default_timer")
}
keys = list(self.__models[key].keys())
for subkey, value in model.items():
if subkey not in keys:
self.__models[key][subkey] = value
self.__models[key]["model"] = getattr(LibreTranslatePlus.Models, model["model"])(self.ltp, self.__models[key])
except Exception as exception:
error |= 1 << 0
self.__show_set_exception and self.exception(exception, "ltp_models_set_exception", messages_variables)
self.validate(
error,
(
"exception",
"key_null",
"key_not_string",
"key_empty",
"key_exists",
"model_data_null",
"model_data_not_dictionary",
"model_data_empty",
"model_not_defined",
"model_null",
"model_not_string",
"model_empty",
"model_not_exists",
"model_no_paths",
"model_no_main_language",
"paths_null",
"paths_bad_type",
"paths_empty",
"paths_with_errors",
"language_key_null",
"language_key_not_string",
"language_key_empty",
"language_key_not_in_codes",
"language_key_not_actived"
),
messages_variables,
self.__show_set_error and "ltp_models_set_error",
self.__show_set_ok and "ltp_models_set_ok"
)
elif isinstance(inputs, str):
self.set(self.ltp.load_json(inputs, self.__show_exception), overwrite)
def close(self, callback = None):
self._print("info", "ltp_models_closing")
end = lambda status:callable(callback) and callback(status)
if self.__closed:
self._print("warn", "ltp_models_already_closed")
end(False)
return False
self.__closed = True
self.stop()
end(True)
self._print("ok", "ltp_models_closed")
return True
def launch(self, callback = None):
self._print("info", "ltp_models_launching")
end = lambda status:callable(callback) and callback(status)
if self.__on:
self._print("warn", "ltp_models_already_launched")
end(False)
return False
self.__on = True
for model in self.__models.values():
model and model["model"].start()
end(True)
self._print("ok", "ltp_models_launched")
return True
def stop(self, callback = None):
self._print("info", "ltp_models_stopping")
end = lambda status:callable(callback) and callback(status)
if not self.__on:
self._print("warn", "ltp_models_already_stopped")
end(False)
return False
self.__on = False
for model in self.__models.values():
model and model["model"].stop()
end(True)
self._print("ok", "ltp_models_stopped")
return True
LibreTranslatePlus.Models = Anonymous
del globals()["Anonymous"]