U
    kufg!                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddl	Z	ddl
Z
zddlmZ W n  ek
r   ddlmZ Y nX ddlmZmZ ddlmZ dd	lmZmZ e
jd
edd G dd deZeG dd dZeG dd dZeG dd dZdd ZejddZ e j!dddddd e j!dd d!d" e j!d#d$d%d&gd%d'd( e j!d)d*ed+d, e j!d-d.d" e j!d/d0e"d1d2 e j!d3d4d" d5d6 Z#d7d8 Z$ej%d9d fd:d;Z&ej'ej(ej)fd<d=Z*dS )>z"
The ``jsonschema`` command line.
    )metadata)JSONDecodeError)dedentNresolve_name)definefield)SchemaError)_RefResolvervalidator_forzThe jsonschema CLI is deprecated and will be removed in a future version. Please use check-jsonschema instead, which can be installed from https://pypi.org/project/check-jsonschema/   )
stacklevelc                   @   s   e Zd ZdS )_CannotLoadFileN)__name__
__module____qualname__ r   r   2/tmp/pip-unpacked-wheel-k94ccfep/jsonschema/cli.pyr   #   s   r   c                   @   sR   e Zd Ze Ze Ze Zedd Zdd Z	dd Z
dd Zd	d
 Zdd ZdS )
_Outputterc                 C   s:   |d dkrt |d }n|d dkr,t }| |||dS )Noutputplainerror_formatpretty)	formatterstdoutstderr)_PlainFormatter_PrettyFormatter)cls	argumentsr   r   r   r   r   r   from_arguments.   s
    z_Outputter.from_argumentsc                 C   s   zt |}W n< tk
rH } z| j|t d t |W 5 d }~X Y nX |\ zt|W W  5 Q R  S  tk
r } z| j	|t d t |W 5 d }~X Y nX W 5 Q R X d S )Npathexc_info)
openFileNotFoundErrorfilenotfound_errorsysr#   r   jsonloadr   parsing_error)selfr"   fileerrorr   r   r   r)   6   s    z_Outputter.loadc                 K   s   | j | jjf | d S N)_stderrwrite
_formatterr&   r+   kwargsr   r   r   r&   D   s    z_Outputter.filenotfound_errorc                 K   s   | j | jjf | d S r.   )r/   r0   r1   r*   r2   r   r   r   r*   G   s    z_Outputter.parsing_errorc                 K   s   | j | jjf | d S r.   )r/   r0   r1   validation_errorr2   r   r   r   r4   J   s    z_Outputter.validation_errorc                 K   s   | j | jjf | d S r.   )_stdoutr0   r1   validation_successr2   r   r   r   r6   M   s    z_Outputter.validation_successN)r   r   r   r   r1   r5   r/   classmethodr    r)   r&   r*   r4   r6   r   r   r   r   r   '   s   
r   c                   @   s8   e Zd ZedZdZdd Zdd Zdd Zd	d
 Z	dS )r   z`        ===[{type}]===({path})===

        {body}
        -----------------------------
        z===[SUCCESS]===({path})===
c                 C   s   | j j|d|ddS )Nr%   z does not exist.r"   typebody)
_ERROR_MSGformatr+   r"   r#   r   r   r   r&   ^   s
    z#_PrettyFormatter.filenotfound_errorc                 C   s2   |\}}}d t|||}| jj||j|dS )N r8   )join	tracebackformat_exceptionr;   r<   r   )r+   r"   r#   exc_type	exc_valueexc_tracebackZ	exc_linesr   r   r   r*   e   s    
z_PrettyFormatter.parsing_errorc                 C   s   | j j||jj|dS )Nr8   )r;   r<   	__class__r   r+   instance_pathr-   r   r   r   r4   p   s
    z!_PrettyFormatter.validation_errorc                 C   s   | j j|dS )N)r"   )_SUCCESS_MSGr<   r+   rG   r   r   r   r6   w   s    z#_PrettyFormatter.validation_successN)
r   r   r   r   r;   rH   r&   r*   r4   r6   r   r   r   r   r   Q   s   r   c                   @   s2   e Zd Ze Zdd Zdd Zdd Zdd Zd	S )
r   c                 C   s
   |dS )Nz does not exist.
r   r=   r   r   r   r&      s    z"_PlainFormatter.filenotfound_errorc                 C   s    d |dkrdnt||d S )NzFailed to parse {}: {}
<stdin>   )r<   reprr=   r   r   r   r*      s    z_PlainFormatter.parsing_errorc                 C   s   | j j||dS )N)	file_namer-   )_error_formatr<   rF   r   r   r   r4      s    z _PlainFormatter.validation_errorc                 C   s   dS )Nr>   r   rI   r   r   r   r6      s    z"_PlainFormatter.validation_successN)	r   r   r   r   rN   r&   r*   r4   r6   r   r   r   r   r   {   s
   r   c                 C   s   d| krd|  } t | S )N.zjsonschema.r   )namer   r   r   _resolve_name_with_default   s    rQ   zJSON Schema Validation CLI)descriptionz-iz
--instanceappend	instancesz
        a path to a JSON instance (i.e. filename.json) to validate (may
        be specified multiple times). If no instances are provided via this
        option, one will be expected on standard input.
    )actiondesthelpz-Fz--error-formata  
        the format to use for each validation error message, specified
        in a form suitable for str.format. This string will be passed
        one formatted object named 'error' for each ValidationError.
        Only provide this option when using --output=plain, which is the
        default. If this argument is unprovided and --output=plain is
        used, a simple default representation will be used.
    )rW   z-oz--outputr   r   z
        an output format to use. 'plain' (default) will produce minimal
        text with one line for each error, while 'pretty' will produce
        more detailed human-readable output on multiple lines.
    )choicesdefaultrW   z-Vz--validatorz
        the fully qualified object name of a validator to use, or, for
        validators that are registered with jsonschema, simply the name
        of the class.
    )r9   rW   z
--base-uriz
        a base URI to assign to the provided schema, even if it does not
        declare one (via e.g. $id). This option can be used if you wish to
        resolve relative references to a particular URI (or local path)
    z	--versionversionZ
jsonschema)rU   rZ   schemaz=the path to a JSON Schema to validate with (i.e. schema.json)c                 C   sX   t tj| pdgd}|d dkr4|d r4td|d dkrT|d d krTd|d< |S )Nz--helpargsr   r   r   z3--error-format can only be used with --output plainz"{error.instance}: {error.message}
)varsparser
parse_argsr-   )r]   r   r   r   r   r`      s    r`   c                 C   s:   d}| |D ]}d}|j| |d q|s6|j| d |S )NFTrG   r-   )rG   )Ziter_errorsr4   r6   )rG   instance	validator	outputterinvalidr-   r   r   r   _validate_instance   s    rf   rK   c                 C   s   t tt| dd d S )Nr\   )r   )r'   exitrunr`   r\   r   r   r   main   s    ri   c              
      sF  t j| ||d z | d }W n tk
r8   Y dS X | d }|d krRt|}z|| W n: tk
r } z j| d |d W Y dS d }~X Y nX | d r j| d  }}n fdd}d	g}| d
 d k	rt| d
 |dnd }	|||	d}
d}|D ]B}z||}W n tk
r(   d}Y nX |t	|||
 dO } q|S )N)r   r   r   r[   rK   rc   ra   rT   c              
      sN   zt W S  tk
rH } z jdt d t |W 5 d }~X Y nX d S )NrJ   r!   )r(   r)   r   r*   r'   r#   r   )_r-   rd   stdinr   r   r)   
  s     zrun.<locals>.loadrJ   base_uri)rm   Zreferrer)resolverr   )rG   rb   rc   rd   )
r   r    r)   r   r   Zcheck_schemar	   r4   r
   rf   )r   r   r   rl   r[   Z	Validatorr-   r)   rT   rn   rc   Z	exit_codeZeachrb   r   rk   r   rh      sZ    

rh   )+__doc__	importlibr   r(   r   textwrapr   argparser'   r@   warningspkgutilr   ImportErrorZpkgutil_resolve_nameattrsr   r   Zjsonschema.exceptionsr	   Zjsonschema.validatorsr
   r   warnDeprecationWarning	Exceptionr   r   r   r   rQ   ArgumentParserr_   add_argumentrZ   r`   rf   argvri   r   r   rl   rh   r   r   r   r   <module>   s   )) 
  
 	