o
    NK&h                     @   s   d Z dZddlZddlmZmZ ddlZddlmZ ddl	m
Z
mZmZ ddlmZmZmZmZ ddlmZmZmZ G d	d
 d
eZdS )z
LibreTranslate API
z"Copyright (C) 2020 Nidhal Baccouri    N)ListOptional)BaseTranslator)	BASE_URLSLIBRE_ENV_VARLIBRE_LANGUAGES_TO_CODES)ApiKeyExceptionAuthorizationExceptionServerExceptionTranslationNotFound)is_emptyis_input_validrequest_failedc                       s   e Zd ZdZddeedddfdededee d	e	d
ee f
 fddZ
de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 )LibreTranslatorzd
    class that wraps functions, which use libre translator under the hood to translate text(s)
    enesNTsourcetargetapi_keyuse_free_api
custom_urlc                    sL   |st td|| _|stdntd}t j|s|n|||td dS )a  
        @param api_key: your api key
        @param source: source language to translate from
        List of LibreTranslate endpoint can be found at :
        https://github.com/LibreTranslate/LibreTranslate#mirrors
        Some require an API key
        @param target: target language to translate to
        @param use_free_api: set True if you want to use the free api.
        This means a url that does not require and api key would be used
        @param custom_url: you can use a custom endpoint
        )env_varLIBRE
LIBRE_FREE)base_urlr   r   	languagesN)r   r   r   r   getsuper__init__r   )selfr   r   r   r   r   kwargsurl	__class__ H/var/www/html/venv/lib/python3.10/site-packages/deep_translator/libre.pyr      s   


zLibreTranslator.__init__textreturnc                 K   s   t |r\|  st|r|S d}|| j| jdd}| jr!| j|d< ztj| j| |d}W n t	y8   t
dw |jdkrCt| jt|jdrNt
|j| }|sXt||d	 S d
S )z
        function that uses microsoft translate to translate a text
        @param text: desired text to translate
        @return: str: translated text
        	translater&   )qr   r   formatr   )paramsi  i  )status_codetranslatedTextN)r   _same_source_targetr   _source_targetr   requestspost	_base_urlConnectionErrorr
   r,   r	   r   jsonr   )r   r&   r    translate_endpointr+   responseresr$   r$   r%   r(   C   s6   





zLibreTranslator.translatepathc                 K      | j |fi |S )z
        translate directly from file
        @param path: path to the target file
        @type path: str
        @param kwargs: additional args
        @return: str
        )_translate_file)r   r9   r    r$   r$   r%   translate_filek   s   zLibreTranslator.translate_filebatchc                 K   r:   )z
        translate a list of texts
        @param batch: list of texts you want to translate
        @return: list of translations
        )_translate_batch)r   r=   r    r$   r$   r%   translate_batchu   s   zLibreTranslator.translate_batch)__name__
__module____qualname____doc__osgetenvr   strr   boolr   r(   r<   r   r?   __classcell__r$   r$   r"   r%   r      s*    
$("
r   )rC   __copyright__rD   typingr   r   r1   deep_translator.baser   deep_translator.constantsr   r   r   deep_translator.exceptionsr   r	   r
   r   deep_translator.validater   r   r   r   r$   r$   r$   r%   <module>   s    