U
    =g$                     @   s   d dl Z d dlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ e de je jB ZG dd	 d	eZe ZejZdS )
    N)repeat   )	LIGATURES)auto_config)UNSHAPEDISOLATEDTATWEELZWJLETTERS_ARABICLETTERS_ARABIC_V2LETTERS_KURDISHFINALINITIALMEDIAL&connects_with_letters_before_and_afterconnects_with_letter_beforeconnects_with_letter_afteru2   [ؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۨ-ۭࣔ-࣡ࣔ-࣭ࣣ-ࣿ]c                       s>   e Zd ZdZd fdd	Zedd Zdd Zd	d
 Z  Z	S )ArabicReshapera  
    A class for Arabic reshaper, it allows for fine-tune configuration over the
    API.

    If no configuration is passed to the constructor, the class will check for
    an environment variable :envvar:`PYTHON_ARABIC_RESHAPER_CONFIGURATION_FILE`
    , if the variable is available, the class will load the file pointed to by
    the variable, and will read it as an ini file.
    If the variable doesn't exist, the class will load with the default
    configuration file :file:`default-config.ini`

    Check these links for information on the configuration files format:

    * Python 3: https://docs.python.org/3/library/configparser.html

    See the default configuration file :file:`default-config.ini` for details
    on how to configure your reshaper.
    Nc                    sV   t t|   t||| _| jd| _| jdkr:t| _n| jdkrLt	| _nt
| _d S )NlanguageZArabicV2ZKurdish)superr   __init__r   configurationgetr   r   lettersr   r
   )selfr   Zconfiguration_file	__class__ C/tmp/pip-unpacked-wheel-0wpw7hns/arabic_reshaper/arabic_reshaper.pyr   :   s    

zArabicReshaper.__init__c           	      C   s   t | dsg }i }d}d}d}tD ]B}|\}}| j|s<q"|| ||< |d||  |d7 }q"|| _td	|tj
| _| jS )NZ__ligatures_rer   r   z({})|)hasattrr   r   
getbooleanappendformat!_re_group_index_to_ligature_formsrecompilejoinUNICODEZ_ArabicReshaper__ligatures_re)	r   patternsZ re_group_index_to_ligature_formsindexZFORMSMATCHZligature_recordZligaturereplacementr   r   r   _ligatures_reG   s"    

zArabicReshaper._ligatures_rec                 C   s   t | ds| jS | j| S )Nr$   )r    r-   r$   )r   group_indexr   r   r   '_get_ligature_forms_from_re_group_index\   s    
z6ArabicReshaper._get_ligature_forms_from_re_group_indexc                 C   s  |sdS g }d}d}d}| j d}| j d}| j d}| j d}	| j d	}
i }|
r`tnt}|D ]}t|r|st|d }|	r|d8 }||krg ||< |	r|| d| n|| | n4|t	kr|rܐn$|t
kr|sn|| jkr|||f n|s|||f n|d }|| |krD|||f nt|| jsb|||f nt|| | js|||f n||| tkrt|| | js|||f nL|| |kr|| tf|d< ||tf n|| tf|d< ||tf |rht|dkrh|d
 | t
krh|t|d  qh|rZ|rZ|d | t
krZ|  | j drntd|}|r|t	d}t| j|D ]}tdd t| D d}| |}| \}}|| | }||d  | }d }||tfkr||tfkrt}nt}n||tfkr&t}nt}|| s8q|| |f||< td|f|d | ||d |< qg }|sd|kr||d  t|D ]z\}}|| r|| |ks|| tkr|||  n|| j||  ||   |s||kr|||  qd|S )N r   r   delete_harakatdelete_tatweelsupport_zwjshift_harakat_position use_unshaped_instead_of_isolated   Zsupport_ligaturesc                 s   s   | ]\}}|r|V  qd S )Nr   ).0igroupr   r   r   	<genexpr>   s     z)ArabicReshaper.reshape.<locals>.<genexpr>) r   r!   r   r   
HARAKAT_REmatchleninsertr"   r   r	   r   r   r   r   r   r   r   popsubreplacer%   finditerr-   next	enumerategroupsr/   spanr   extendr'   )r   textoutputZLETTERZFORMZNOT_SUPPORTEDr2   r3   r4   r5   r6   Zpositions_harakatZisolated_formletterpositionZprevious_letterr>   r.   ZformsabZa_formZb_formZligature_formresultr:   or   r   r   reshapea   s    

   

&

zArabicReshaper.reshape)NN)
__name__
__module____qualname____doc__r   propertyr-   r/   rR   __classcell__r   r   r   r   r   &   s   
r   )r%   	itertoolsr   Z	ligaturesr   Zreshaper_configr   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r&   r(   Xr=   objectr   Zdefault_reshaperrR   r   r   r   r   <module>
   s   <
 J