U
    -e]                     @   s@   d dl mZ ddlmZ eeZG dd dZG dd dZdS )	    )import_module   )
get_loggerc                   @   s   e Zd ZdZdddZdS )_PatchedModuleObjzMSet all the modules components as attributes of the _PatchedModuleObj object.Nc                 C   sZ   |pg }|d k	r@|j D ](}||ks,|dst| |t|| qt|trP|jn|| _d S )N__)__dict__
startswithsetattrgetattr
isinstancer   _original_module)selfmoduleattrskey r   X/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/datasets/utils/patching.py__init__   s    
z_PatchedModuleObj.__init__)N)__name__
__module____qualname____doc__r   r   r   r   r   r   	   s   r   c                   @   sD   e Zd ZdZg ZdedddZdd Zdd	 Zd
d Z	dd Z
dS )patch_submoduleaF  
    Patch a submodule attribute of an object, by keeping all other submodules intact at all levels.

    Example::

        >>> import importlib
        >>> from datasets.load import dataset_module_factory
        >>> from datasets.streaming import patch_submodule, xjoin
        >>>
        >>> dataset_module = dataset_module_factory("snli")
        >>> snli_module = importlib.import_module(dataset_module.module_path)
        >>> patcher = patch_submodule(snli_module, "os.path.join", xjoin)
        >>> patcher.start()
        >>> assert snli_module.os.path.join is xjoin
    N)targetc                 C   s6   || _ || _|| _|dd | _i | _|p.g | _d S )N.r   )objr   newsplitr   originalr   )r   r   r   r   r   r   r   r   r   (   s    zpatch_submodule.__init__c           
      C   s  | j d^ }}tt|D ]}ztd|d |d  }W n tk
rX   Y qY nX | j D ]}t	| j|}||kst
|trd|j|krd|| j|< t| j|t|| jd t	| j|}||d d  D ],}t||tt	||d | jd t	||}qt||| j qdq|rzt	td||}	W n ttfk
rH   Y d S X | j D ]:}t	| j||	krTt	| j|| j|< t| j|| j qTnF|t d krt d | | j|< t| j|| j ntd| dd S )Nr   r   )r   __builtins__zTried to patch attribute z instead of a submodule.)r   r   rangelenr   joinModuleNotFoundErrorr   __dir__r
   r   r   r   r   r	   r   r   AttributeErrorglobalsRuntimeError)
r   Z
submodulesZtarget_attri	submoduleattrZobj_attrZpatchedr   Z
attr_valuer   r   r   	__enter__0   sB    

zpatch_submodule.__enter__c                 G   s*   t | jD ]}t| j|| j| q
d S )N)listr   r	   r   pop)r   exc_infor*   r   r   r   __exit__f   s    zpatch_submodule.__exit__c                 C   s   |    | j|  dS )zActivate a patch.N)r+   _active_patchesappendr   r   r   r   startj   s    zpatch_submodule.startc                 C   s0   z| j |  W n tk
r&   Y dS X |  S )zStop an active patch.N)r0   remove
ValueErrorr/   r2   r   r   r   stopo   s
    zpatch_submodule.stop)N)r   r   r   r   r0   strr   r+   r/   r3   r6   r   r   r   r   r      s   6r   N)	importlibr   loggingr   r   loggerr   r   r   r   r   r   <module>   s   