File size: 2,445 Bytes
f36740b
28c2eed
9a69a65
58568a7
f06c14e
 
58568a7
f06c14e
f36740b
f06c14e
 
 
 
 
58568a7
f06c14e
 
 
 
 
 
 
 
 
 
 
 
 
58568a7
eaf7d7b
 
a06172d
58568a7
f06c14e
9a69a65
 
f06c14e
f36740b
f06c14e
 
 
 
9a69a65
 
 
f36740b
f06c14e
 
9a69a65
 
f36740b
 
 
 
 
 
 
f06c14e
 
58568a7
a06172d
f36740b
 
 
 
 
 
f6bfd56
f06c14e
eaf7d7b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""parent translator class"""

from deep_translator.exceptions import NotValidPayload, NotValidLength
from abc import ABC, abstractmethod


class BaseTranslator(ABC):
    """
    Abstract class that serve as a parent translator for other different translators
    """
    def __init__(self,
                 base_url=None,
                 source="auto",
                 target="en",
                 payload_key=None,
                 element_tag=None,
                 element_query=None,
                 **url_params):
        """
        @param source: source language to translate from
        @param target: target language to translate to
        """
        self.__base_url = base_url
        self._source = source
        self._target = target
        self._url_params = url_params
        self._element_tag = element_tag
        self._element_query = element_query
        self.payload_key = payload_key
        self.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) '
                                      'AppleWebit/535.19'
                                      '(KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19'}
        super(BaseTranslator, self).__init__()

    @staticmethod
    def _validate_payload(payload, min_chars=1, max_chars=5000):
        """
        validate the target text to translate
        @param payload: text to translate
        @return: bool
        """

        if not payload or not isinstance(payload, str):
            raise NotValidPayload(payload)
        if not BaseTranslator.__check_length(payload, min_chars, max_chars):
            raise NotValidLength(payload, min_chars, max_chars)
        return True

    @staticmethod
    def __check_length(payload, min_chars, max_chars):
        """
        check length of the provided target text to translate
        @param payload: text to translate
        @param min_chars: minimum characters allowed
        @param max_chars: maximum characters allowed
        @return: bool
        """
        return True if min_chars < len(payload) < max_chars else False

    @abstractmethod
    def translate(self, text, **kwargs):
        """
        translate a text using a translator under the hood and return the translated text
        @param text: text to translate
        @param kwargs: additional arguments
        @return: str
        """
        return NotImplemented('You need to implement the translate method!')