o
    NK&h                     @   sh   d Z dZddlmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ ddlmZmZ G dd	 d	eZd
S )zbase translator classz"Copyright (C) 2020 Nidhal Baccouri    )ABCabstractmethod)Path)ListOptionalUnion)GOOGLE_LANGUAGES_TO_CODES)InvalidSourceOrTargetLanguageLanguageNotSupportedExceptionc                       s:  e Zd ZdZdedddddfdedededed	ee d
ee dee f fddZe	dd Z
e
jdd Z
e	dd Zejdd Zdd Zdd ZdefddZ	d0dedeeef fddZdedefd d!Zed"edefd#d$Zd%efd&d'Zd%efd(d)Zd*edefd+d,Zd-ee dee fd.d/Z  ZS )1BaseTranslatorzX
    Abstract class that serve as a base translator for other different translators
    Nautoenbase_url	languagessourcetargetpayload_keyelement_tagelement_queryc           	         sn   || _ || _t| j | _|st||st|| ||\| _| _|| _	|| _
|| _|| _t   dS )zy
        @param source: source language to translate from
        @param target: target language to translate to
        N)	_base_url
_languageslistkeys_supported_languagesr	   _map_language_to_code_source_target_url_params_element_tag_element_queryr   super__init__)	selfr   r   r   r   r   r   r   
url_params	__class__ G/var/www/html/venv/lib/python3.10/site-packages/deep_translator/base.pyr!      s   zBaseTranslator.__init__c                 C      | j S Nr   r"   r&   r&   r'   r   3      zBaseTranslator.sourcec                 C   
   || _ d S r)   r*   r"   langr&   r&   r'   r   7      
c                 C   r(   r)   r   r+   r&   r&   r'   r   ;   r,   zBaseTranslator.targetc                 C   r-   r)   r1   r.   r&   r&   r'   r   ?   r0   c                 C   s   | j jS r)   )r%   __name__r+   r&   r&   r'   _typeC   s   zBaseTranslator._typec                 g   s\    |D ](}|| j  v s|dkr|V  q|| j  v r"| j | V  qt|d| j  ddS )a   
        map language to its corresponding code (abbreviation) if the language was passed
        by its full name by the user
        @param languages: list of languages
        @return: mapped value of the language or raise an exception if the language is
        not supported
        r   zSNo support for the provided language.
Please select on of the supported languages:
)messageN)r   valuesr   r
   )r"   r   languager&   r&   r'   r   F   s   z$BaseTranslator._map_language_to_codereturnc                 C   s   | j | jkS r)   )r   r   r+   r&   r&   r'   _same_source_target[   s   z"BaseTranslator._same_source_targetFas_dictc                 K   s   |s| j S | jS )z
        return the supported languages by the Google translator
        @param as_dict: if True, the languages will be returned as a dictionary
        mapping languages to their abbreviations
        @return: list or dict
        )r   r   )r"   r9   kwargsr&   r&   r'   get_supported_languages^   s   	z&BaseTranslator.get_supported_languagesr6   c                 K   s,   |dks|| j  v s|| j  v rdS dS )z
        check if the language is supported by the translator
        @param language: a string for 1 language
        @return: bool or raise an Exception
        r   TF)r   r   r5   )r"   r6   r:   r&   r&   r'   is_language_supportedi   s
   z$BaseTranslator.is_language_supportedtextc                 K   s   t dS )z
        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
        z+You need to implement the translate method!)NotImplemented)r"   r=   r:   r&   r&   r'   	translatex   s   	zBaseTranslator.translatefc                 C   s   dd l }||S Nr   )docx2txtprocess)r"   r@   rB   r&   r&   r'   
_read_docx   s   
zBaseTranslator._read_docxc                 C   s$   dd l }||}|jd }| S rA   )pypdf	PdfReaderpagesextract_text)r"   r@   rE   readerpager&   r&   r'   	_read_pdf   s   

zBaseTranslator._read_pdfpathc                 K   s   t |ts	t|}| std td |j}|dkr%| jt|d}n*|dkr2| jt|d}nt	|ddd}|
  }W d	   n1 sJw   Y  | |S )
z
        translate directly from file
        @param path: path to the target file
        @type path: str
        @param kwargs: additional args
        @return: str
        zPath to the file is wrong!   z.docx)r@   z.pdfrzutf-8)encodingN)
isinstancer   existsprintexitsuffixrD   strrK   openreadstripr?   )r"   rL   r:   extr=   r@   r&   r&   r'   _translate_file   s   

zBaseTranslator._translate_filebatchc                 K   sB   |st dg }t|D ]\}}| j|fi |}|| q|S )z
        translate a list of texts
        @param batch: list of texts you want to translate
        @return: list of translations
        z/Enter your text list that you want to translate)	Exception	enumerater?   append)r"   r[   r:   arrir=   
translatedr&   r&   r'   _translate_batch   s   zBaseTranslator._translate_batch)F)r2   
__module____qualname____doc__r   rU   dictr   r!   propertyr   setterr   r3   r   boolr8   r   r   r;   r<   r   r?   rD   rK   rZ   r   rb   __classcell__r&   r&   r$   r'   r      s`    






"r   N)re   __copyright__abcr   r   pathlibr   typingr   r   r   deep_translator.constantsr   deep_translator.exceptionsr	   r
   r   r&   r&   r&   r'   <module>   s    