U
    e hO!                     @   s   U d dl Z d dlZ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 d dlmZ eeZee
ZedZeed< ed	Zeed
< edZeed< edZeed< dZdZdZd ZeddddgZG dd dZdS )    N)
namedtuple)Pattern)Faker)AVAILABLE_LOCALESDEFAULT_LOCALE)SampleCodeValidatorz1^faker\.providers\.BaseProvider\.(?P<method>\w+)$_base_provider_method_patternz2^faker\.providers\.\w+\.Provider\.(?P<method>\w+)$!_standard_provider_method_patternzS^faker\.providers\.\w+\.(?P<locale>[a-z]{2,3}_[A-Z]{2})\.Provider\.(?P<method>\w+)$_locale_provider_method_patternzZ^:sample(?: size=(?P<size>[1-9][0-9]*))?(?: seed=(?P<seed>[0-9]+))?:(?: ?(?P<kwargs>.*))?$_sample_line_patternzgenerator.{method}({kwargs})zb>>> Faker.seed({seed})
>>> for _ in range({size}):
...     fake.{method}({kwargs})
...
{results}

   Samplesizeseedkwargsc                   @   sx   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zedd Zedd ZdS )ProviderMethodDocstringam  
    Class that preprocesses provider method docstrings to generate sample usage and output

    Notes on how samples are generated:
    - If the docstring belongs to a standard provider method, sample usage and output will be
      generated using a `Faker` object in the `DEFAULT_LOCALE`.
    - If the docstring belongs to a localized provider method, the correct locale will be used.
    - If the docstring does not belong to any provider method, docstring preprocessing will be skipped.
    - Docstring lines will be parsed for potential sample sections, and the generation details of each
      sample section will internally be represented as a ``Sample`` namedtuple.
    - Each ``Sample`` will have info on the keyword arguments to pass to the provider method, how many
      times the provider method will be called, and the initial seed value to ``Faker.seed()``.
    c                 C   s   t || _g | _g | _d| _t| d| d| _|dkr@d S t	|}t
	|}t	|}	|r|| }
|
d | _t| _nD|	r|	 }
|
d | _t| _n&|r| }
|
d | _|
d | _nd S d| _|   |   d S )NTz:docstring of z
: WARNING:methodlocaleF)iter
_line_iter_parsed_lines_samples_skippedinspectgetfile_log_prefixr   matchr
   r	   	groupdict_methodr   _locale_parse_generate_samples)selfZappwhatnameobjoptionslinesZbase_provider_method_matchZlocale_provider_method_matchZstandard_provider_method_matchr    r(   :/tmp/pip-unpacked-wheel-cwn3b9ao/faker/sphinx/docstring.py__init__3   s4    






z ProviderMethodDocstring.__init__c                 C   s   t | j d|  d S )N )loggerwarningr   )r"   r-   r(   r(   r)   _log_warningS   s    z$ProviderMethodDocstring._log_warningc                 C   s8   zt | j}W n tk
r&   Y q4Y q X | | q d S N)nextr   StopIteration_parse_section)r"   liner(   r(   r)   r    V   s
    
zProviderMethodDocstring._parsec                 C   s   | ds| j| d S zt| j}W n  tk
rH   | | Y d S X | drj| | | | n&|dkr~| | n|| }| | d S )Nz:sample )
startswithr   appendr0   r   r1   _process_sample_sectionr2   )r"   sectionZ	next_liner(   r(   r)   r2   _   s    



z&ProviderMethodDocstring._parse_sectionc           	      C   s   t |}|s(d| d}| | d S | }|d}|d}|d}|r`tt|tnt}|rpt|nt}|r| 	|nd}t
|||}| j| d S )NzThe section `z%` is malformed and will be discarded.r   r   r   r4   )r   r   r.   r   getmaxintDEFAULT_SAMPLE_SIZEDEFAULT_SEED_beautify_kwargsr   r   r6   )	r"   r8   r   msgr   r   r   r   sampler(   r(   r)   r7   }   s    




z/ProviderMethodDocstring._process_sample_sectionc                 C   s4   dd }dd }t d||}t d||}| S )Nc                 S   s    |  dp|  d}|r|S dS )N      r4   groupr   quotedr(   r(   r)   _repl_whitespace   s    zBProviderMethodDocstring._beautify_kwargs.<locals>._repl_whitespacec                 S   s    |  dp|  d}|r|S dS )NrA   rB   z, rC   rE   r(   r(   r)   _repl_comma   s    z=ProviderMethodDocstring._beautify_kwargs.<locals>._repl_commaz("[^"]*")|(\'[^\']*\')|[ \t]+z("[^"]*")|(\'[^\']*\')|,)resubstrip)r"   r   rG   rH   resultr(   r(   r)   r>      s
    z(ProviderMethodDocstring._beautify_kwargsc                 C   s   t |S r/   )repr)r"   valuer(   r(   r)   _stringify_result   s    z)ProviderMethodDocstring._stringify_resultc                 C   s   ddl m} t| j |dS )Nr   )OrderedDict)	generatorrP   )collectionsrP   _faker   )r"   rP   r(   r(   r)   _generate_eval_scope   s    z,ProviderMethodDocstring._generate_eval_scopec                 C   s   t ttd}| j| d S )Nr4   )r   r<   r=   r   r6   )r"   Zdefault_sampler(   r(   r)   _inject_default_sample_section   s    z6ProviderMethodDocstring._inject_default_sample_sectionc              	      s  j s  d} j D ]}tjj|jd t }|jrfdj d|j d}	| q z2t
|j d fddt|jD }W n8 tk
r   d	j d|j d}	| Y q Y q X |tj|jj|j|j|d
7 }q |rd| }j|d d S )Nr4   )r   r   zNInvalid code elements detected. Sample generation will be skipped for method `z` with arguments `z`.
c                    s   g | ]} t qS r(   )rO   eval).0_commandZ
eval_scoper"   r(   r)   
<listcomp>   s     z=ProviderMethodDocstring._generate_samples.<locals>.<listcomp>z%Sample generation failed for method `)r   r   r   r   resultsz:examples:

)r   rU   rT   _command_templateformatr   r   r   errorsr.   r   r   joinranger   	Exception_sample_output_templater   extendsplit)r"   outputr@   Z	validatorr?   r]   r(   rZ   r)   r!      s:    

&


z)ProviderMethodDocstring._generate_samplesc                 C   s   | j S r/   )r   r"   r(   r(   r)   skipped   s    zProviderMethodDocstring.skippedc                 C   s   | j S r/   )r   rh   r(   r(   r)   r'      s    zProviderMethodDocstring.linesN)__name__
__module____qualname____doc__r*   r.   r    r2   r7   r>   rO   rT   rU   r!   propertyri   r'   r(   r(   r(   r)   r   $   s    	%
r   )r   loggingrI   rR   r   typingr   Zfakerr   Zfaker.configr   r   Zfaker.sphinx.validatorr   	getLoggerrj   r,   rS   compiler   __annotations__r	   r
   r   r^   rd   r<   r=   r   r   r(   r(   r(   r)   <module>   s2    
