o
    Ph                     @   sd   d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 G dd deZG dd	 d	eZd
S )    )UnsupportedMethod)Language)BaseTranslateExceptionBaseTranslator)Tuple)Requestc                   @   s   e Zd ZddiZdS )MyMemoryExceptionNO_MATCHz$There is no match to the translationN)__name__
__module____qualname__error_codes r   r   [/var/www/html/env_mimamsha/lib/python3.10/site-packages/translatepy/translators/mymemory.pyr      s    r   c                
   @   s   e Zd ZdZe fdefddZdededede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fddZdefddZdS )MyMemoryTranslatez2
    translatepy's implementation of MyMemory
    requestc                 C   s   || _ d| _d S )Nz'https://api.mymemory.translated.net/get)sessionbase_url)selfr   r   r   r   __init__   s   
zMyMemoryTranslate.__init__textdestination_languagesource_languagereturnc                 C   s   | j j| j||d | dd}|jdk rHz
| d d }W n ty*   tdw z|d d	d }W n tyA   |}Y nw ||d
 fS dS )zp
        This is the translating endpoint

        Must return a tuple with (detected_language, result)
        |qlangpairparamsi  matchesr   r	   source-translationN)	r   getr   status_codejson
IndexErrorr   split	Exception)r   r   r   r   r   result_detected_languager   r   r   
_translate   s    
zMyMemoryTranslate._translatec                 C   s8   | j j| j|ddd}|  | d d }|d S )zn
        This is the language detection endpoint

        Must return a string with the language code
        zautodetect|enr   r   r    r   r!   )r   r$   r   raise_for_statusr&   )r   r   r   r*   r   r   r   	_language)   s   zMyMemoryTranslate._languagelanguagec                 C   s   |j dkrdS |jS )z
        This is the language validation function
        It receives a "translatepy.language.Language" object and returns the correct language code

        Must return a string with the correct language code
        auto
autodetect)idalpha2)r   r/   r   r   r   _language_normalize5   s   
z%MyMemoryTranslate._language_normalizec                 C   sB   t |dd }|dkrtdS t | dv rtdS t|S )z
        This is the language denormalization function
        It receives a string with the translator language code and returns a "translatepy.language.Language" object

        Must return a string with the correct language code
        r"   r   r1   r0   >   zh-cnzhzho)strr(   r   lower)r   language_coder   r   r   _language_denormalize@   s   z'MyMemoryTranslate._language_denormalizec                 C   s   dS )NMyMemoryr   )r   r   r   r   __str__N   s   zMyMemoryTranslate.__str__N)r
   r   r   __doc__r   r   r8   r   r,   r.   r   r4   r;   r=   r   r   r   r   r      s    "r   N)translatepy.exceptionsr   translatepy.languager   translatepy.translators.baser   r   translatepy.utils.annotationsr   translatepy.utils.requestr   r   r   r   r   r   r   <module>   s    