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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 799 |
class BaseTranslator(ABC):
|
| 800 |
"""
|
| 801 |
Abstract class that serve as a base translator for other different translators
|
|
|
|
| 796 |
"zulu": "zu",
|
| 797 |
}
|
| 798 |
|
| 799 |
+
class BaseError(Exception):
|
| 800 |
+
"""
|
| 801 |
+
base error structure class
|
| 802 |
+
"""
|
| 803 |
+
|
| 804 |
+
def __init__(self, val, message):
|
| 805 |
+
"""
|
| 806 |
+
@param val: actual value
|
| 807 |
+
@param message: message shown to the user
|
| 808 |
+
"""
|
| 809 |
+
self.val = val
|
| 810 |
+
self.message = message
|
| 811 |
+
super().__init__()
|
| 812 |
+
|
| 813 |
+
def __str__(self):
|
| 814 |
+
return "{} --> {}".format(self.val, self.message)
|
| 815 |
+
|
| 816 |
+
|
| 817 |
+
class LanguageNotSupportedException(BaseError):
|
| 818 |
+
"""
|
| 819 |
+
exception thrown if the user uses a language
|
| 820 |
+
that is not supported by the deep_translator
|
| 821 |
+
"""
|
| 822 |
+
|
| 823 |
+
def __init__(
|
| 824 |
+
self, val, message="There is no support for the chosen language"
|
| 825 |
+
):
|
| 826 |
+
super().__init__(val, message)
|
| 827 |
+
|
| 828 |
+
|
| 829 |
+
class NotValidPayload(BaseError):
|
| 830 |
+
"""
|
| 831 |
+
exception thrown if the user enters an invalid payload
|
| 832 |
+
"""
|
| 833 |
+
|
| 834 |
+
def __init__(
|
| 835 |
+
self,
|
| 836 |
+
val,
|
| 837 |
+
message="text must be a valid text with maximum 5000 character,"
|
| 838 |
+
"otherwise it cannot be translated",
|
| 839 |
+
):
|
| 840 |
+
super(NotValidPayload, self).__init__(val, message)
|
| 841 |
+
|
| 842 |
+
|
| 843 |
+
class InvalidSourceOrTargetLanguage(BaseError):
|
| 844 |
+
"""
|
| 845 |
+
exception thrown if the user enters an invalid payload
|
| 846 |
+
"""
|
| 847 |
+
|
| 848 |
+
def __init__(self, val, message="Invalid source or target language!"):
|
| 849 |
+
super(InvalidSourceOrTargetLanguage, self).__init__(val, message)
|
| 850 |
+
|
| 851 |
+
|
| 852 |
+
class TranslationNotFound(BaseError):
|
| 853 |
+
"""
|
| 854 |
+
exception thrown if no translation was found for the text provided by the user
|
| 855 |
+
"""
|
| 856 |
+
|
| 857 |
+
def __init__(
|
| 858 |
+
self,
|
| 859 |
+
val,
|
| 860 |
+
message="No translation was found using the current translator. Try another translator?",
|
| 861 |
+
):
|
| 862 |
+
super(TranslationNotFound, self).__init__(val, message)
|
| 863 |
+
|
| 864 |
+
|
| 865 |
+
class ElementNotFoundInGetRequest(BaseError):
|
| 866 |
+
"""
|
| 867 |
+
exception thrown if the html element was not found in the body parsed by beautifulsoup
|
| 868 |
+
"""
|
| 869 |
+
|
| 870 |
+
def __init__(
|
| 871 |
+
self, val, message="Required element was not found in the API response"
|
| 872 |
+
):
|
| 873 |
+
super(ElementNotFoundInGetRequest, self).__init__(val, message)
|
| 874 |
+
|
| 875 |
+
|
| 876 |
+
class NotValidLength(BaseError):
|
| 877 |
+
"""
|
| 878 |
+
exception thrown if the provided text exceed the length limit of the translator
|
| 879 |
+
"""
|
| 880 |
+
|
| 881 |
+
def __init__(self, val, min_chars, max_chars):
|
| 882 |
+
message = f"Text length need to be between {min_chars} and {max_chars} characters"
|
| 883 |
+
super(NotValidLength, self).__init__(val, message)
|
| 884 |
+
|
| 885 |
+
|
| 886 |
+
class RequestError(Exception):
|
| 887 |
+
"""
|
| 888 |
+
exception thrown if an error occurred during the request call, e.g a connection problem.
|
| 889 |
+
"""
|
| 890 |
+
|
| 891 |
+
def __init__(
|
| 892 |
+
self,
|
| 893 |
+
message="Request exception can happen due to an api connection error. "
|
| 894 |
+
"Please check your connection and try again",
|
| 895 |
+
):
|
| 896 |
+
self.message = message
|
| 897 |
+
|
| 898 |
+
def __str__(self):
|
| 899 |
+
return self.message
|
| 900 |
+
|
| 901 |
+
|
| 902 |
+
|
| 903 |
+
class TooManyRequests(Exception):
|
| 904 |
+
"""
|
| 905 |
+
exception thrown if an error occurred during the request call, e.g a connection problem.
|
| 906 |
+
"""
|
| 907 |
+
|
| 908 |
+
def __init__(
|
| 909 |
+
self,
|
| 910 |
+
message="Server Error: You made too many requests to the server."
|
| 911 |
+
"According to google, you are allowed to make 5 requests per second"
|
| 912 |
+
"and up to 200k requests per day. You can wait and try again later or"
|
| 913 |
+
"you can try the translate_batch function",
|
| 914 |
+
):
|
| 915 |
+
self.message = message
|
| 916 |
+
|
| 917 |
+
def __str__(self):
|
| 918 |
+
return self.message
|
| 919 |
+
|
| 920 |
+
class ServerException(Exception):
|
| 921 |
+
"""
|
| 922 |
+
Default YandexTranslate exception from the official website
|
| 923 |
+
"""
|
| 924 |
+
|
| 925 |
+
errors = {
|
| 926 |
+
400: "ERR_BAD_REQUEST",
|
| 927 |
+
401: "ERR_KEY_INVALID",
|
| 928 |
+
402: "ERR_KEY_BLOCKED",
|
| 929 |
+
403: "ERR_DAILY_REQ_LIMIT_EXCEEDED",
|
| 930 |
+
404: "ERR_DAILY_CHAR_LIMIT_EXCEEDED",
|
| 931 |
+
413: "ERR_TEXT_TOO_LONG",
|
| 932 |
+
429: "ERR_TOO_MANY_REQUESTS",
|
| 933 |
+
422: "ERR_UNPROCESSABLE_TEXT",
|
| 934 |
+
500: "ERR_INTERNAL_SERVER_ERROR",
|
| 935 |
+
501: "ERR_LANG_NOT_SUPPORTED",
|
| 936 |
+
503: "ERR_SERVICE_NOT_AVAIBLE",
|
| 937 |
+
}
|
| 938 |
+
|
| 939 |
+
def __init__(self, status_code, *args):
|
| 940 |
+
message = self.errors.get(status_code, "API server error")
|
| 941 |
+
super(ServerException, self).__init__(message, *args)
|
| 942 |
+
|
| 943 |
+
def is_empty(text: str) -> bool:
|
| 944 |
+
return text == ""
|
| 945 |
+
|
| 946 |
+
|
| 947 |
+
def request_failed(status_code: int) -> bool:
|
| 948 |
+
"""Check if a request has failed or not.
|
| 949 |
+
A request is considered successfull if the status code is in the 2** range.
|
| 950 |
+
|
| 951 |
+
Args:
|
| 952 |
+
status_code (int): status code of the request
|
| 953 |
+
|
| 954 |
+
Returns:
|
| 955 |
+
bool: indicates request failure
|
| 956 |
+
"""
|
| 957 |
+
if status_code > 299 or status_code < 200:
|
| 958 |
+
return True
|
| 959 |
+
return False
|
| 960 |
+
|
| 961 |
+
|
| 962 |
+
def is_input_valid(
|
| 963 |
+
text: str, min_chars: int = 0, max_chars: Optional[int] = None
|
| 964 |
+
) -> bool:
|
| 965 |
+
"""
|
| 966 |
+
validate the target text to translate
|
| 967 |
+
@param min_chars: min characters
|
| 968 |
+
@param max_chars: max characters
|
| 969 |
+
@param text: text to translate
|
| 970 |
+
@return: bool
|
| 971 |
+
"""
|
| 972 |
+
|
| 973 |
+
if not isinstance(text, str):
|
| 974 |
+
raise NotValidPayload(text)
|
| 975 |
+
if max_chars and (not min_chars <= len(text) < max_chars):
|
| 976 |
+
raise NotValidLength(text, min_chars, max_chars)
|
| 977 |
+
|
| 978 |
+
return True
|
| 979 |
+
|
| 980 |
class BaseTranslator(ABC):
|
| 981 |
"""
|
| 982 |
Abstract class that serve as a base translator for other different translators
|