U
    -eS1                     @   s$  d dl mZ d dlZd dlmZ d dlmZmZm	Z	 d dl
Z
ddlmZ ddlmZ dZe de ZeeZee	eef d	d
dZed\ZZdddgZe	eee ee f ZG dd dZedG dd deZedkr d dlmZ eddZe d e! Z"ee"j#Z#e$e#Z%dS )    N)Path)AnyListTuple   )	resources)
deprecatedzDhttps://github.com/huggingface/datasets/tree/main/src/datasets/utils/)resourcereturnc                 C   s$   t t| }t|t d|  fS )Nz/resources/)pkg_resources	read_textr   yamlZ	safe_loadBASE_REF_URL)r
   content r   V/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/datasets/utils/readme.pyload_yaml_resource   s    r   zreadme_structure.yamlz[Needs More Information]z[More Information Needed]zj(https://github.com/huggingface/datasets/blob/main/CONTRIBUTING.md#how-to-contribute-to-the-dataset-cards)c                   @   sT   e Zd Zdeeee edddZdedddZee	d	d
dZ
edddZdS )SectionNF)namelevellinessuppress_parsing_errorsc                 C   sJ   || _ || _|| _d| _d| _i | _g | _g | _| jd k	rF| j|d d S )N Tr   )	r   r   r   textis_empty_textr   parsing_error_listparsing_warning_listparse)selfr   r   r   r   r   r   r   __init__$   s    
zSection.__init__r   c                 C   s  d}g }d}| j D ]}|ddkr(qq|dd d dkrF| }q| d | jd kr|s|dkrt|| jd || j|< g }n>|g kr|  jd| 7  _| jdkr| jtkrd| _	g }d| d	d  d}q|
| q|dkr2|| jkr| j
d
| d t|| jd || j|< n@|g krr|  jd| 7  _| jdkrr| jtkrrd| _	| jdkr|s| jg ks| jg krddd | j| j D  }}d| j d| }t|d S )Nr   F 
   z```r   # r   z)Multiple sections with the same heading `z:` have been found. Please keep only one of these sections.
c                 s   s   | ]}d | V  qdS )-	Nr   .0xr   r   r   	<genexpr>V   s     z Section.parse.<locals>.<genexpr>z=The following issues were found while parsing the README at ``:
)r   stripsplitr   r   r   r   joinFILLER_TEXTr   appendr   r   r   
ValueError)r    r   Zcurrent_sub_levelZcurrent_linesZ
code_startlineerrorserror_stringr   r   r   r   0   sD    



 zSection.parse)	structurer   c              	   C   s  g }g }|d dkr8| j r8| ji kr8|d| j d |d dkr^| j r^|d| j d |d d	k	r~| ji krd
d |d D }|d| j dddd |D  d ndd |d D }d}t|D ]r\}}|| jkr|d| j d| d d}q| jdkrqn| j| |d | \}	}
}||
7 }||7 }q|r~| jD ]*}||krR|d| j d| d qR|ri ||fS |  ||fS d	S )aC  Validates a Section class object recursively using the structure provided as a dictionary.

        Args:
            structute (:obj: `dict`): The dictionary representing expected structure.

        Returns:
            :obj: `ReadmeValidatorOutput`: The dictionary representation of the section, and the errors.
        Zallow_emptyFz"Expected some content in section `z` but it is empty.Zallow_empty_textzExpected some text in section `z4` but it is empty (text in subsections are ignored).subsectionsNc                 S   s   g | ]}|d  qS r   r   r)   Z
subsectionr   r   r   
<listcomp>x   s     z$Section.validate.<locals>.<listcomp>z	Section `z&` expected the following subsections: , c                 S   s   g | ]}d | d  qS `r   r(   r   r   r   r:   {   s     z. Found 'None'.c                 S   s   g | ]}|d  qS r8   r   r9   r   r   r   r:      s     z` is missing subsection: `z`.Tz###r=   z` has an extra subsection: `z[`. Skipping further validation checks for this subsection as expected structure is unknown.)	r   r   r1   r   r/   	enumerater   validateto_dict)r    r6   
error_listwarning_listvaluesZstructure_namesZhas_missing_subsectionsidxr   _Zsubsec_error_listZsubsec_warning_listr   r   r   r?   Z   sL    

"






zSection.validate)r   c                 C   s$   | j | j| jdd | j D dS )z3Returns the dictionary representation of a section.c                 S   s   g | ]}|  qS r   )r@   )r)   valuer   r   r   r:      s     z#Section.to_dict.<locals>.<listcomp>)r   r   r   r7   )r   r   r   r   rC   r    r   r   r   r@      s
    zSection.to_dict)NF)F)__name__
__module____qualname__strr   boolr!   r   dictReadmeValidatorOutputr?   r@   r   r   r   r   r   #   s   *Gr   z*Use `huggingface_hub.DatasetCard` instead.c                       s   e Zd Zdeee eed fddZdd Ze	de
eedd	d
Ze	deeeedddZded fddZdd Zdd Z  ZS )ReadMeNF)r   r   r6   r   c                    sB   t  j|dd || _d| _d| _|| _| jd k	r>| j|d d S )Nr   )r   r   r   r   )superr!   r6   yaml_tags_line_count	tag_countr   r   )r    r   r   r6   r   	__class__r   r   r!      s    
zReadMe.__init__c                 C   st   | j d kr| t\}}}n| | j \}}}|g ks>|g krpddd || D }d| j d| }t|d S )Nr&   c                 S   s   g | ]}d | qS )r'   r   r(   r   r   r   r:      s     z#ReadMe.validate.<locals>.<listcomp>z3The following issues were found for the README at `r,   )r6   	_validatereadme_structurer/   r   r2   )r    r   rA   rB   r4   r5   r   r   r   r?      s    
zReadMe.validate)pathr6   r   c              	   C   s0   t |dd}| }W 5 Q R X | ||||dS )Nzutf-8)encodingr   )open	readlines)clsrX   r6   r   fr   r   r   r   from_readme   s    zReadMe.from_readmeroot)stringr6   	root_namer   c                 C   s   | d}| ||||dS )Nr&   r   )r.   )r\   r`   r6   ra   r   r   r   r   r   from_string   s    
zReadMe.from_stringr   c                    s   d}| j D ]D}|  jd7  _|ddkrF|  jd7  _| jdkrF qP|d7 }q
| jdkrp| j |d d  | _ n| j | jd  | _ t j|d d S )Nr   r   r"   z---   r   )r   rR   r-   rS   rQ   r   )r    r   
line_countr3   rT   r   r   r      s    



zReadMe.parsec                 C   s   t |  S )z>Returns the string of dictionary representation of the ReadMe.)rK   r@   rG   r   r   r   __str__   s    zReadMe.__str__c           	   	   C   s  g }g }| j dkr|d n*| jdkr4|d n| jdkrH|d t| j }|dkr|dddd	 t| j D  d
 nj|dk r|d nVt| j d }|dr| j| 	|d d \}}}||7 }||7 }n
|d |ri ||fS | 
 ||fS d S )Nr   z-Empty YAML markers are present in the README.z*No YAML markers are present in the README.r   z2Only the start of YAML tags present in the README.z-The README has several first-level headings: r;   c                 S   s   g | ]}d | d  qS r<   r   r(   r   r   r   r:      s     z$ReadMe._validate.<locals>.<listcomp>zL. Only one heading is expected. Skipping further validation for this README.zmThe README has no first-level headings. One heading is expected. Skipping further validation for this README.zDataset Card forr7   zuNo first-level heading starting with `Dataset Card for` found in README. Skipping further validation for this README.)rR   r1   rS   lenr   keysr/   list
startswithr?   r@   )	r    rW   rA   rB   Znum_first_level_keysZ	start_keyrE   Zsec_error_listZsec_warning_listr   r   r   rV      s<    



$





zReadMe._validate)NF)NF)Nr_   F)F)rH   rI   rJ   rK   r   rM   rL   r!   r?   classmethodr   r^   rb   r   re   rV   __classcell__r   r   rT   r   rO      s"   	
        rO   __main__)ArgumentParserz?Validate the content (excluding YAML tags) of a README.md file.)usagereadme_filepath)&importlib.resourcesr   r   loggingpathlibr   typingr   r   r   r   r   Zdeprecation_utilsr   r   __file__Zthis_url	getLoggerrH   loggerrK   r   rW   Zknown_readme_structure_urlr0   rM   rN   r   rO   argparserm   Zapadd_argument
parse_argsargsro   r^   Zreadmer   r   r   r   <module>   s6   
 	b



