200 lines
7.6 KiB
Python
200 lines
7.6 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from re import compile as re_compile
|
|
|
|
if "LibreTranslatePlus" not in globals():
|
|
class LibreTranslatePlus:pass
|
|
|
|
class Anonymous(LibreTranslatePlus.Abstracts.Base): # LibreTranslatePlus.Connections
|
|
|
|
re_url = re_compile(r'^https?\:\/{2}')
|
|
|
|
def __init__(self, ltp, _input = None):
|
|
super().__init__(ltp)
|
|
|
|
self._print("info", "ltp_connections_building")
|
|
|
|
self.__started = False
|
|
self.__connections = {}
|
|
self.__show_exception = None
|
|
|
|
self._print("ok", "ltp_connections_built")
|
|
|
|
def start(self, callback = None):
|
|
|
|
self._print("info", "ltp_connections_starting")
|
|
|
|
end = lambda status:callable(callback) and callback(status)
|
|
|
|
if self.__started:
|
|
self._print("warn", "ltp_connections_already_started")
|
|
end(False)
|
|
return False
|
|
self.__started = True
|
|
|
|
self.__show_exception = self.settings(("connections_show_exception_message", "show_exception_message"))
|
|
|
|
self.__show_add_exception = self.settings(("connections_show_add_exception_message", "show_error_message"))
|
|
self.__show_add_error = self.settings(("connections_show_add_error_message", "show_ok_message"))
|
|
self.__show_add_ok = self.settings(("connections_show_add_ok_message", "show_exception_message"))
|
|
|
|
self.__show_get_error = self.settings(("connections_show_get_error_message", "show_error_message"))
|
|
self.__show_get_ok = self.settings(("connections_show_get_ok_message", "show_ok_message"))
|
|
|
|
for key in ("connections_default_connections", "default_connections", "connections"):
|
|
self.add(self.settings(key), True)
|
|
|
|
self._print("ok", "ltp_connections_started")
|
|
|
|
end(True)
|
|
|
|
def add(self, inputs, overwrite = None):
|
|
if isinstance(inputs, (list, tuple)):
|
|
for subinputs in inputs:
|
|
self.add(subinputs, overwrite)
|
|
elif isinstance(inputs, dict):
|
|
if not isinstance(overwrite, bool):
|
|
overwrite = self.settings(("connections_overwrite", "overwrite"))
|
|
for key, connection in inputs.items():
|
|
|
|
error = 0
|
|
|
|
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.__connections else
|
|
0) << 0) |
|
|
((
|
|
1 << 0 if connection == None else
|
|
1 << 1 if not isinstance(connection, dict) else
|
|
(
|
|
# ((
|
|
# 1 << 0 if "url" not in connection else
|
|
# 1 << 1 if connection["url"] == None else
|
|
# 1 << 2 if not isinstance(connection["url"], str) else
|
|
# 1 << 3 if not connection["url"] else
|
|
# 1 << 4 if not LibreTranslatePlus.Connections.re_url.match(connection["url"]) else
|
|
# 0) << 2) |
|
|
((
|
|
1 << 0 if "type" not in connection else
|
|
1 << 1 if connection["type"] == None else
|
|
1 << 2 if not isinstance(connection["type"], str) else
|
|
1 << 3 if not connection["type"] else
|
|
1 << 4 if not hasattr(LibreTranslatePlus.Drivers, connection["type"]) else
|
|
0) << 7) |
|
|
0) |
|
|
0) << 4) |
|
|
0) << 1
|
|
|
|
if not error:
|
|
self.__connections[key] = getattr(LibreTranslatePlus.Drivers, connection["type"])(self.ltp, {
|
|
"key" : key,
|
|
**connection
|
|
})
|
|
|
|
except Exception as exception:
|
|
error |= 1 << 0
|
|
self.exception(exception, self.__show_add_exception and "ltp_connections_add_exception", {
|
|
"key" : key,
|
|
**(connection if isinstance(connection, dict) else {})
|
|
})
|
|
|
|
self.validate(
|
|
error,
|
|
(
|
|
"exception",
|
|
"key_null",
|
|
"key_not_string",
|
|
"key_empty",
|
|
"key_exists",
|
|
"connection_data_null",
|
|
"connection_data_not_dictionary",
|
|
"connection_no_url",
|
|
"url_null",
|
|
"url_not_string",
|
|
"url_empty",
|
|
"url_bad_protocol",
|
|
"connection_no_type",
|
|
"connection_type_null",
|
|
"connection_type_not_string",
|
|
"connection_type_empty",
|
|
"connection_type_unknown"
|
|
),
|
|
{
|
|
"key" : key,
|
|
**(connection if isinstance(connection, dict) else {})
|
|
},
|
|
self.__show_add_error and "ltp_connections_add_error",
|
|
self.__show_add_ok and "ltp_connections_add_ok"
|
|
)
|
|
|
|
elif isinstance(inputs, str):
|
|
self.add(self.ltp.load_json(inputs, self.__show_exception), overwrite)
|
|
|
|
def get(self, key, show_errors = None):
|
|
|
|
(has, error) = self.has(key, show_errors)
|
|
|
|
return (self.__connections[key] if has else None, error)
|
|
|
|
def has(self, key, show_errors = None):
|
|
|
|
error = (
|
|
1 << 0 if key == None else
|
|
1 << 1 if not isinstance(key, str) else
|
|
1 << 2 if not key else
|
|
1 << 3 if key not in self.__connections else
|
|
1 << 4 if self.__connections[key] == None else
|
|
0)
|
|
has_show_errors = isinstance(show_errors, bool)
|
|
|
|
self.validate(
|
|
error,
|
|
(
|
|
"key_null",
|
|
"key_not_string",
|
|
"key_empty",
|
|
"key_unknown",
|
|
"key_deleted"
|
|
),
|
|
{
|
|
"key" : key
|
|
},
|
|
(show_errors if has_show_errors else self.__show_get_error) and "ltp_connections_get_error",
|
|
(show_errors if has_show_errors else True) and self.__show_get_ok and "ltp_connections_get_ok"
|
|
)
|
|
|
|
return (not error, error)
|
|
|
|
def _translate_command(self, attributes, parameters):
|
|
|
|
if len(parameters) != 4:
|
|
self._print("warn", "ltp_connections_translate_command_error")
|
|
return
|
|
|
|
connection = self.get(parameters[0])[0]
|
|
|
|
if connection:
|
|
translated = connection.translate(parameters[1], parameters[2], parameters[3])
|
|
if translated:
|
|
print(translated[0])
|
|
|
|
def _languages_command(self, attributes, parameters):
|
|
|
|
if len(parameters) != 1:
|
|
self._print("warn", "ltp_connections_languages_command_error")
|
|
return
|
|
|
|
connection = self.get(parameters[0])[0]
|
|
|
|
if connection:
|
|
print(connection.languages())
|
|
|
|
|
|
LibreTranslatePlus.Connections = Anonymous
|
|
del globals()["Anonymous"] |