o
    0ή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h d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#de$dee$ f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>   notanyOfitemsoneOfcontainspropertyNamesz\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def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r'|j	|j
|_	|_
|j|_|j|_|j|_|S )N JSONSCHEMA_DEBUG_CODE_GENERATIONfalse)_ErrorFormattingstrvaluename
definitionruleosgetenvlower	__cause____traceback__message_original_messagesummarydetails)clsr   	formatterobj
debug_code r4   \/tmp/pip-target-vg8gfxp4/lib/python/setuptools/config/_validate_pyproject/error_reporting.py_from_jsonschema=   s   z ValidationError._from_jsonschemaN)
__name__
__module____qualname____doc__r.   r/   r-   classmethodr   r6   r4   r4   r4   r5   r   (   s    r   c               
   c   s4    zd V  W d S  t y }  zt| d d } ~ ww N)r   r   r6   )r   r4   r4   r5   detailed_errorsJ   s   r=   c                   @   sj   e Zd ZdefddZdefddZedefddZedefd	d
Z	dd Z
dd Zdef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   r4   r4   r5   __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.   rC   r4   r4   r5   __str__Z   s   z_ErrorFormatting.__str__c                 C      | j s|  | _ | j S r<   )rA   _expand_summaryrK   r4   r4   r5   r.   `      
z_ErrorFormatting.summaryc                 C   rM   r<   )rB   _expand_detailsrK   r4   r4   r5   r/   g   rO   z_ErrorFormatting.detailsc                 C   s"   t d}|dr||d  S |S )Nzdata.)len
startswith)rC   r$   xr4   r4   r5   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	v r;|r;t
t}  dt||d S  S )Nc                 3       | ]}| v V  qd S r<   r4   ).0	substringmsgr4   r5   	<genexpr>x       z3_ErrorFormatting._expand_summary.<locals>.<genexpr>z:

    )r-   _MESSAGE_REPLACEMENTSr   r@   any_SKIP_DETAILSr   rule_definitionr&   _NEED_DETAILS_SummaryWriter_TOML_JARGONr   )rC   badreplschemar.   r4   rW   r5   rN   r   s   z _ErrorFormatting._expand_summaryc              	   C   s   g }| j jdg }| j jdd pd|}|r-d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   r[   F)widthinitial_indentsubsequent_indentbreak_long_wordszDESCRIPTION:
   )r   zGIVEN VALUE:
zOFFENDING RULE: zDEFINITION:
rF   )r   r%   popjoinr   appendjsondumpsr#   r   r&   )rC   optional
desc_linesdescrf   re   r#   defaultsr4   r4   r5   rP      s*   	z _ErrorFormatting._expand_detailsN)r7   r8   r9   r   rD   r"   rL   r   r.   r/   r?   rN   rP   r4   r4   r4   r5   r!   R   s    r!   c                
   @   sp  e Zd Zh dZd)deeeef  fddZdeee	e f deee	e f fdd	Z
	
d*dddeee	e f dedee defddZdee defddZdedee fddZdedee dee fddZ	d+dededee defddZdee fddZdee defdd Zdedee defd!d"Zdedee dee fd#d$Zd%ed&edefd'd(ZdS ),ra   >   titledefaultexamplesrf   Njargonc                 C   sZ   |pi | _ dddd| d dddd| d	 | d
 dddd| _g d| _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   allOfr   prefixItemsr   r   r   patternPropertiesconstenum)r   r   	maxLength	minLengthpatternformatminimummaximumexclusiveMinimumexclusiveMaximum
multipleOf)r{   _jargon_terms_guess_inline_defs)rC   r{   r4   r4   r5   rD      s   
z_SummaryWriter.__init__termrE   c                    s*   t |tr fdd|D S  j||S )Nc                    s   g | ]	} j ||qS r4   )r{   get)rU   trK   r4   r5   
<listcomp>   s    z*_SummaryWriter._jargon.<locals>.<listcomp>)
isinstancelistr{   r   )rC   r   r4   rK   r5   r      s   
z_SummaryWriter._jargonr   r4   _pathre   prefixr   c             
   C   s  t |tr| |||S | ||}| ||}|r | | S | |d}| |d}t|d }t }	t	|
 D ]|\}
\}}g ||}|
dkrO|n|}|	| | | d t |tr| ||}| ||}|	|rxd| n	d| |||d  q=t |tr|dks| |r| |||}|d	rdnd}|	| |  q=|	d| || d q=|	 W  d    S 1 sw   Y  d S )
Nz  - rg   r   :rh   r   type[)r   r   _handle_list_filter_unecessary_handle_simple_dict_child_prefixrQ   ioStringIO	enumerater   write_labeldict_is_propertyrR   _valuegetvalue)rC   re   r   r   filteredsimplechild_prefixitem_prefixr   bufferir   r#   
child_pathline_prefixchildrensepr4   r4   r5   __call__   sB   



$z_SummaryWriter.__call__pathc                    s:   |  |s|s	dS |d  t fdddD p | jv S )NFc                 3   s    | ]}  |V  qd S r<   )rR   )rU   kr   r4   r5   rY      s    z0_SummaryWriter._is_unecessary.<locals>.<genexpr>z$_)r   r]   _IGNORE)rC   r   r4   r   r5   _is_unecessary   s    z_SummaryWriter._is_unecessaryc                    s    fdd|  D S )Nc                    s(   i | ]\}} g  |s||qS r4   )r   )rU   r   r#   r   rC   r4   r5   
<dictcomp>   s    z5_SummaryWriter._filter_unecessary.<locals>.<dictcomp>)r   )rC   re   r   r4   r   r5   r      s   z!_SummaryWriter._filter_unecessaryr#   c                    sV   t  fdd| jD }t dd   D  }|s|r)dd|  | dS d S )Nc                 3   rT   r<   r4   )rU   pr#   r4   r5   rY      rZ   z5_SummaryWriter._handle_simple_dict.<locals>.<genexpr>c                 s   s    | ]
}t |ttfV  qd S r<   )r   r   r   )rU   vr4   r4   r5   rY      s    {, z}
)r]   r   valuesrp   _inline_attrs)rC   r#   r   inliner   r4   r   r5   r      s
   z"_SummaryWriter._handle_simple_dictschemasc                    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   )rU   er4   r4   r5   rY   
  s    z._SummaryWriter._handle_list.<locals>.<genexpr><   rh   r   c                 3   s2    | ]\}}| g d | ddV  qdS )r   ]r   Nr4   )rU   r   r   r   r   rC   r4   r5   rY     s    "
)r   reprallrQ   r   rp   r   )rC   r   r   r   repr_r4   r   r5   r     s   

z_SummaryWriter._handle_listc                 C   s8   d}|ddd D ]}|dvr n|d7 }q	|d dkS )zGCheck if the given path can correspond to an arbitrarily named propertyr   Nr   >   r   r~   r      r4   )rC   r   counterr   r4   r4   r5   r     s   
z_SummaryWriter._is_propertyc                 C   sX   |^ }}|  |st|}| j|pd| |S |d dkr(d|dS t|S )Nrg   r   r~   z(regex ))r   _separate_termsr   r   rp   r   r   )rC   r   parentsr   norm_keyr4   r4   r5   r     s   

z_SummaryWriter._labelc                 C   sN   |d dkr#|  |s#| |}t|trdd| dS tt|S t|S )Nr   r   r   r   r   )r   r   r   r   rp   r   r"   r   )rC   r#   r   type_r4   r4   r5   r   (  s   
z_SummaryWriter._valuec                 c   sB    |  D ]\}}g ||}| | d| || V  qd S )Nz: )r   r   r   )rC   re   r   r   r#   r   r4   r4   r5   r   0  s
    z_SummaryWriter._inline_attrsparent_prefixr   c                 C   s   t |d | S )Nrg   )rQ   )rC   r   r   r4   r4   r5   r   5  s   z_SummaryWriter._child_prefixr<   )r   )r   r4   )r7   r8   r9   r   r	   r   r"   rD   r   r   r   r   r
   r   boolr   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r4   r5   ra      sD    *"
*

ra   wordrE   c                 C   s   dd t | D S )zE
    >>> _separate_terms("FooBar-foo")
    ['foo', 'bar', 'foo']
    c                 S   s   g | ]}|r|  qS r4   )r)   )rU   wr4   r4   r5   r   >  s    z#_separate_terms.<locals>.<listcomp>)_CAMEL_CASE_SPLITTERsplit)r   r4   r4   r5   r   9  s   r   )&r   rr   rI   r'   re
contextlibr   textwrapr   r   typingr   r   r   r   r	   r
   r   r   fastjsonschema_exceptionsr   	getLoggerr7   rG   r\   r^   r`   compiler   I_IDENTIFIERrb   r   r=   r!   ra   r"   r   r4   r4   r4   r5   <module>   s>    (

"
I 