U
    -c,                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZmZmZmZmZmZmZmZ ddlmZ eeZddd	d
dZdZdddddddhZedZedejZdddddZG dd deZ edd Z!G dd dZ"G dd  d Z#e$ee$ d!d"d#Z%dS )$    N)contextmanager)indentwrap)AnyDictIteratorListOptionalSequenceUnioncast   )JsonSchemaValueExceptionzkeys must be named byzat least one item that matches z"only items matching the definition)z(must be named by propertyName definitionzone of contains definitionz same as const definition:zonly specified items)zmust not be emptyzis always invalidzmust not be thereanyOfoneOfcontainspropertyNamesnotitemsz\W+|([A-Z][^A-Z\W]*)z^[\w_]+$tablekeykeys)objectproperty
propertiesproperty namesc                   @   s.   e Zd ZdZdZdZdZeedddZ	dS )ValidationErrora  Report violations of a given JSON schema.

    This class extends :exc:`~fastjsonschema.JsonSchemaValueException`
    by adding the following properties:

    - ``summary``: an improved version of the ``JsonSchemaValueException`` error message
      with only the necessary information)

    - ``details``: more contextual information about the error like the failing schema
      itself and the value that violates the schema.

    Depending on the level of the verbosity of the ``logging`` configuration
    the exception message will be only ``summary`` (default) or a combination of
    ``summary`` and ``details`` (when the logging level is set to :obj:`logging.DEBUG`).
    r   exc                 C   sj   t |}| t||j|j|j|j}tdd }|dkrN|j	|j
 |_	|_
|j|_|j|_|j|_|S )NZ JSONSCHEMA_DEBUG_CODE_GENERATIONfalse)_ErrorFormattingstrvaluename
definitionruleosgetenvlower	__cause____traceback__message_original_messagesummarydetails)clsr   	formatterobjZ
debug_code r3   Y/tmp/pip-unpacked-wheel-izj_87as/setuptools/config/_validate_pyproject/error_reporting.py_from_jsonschema=   s    z ValidationError._from_jsonschemaN)
__name__
__module____qualname____doc__r.   r/   r-   classmethodr   r5   r3   r3   r3   r4   r   (   s   r   c               
   c   s>   z
d V  W n. t k
r8 }  zt| d W 5 d } ~ X Y nX d S N)r   r   r5   r   r3   r3   r4   detailed_errorsJ   s    
r<   c                   @   sj   e Zd ZedddZedddZeedddZeedd	d
Z	dd Z
dd ZedddZdS )r!   r   c                 C   sB   || _ d| |j d| _| j j|j| j| _d| _d| _d S )N`r   )r   _simplify_namer$   r,   replacer-   _summary_details)selfr   r3   r3   r4   __init__S   s
    z_ErrorFormatting.__init__)returnc                 C   s,   t  tjkr&| jr&| j d| j S | jS )N

)_loggergetEffectiveLevelloggingDEBUGr/   r.   rB   r3   r3   r4   __str__Z   s    z_ErrorFormatting.__str__c                 C   s   | j s|  | _ | j S r;   )r@   _expand_summaryrJ   r3   r3   r4   r.   `   s    
z_ErrorFormatting.summaryc                 C   s   | j s|  | _ | j S r;   )rA   _expand_detailsrJ   r3   r3   r4   r/   g   s    
z_ErrorFormatting.detailsc                 C   s"   t d}|dr||d  S |S )Nzdata.)len
startswith)rB   r$   xr3   r3   r4   r>   n   s    z_ErrorFormatting._simplify_namec                    sz   | j  t D ]\}} || qt fddtD r> S | jj}| jjt	krv|rvt
t}  dt||d S  S )Nc                 3   s   | ]}| kV  qd S r;   r3   ).0Z	substringmsgr3   r4   	<genexpr>x   s     z3_ErrorFormatting._expand_summary.<locals>.<genexpr>z:

    )r-   _MESSAGE_REPLACEMENTSr   r?   any_SKIP_DETAILSr   Zrule_definitionr&   _NEED_DETAILS_SummaryWriter_TOML_JARGONr   )rB   badreplschemar.   r3   rR   r4   rL   r   s    z _ErrorFormatting._expand_summaryc              	   C   s   g }| j jdg }| j jdd p,d|}|rZdt|ddddd}|d	|  tj| j jd
d}tj| j jd
d}dt	|d d| j j
dt	|d g}d|| S )Nz$$descriptiondescription 
P   rU   F)widthinitial_indentsubsequent_indentbreak_long_wordszDESCRIPTION:
   )r   zGIVEN VALUE:
zOFFENDING RULE: zDEFINITION:
rE   )r   r%   popjoinr   appendjsondumpsr#   r   r&   )rB   optionalZ
desc_linesdescr_   r^   r#   defaultsr3   r3   r4   rM      s*    	z _ErrorFormatting._expand_detailsN)r6   r7   r8   r   rC   r"   rK   r   r.   r/   r>   rL   rM   r3   r3   r3   r4   r!   R   s   r!   c                   @   sL  e Zd ZddddhZd+eeeef  dddZeee	e f eee	e f d	d
dZ
d,ddeee	e f eee edddZee edddZeee dddZeee ee dddZd-eeee edddZee ddd Zee edd!d"Zeee edd#d$Zeee ee d%d&d'Zeeed(d)d*ZdS ).rZ   r_   defaulttitleZexamplesN)jargonc                 C   sl   |pi | _ dddd| d dddd| d	 | d
 dddd| _dddddddddddg| _d S )Nzat least one of the followingzexactly one of the followingzall of the followingz(*NOT* the following)r   z (in order)zcontains at least one ofznon-predefined acceptable r   r   z named via patternzpredefined valuezone of)r   r   ZallOfr   ZprefixItemsr   r   r   patternPropertiesconstenumru   rt   Z	maxLengthZ	minLengthpatternformatZminimummaximumZexclusiveMinimumZexclusiveMaximumZ
multipleOf)rr   _jargon_terms_guess_inline_defs)rB   rr   r3   r3   r4   rC      s2    
z_SummaryWriter.__init__)termrD   c                    s*   t |tr fdd|D S  j||S )Nc                    s   g | ]} j ||qS r3   )rr   get)rQ   trJ   r3   r4   
<listcomp>   s     z*_SummaryWriter._jargon.<locals>.<listcomp>)
isinstancelistrr   r}   )rB   r|   r3   rJ   r4   ry      s    
z_SummaryWriter._jargonr   r3   _path)r^   prefixr   rD   c             
   C   s  t |tr| |||S | ||}| ||}|r@| | S | |d}| |d}t|d }t &}	t	|
 D ] \}
\}}||f}|
dkr|n|}|	| | | d t |tr| ||}| ||}|	|rd| nd| |||d  q|t |trd|dks.| |rd| |||}|d	rLdnd}|	| |  q||	d| || d q||	 W  5 Q R  S Q R X d S )
Nz  - r`   r   :ra   r   type[)r   r   _handle_list_filter_unecessary_handle_simple_dict_child_prefixrN   ioStringIO	enumerater   write_labeldict_is_propertyrO   _valuegetvalue)rB   r^   r   r   filteredsimplechild_prefixitem_prefixr   bufferir   r#   
child_pathZline_prefixchildrensepr3   r3   r4   __call__   s@    

z_SummaryWriter.__call__)pathrD   c                    s:   |  |s|sdS |d  t fdddD p8 | jkS )NFc                 3   s   | ]}  |V  qd S r;   )rO   )rQ   kr   r3   r4   rT      s     z0_SummaryWriter._is_unecessary.<locals>.<genexpr>z$_)r   rW   _IGNORE)rB   r   r3   r   r4   _is_unecessary   s    z_SummaryWriter._is_unecessary)r^   r   c                    s    fdd|  D S )Nc                    s&   i | ]\}}  |fs||qS r3   )r   )rQ   r   r#   r   rB   r3   r4   
<dictcomp>   s    z5_SummaryWriter._filter_unecessary.<locals>.<dictcomp>)r   )rB   r^   r   r3   r   r4   r      s    z!_SummaryWriter._filter_unecessary)r#   r   rD   c                    sV   t  fdd| jD }t dd   D  }|s8|rRdd|  | dS d S )Nc                 3   s   | ]}| kV  qd S r;   r3   )rQ   pr#   r3   r4   rT      s     z5_SummaryWriter._handle_simple_dict.<locals>.<genexpr>c                 s   s   | ]}t |ttfV  qd S r;   )r   r   r   )rQ   vr3   r3   r4   rT      s     {, z}
)rW   r{   valuesri   _inline_attrs)rB   r#   r   inliner   r3   r   r4   r      s
    z"_SummaryWriter._handle_simple_dict)schemasr   r   rD   c                    sj    rdS t|}tdd |D r>t|dk r>| dS |d d fddt|D S )Nr   c                 s   s   | ]}t |ttf V  qd S r;   )r   r   r   )rQ   er3   r3   r4   rT   
  s     z._SummaryWriter._handle_list.<locals>.<genexpr><   ra   r   c                 3   s.   | ]&\}}| d | dfdV  qdS )r   ]r   Nr3   )rQ   r   r   r   r   rB   r3   r4   rT     s    )r   reprallrN   r   ri   r   )rB   r   r   r   repr_r3   r   r4   r     s    

z_SummaryWriter._handle_list)r   c                 C   s8   d}|ddd D ]}|dkr" q,|d7 }q|d dkS )zGCheck if the given path can correspond to an arbitrarily named propertyr   Nr   >   r   rs   r      r3   )rB   r   counterr   r3   r3   r4   r     s    
z_SummaryWriter._is_propertyc                 C   sX   |^ }}|  |s8t|}| j|p6d| |S |d dkrPd|dS t|S )Nr`   r   rs   z(regex ))r   _separate_termsrz   r}   ri   ry   r   )rB   r   parentsr   Znorm_keyr3   r3   r4   r     s    

z_SummaryWriter._labelc                 C   sN   |d dkrF|  |sF| |}t|tr<dd| dS tt|S t|S )Nr   r   r   r   r   )r   ry   r   r   ri   r   r"   r   )rB   r#   r   type_r3   r3   r4   r   (  s
    
$z_SummaryWriter._value)r^   r   rD   c                 c   s>   |  D ]0\}}||f}| | d| || V  qd S )Nz: )r   r   r   )rB   r^   r   r   r#   r   r3   r3   r4   r   0  s    
z_SummaryWriter._inline_attrs)parent_prefixr   rD   c                 C   s   t |d | S )Nr`   )rN   )rB   r   r   r3   r3   r4   r   5  s    z_SummaryWriter._child_prefix)N)r   )r   r3   )r6   r7   r8   r   r	   r   r"   rC   r   r   ry   r   r
   r   boolr   r   r   r   r   r   r   r   r   r   r   r   r3   r3   r3   r4   rZ      s8   "( *     
rZ   )wordrD   c                 C   s   dd t | D S )zE
    >>> _separate_terms("FooBar-foo")
    ['foo', 'bar', 'foo']
    c                 S   s   g | ]}|r|  qS r3   )r)   )rQ   wr3   r3   r4   r   >  s      z#_separate_terms.<locals>.<listcomp>)_CAMEL_CASE_SPLITTERsplit)r   r3   r3   r4   r   9  s    r   )&r   rk   rH   r'   re
contextlibr   textwrapr   r   typingr   r   r   r   r	   r
   r   r   Zfastjsonschema_exceptionsr   	getLoggerr6   rF   rV   rX   rY   compiler   IZ_IDENTIFIERr[   r   r<   r!   rZ   r"   r   r3   r3   r3   r4   <module>   s<   (

"
I 