U
    <c
                     @  sZ   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZ G dd dZd	S )
z#A diagnostic engine based on SARIF.    )annotations)ListOptional)infra)	formattersarif)versionc                   @  s   e Zd ZU dZded< ddddZddd	d
ZddddZddddZddddZ	ddddZ
dddddddZdS )DiagnosticEnginea  A generic diagnostic engine based on SARIF.

    This class is the main interface for diagnostics. It manages the creation of diagnostic contexts.
    A DiagnosticContext provides the entry point for recording Diagnostics.
    Each DiagnosticContext is powered by a DiagnosticTool, which can be customized with
    custom RuleCollection and Diagnostic type.
    See infra.DiagnosticContext and infra.DiagnosticTool for more details.

    Examples:
        Step 1: Create a set of rules.
        >>> rules = infra.RuleCollection.from_list(
        ...     "CustomRuleCollection",
        ...     [
        ...         infra.Rule(
        ...             id="r1",
        ...             name="rule-1",
        ...             message_default_template="Mising xxx",
        ...         ),
        ...     ],
        ... )

        Step 2: Create a diagnostic tool.
        >>> tool = infra.DiagnosticTool(
        ...     name="tool",
        ...     version="1.0.0",
        ...     rules=rules,
        ... )

        Step 3: Create a diagnostic engine.
        >>> engine = DiagnosticEngine()

        Step 4: Start a new diagnostic context.
        >>> with engine.start_diagnostic_context(tool) as context:

        Step 5: Add diagnostics in your code.
        ...     context.diagnose(rules.rule1, infra.Level.ERROR)

        Step 6: Afterwards, get the SARIF log.
        >>> sarif_log = engine.sarif_log()
    zList[infra.DiagnosticContext]	_contextsNone)returnc                 C  s
   g | _ d S N)r
   self r   Q/tmp/pip-unpacked-wheel-gikjz4vx/torch/onnx/_internal/diagnostics/infra/engine.py__init__8   s    zDiagnosticEngine.__init__zsarif.SarifLogc                 C  s    t jtjtjdd | jD dS )Nc                 S  s   g | ]}|  qS r   )r   ).0contextr   r   r   
<listcomp>?   s     z.DiagnosticEngine.sarif_log.<locals>.<listcomp>)r   Z
schema_uriruns)r   ZSarifLogsarif_versionZSARIF_VERSIONZSARIF_SCHEMA_LINKr
   r   r   r   r   	sarif_log;   s
    zDiagnosticEngine.sarif_logstrc                 C  s   |   S r   to_jsonr   r   r   r   __str__B   s    zDiagnosticEngine.__str__c                 C  s   |   S r   r   r   r   r   r   __repr__F   s    zDiagnosticEngine.__repr__c                 C  s   t |  S r   )r   Zsarif_to_jsonr   r   r   r   r   r   I   s    zDiagnosticEngine.to_jsonc                 C  s   | j   dS )zClears all diagnostic contexts.N)r
   clearr   r   r   r   r   L   s    zDiagnosticEngine.clearNzinfra.DiagnosticToolz!Optional[infra.DiagnosticOptions]zinfra.DiagnosticContext)tooloptionsr   c                 C  s   t ||}| j| |S r   )r   ZDiagnosticContextr
   append)r   r   r    r   r   r   r   create_diagnostic_contextP   s    z*DiagnosticEngine.create_diagnostic_context)N)__name__
__module____qualname____doc____annotations__r   r   r   r   r   r   r"   r   r   r   r   r	      s   
) r	   N)r&   
__future__r   typingr   r   Z torch.onnx._internal.diagnosticsr   Z&torch.onnx._internal.diagnostics.infrar   r   Z,torch.onnx._internal.diagnostics.infra.sarifr   r   r	   r   r   r   r   <module>   s   