
    2~hO!                        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j                  e      Z ee
      Z ej$                  d      Zeed<    ej$                  d	      Zeed
<    ej$                  d      Zeed<    ej$                  d      Zeed<   dZdZdZd Z edg d      Z G d d      Zy)    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                   l    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zed        Zed        Zy)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                 V   t        |      | _        g | _        g | _        d| _        t        j                  |       d| d| _        |dk7  ry t        j                  |      }t        j                  |      }t        j                  |      }	|r&|j                         }
|
d   | _        t        | _        nP|	r&|	j                         }
|
d   | _        t        | _        n(|r%|j                         }
|
d   | _        |
d   | _        ny d| _        | j!                          | j#                          y )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)selfappwhatnameobjoptionslinesbase_provider_method_matchlocale_provider_method_matchstandard_provider_method_matchr    s              _/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/faker/sphinx/docstring.py__init__z ProviderMethodDocstring.__init__3   s   u+%ooc23>$zR8%B%H%H%N"'F'L'LT'R$)J)P)PQU)V&%2<<>I$X.DL)DL+6@@BI$X.DL)DL)4>>@I$X.DL$X.DL     c                 L    t         j                  | j                   d|        y )N )loggerwarningr   )r%   r5   s     r/   _log_warningz$ProviderMethodDocstring._log_warningS   s     $**+1WI67r1   c                 r    	 	 t        | j                        }| j                  |       (# t        $ r Y y w xY wN)nextr   _parse_sectionStopIteration)r%   lines     r/   r#   zProviderMethodDocstring._parseV   sA    *DOO, ##D)  ! s   * 	66c                    |j                  d      s| j                  j                  |       y 	 t        | j                        }|j                  d      r#| j                  |       | j                  |       y |dk(  r| j                  |       y ||z   }| j                  |       y # t
        $ r | j                  |       Y y w xY w)Nz:sample )
startswithr   appendr9   r   r;   _process_sample_sectionr:   )r%   section	next_lines      r/   r:   z&ProviderMethodDocstring._parse_section_   s    !!),%%g.	T__-I 	*((1	* "_((1
 	)G()  	((1	s   B& &CCc                    t         j                  |      }|sd| d}| j                  |       y |j                         }|j	                  d      }|j	                  d      }|j	                  d      }|rt        t        |      t              nt        }|rt        |      nt        }|r| j                  |      nd}t        |||      }| j                  j                  |       y )NzThe section `z%` is malformed and will be discarded.r   r   r   r>   )r   r   r6   r    getmaxintDEFAULT_SAMPLE_SIZEDEFAULT_SEED_beautify_kwargsr   r   r@   )	r%   rB   r   msgr    r   r   r   samples	            r/   rA   z/ProviderMethodDocstring._process_sample_section}   s    $**73 !'*OPCc" OO%	}}V$}}V$x(6:s3t912@S s4yl28&&v.b dF+V$r1   c                     d }d }t        j                  d||      }t        j                  d||      }|j                         S )Nc                 V    | j                  d      xs | j                  d      }|r|S dS )N      r>   groupr   quoteds     r/   _repl_whitespacezBProviderMethodDocstring._beautify_kwargs.<locals>._repl_whitespace   s)    [[^5u{{1~F#6++r1   c                 V    | j                  d      xs | j                  d      }|r|S dS )NrO   rP   z, rQ   rS   s     r/   _repl_commaz=ProviderMethodDocstring._beautify_kwargs.<locals>._repl_comma   s)    [[^5u{{1~F#6--r1   z("[^"]*")|(\'[^\']*\')|[ \t]+z("[^"]*")|(\'[^\']*\')|,)resubstrip)r%   r   rU   rW   results        r/   rJ   z(ProviderMethodDocstring._beautify_kwargs   sE    	,	.
 8:JFS 3[&I ||~r1   c                     t        |      S r8   )repr)r%   values     r/   _stringify_resultz)ProviderMethodDocstring._stringify_result   s    E{r1   c                 :    ddl m} t        | j                     |dS )Nr   )OrderedDict)	generatorra   )collectionsra   _faker"   )r%   ra   s     r/   _generate_eval_scopez,ProviderMethodDocstring._generate_eval_scope   s     + t||,&
 	
r1   c                 d    t        t        t        d      }| j                  j	                  |       y )Nr>   )r   rH   rI   r   r@   )r%   default_samples     r/   _inject_default_sample_sectionz6ProviderMethodDocstring._inject_default_sample_section   s#     3\2F^,r1   c                    | j                   s| j                          d}| j                         }| j                   D ]$  }t        j	                  | j
                  |j                        }t        |      }|j                  r/d| j
                   d|j                   d}| j                  |       u	 t        j                  |j                         dj                  t        |j                        D cg c]  }| j                  t!        ||             c}      }|t"        j	                  |j                  | j
                  |j                  |j                  |      z  }' |r0d	|z   }| j&                  j)                  |j+                  d             y y c c}w # t$        $ r2 d| j
                   d|j                   d}| j                  |       Y w xY w)
Nr>   )r   r   zNInvalid code elements detected. Sample generation will be skipped for method `z` with arguments `z`.
)r   r   r   r   resultsz%Sample generation failed for method `z:examples:

)r   rh   re   _command_templateformatr!   r   r   errorsr6   r   r   joinranger   r_   eval_sample_output_template	Exceptionr   extendsplit)	r%   output
eval_scoperL   command	validatorrK   _rk   s	            r/   r$   z)ProviderMethodDocstring._generate_samples   s   }}//1..0
mm 	F'..dll6==.YG+G4I++/<<.8J6==/Y[]  !!#&

6;;'))`eflfqfq`r$s[\T%;%;D*<U%V$st 188<<!==# 9  '	6 %.F%%fll4&89  %t =dll^K]^d^k^k]llno!!#&s%   1AF2"F
FF7GGc                     | j                   S r8   )r   r%   s    r/   skippedzProviderMethodDocstring.skipped   s    }}r1   c                     | j                   S r8   )r   r|   s    r/   r+   zProviderMethodDocstring.lines   s    !!!r1   N)__name__
__module____qualname____doc__r0   r6   r#   r:   rA   rJ   r_   re   rh   r$   propertyr}   r+    r1   r/   r   r   $   sc    !@8*)<%,$
-#:J   " "r1   r   )r   loggingrX   rc   r   typingr   fakerr   faker.configr   r   faker.sphinx.validatorr   	getLoggerr   r4   rd   compiler	   __annotations__r
   r   r   rl   rr   rH   rI   r   r   r   r1   r/   <module>r      s      	 "   : 6			8	$ )34h)i w i-7RZZ8m-n !7 n+52::f,   !+

q! g  3    	H8	9{" {"r1   