
    ggf$              	           S SK JrJr  S SKr " S S5      r\" S/ SQSS\R
                  SS4S	9r " S
 S\5      r " S S\5      r	 " S S5      r
 " S S5      r " S S\5      r " S S5      rg)    )UserDict
namedtupleNc                       \ rS rSrSrSrSrg)
Importance          N)__name__
__module____qualname____firstlineno__OPTIONALREQUIRED__static_attributes__r
       4nsight-compute-2025.1.1/sections/RequestedMetrics.pyr   r      s    HHr   r   MetricRequest)namealias
importancedefault_valuewarn_when_missingg        T)defaultsc                       \ rS rSrSrSrg)r      aH  A metric request containing name, and optional alias, importance and default value.

Args:
    name (str): The name of the metric.
    alias (str, optional): An alias for the metric name. Defaults to None.
    importance (:obj:`Importance`, optional): Whether the metric is required or
        optional. Defaults to Importance.REQUIRED.
    default_value (int, float, string, optional): A default value for OPTIONAL
        metrics. Defaults to float(0).
    warn_when_missing (bool, optional): Whether to issue a warning when an OPTIONAL
        metric is missing. Defaults to True.
r
   N)r   r   r   r   __doc__r   r
   r   r   r   r      s     	r   c                   0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )MetricNotFoundError$   a  Exception raised when metric cannot be found.

Args:
    name (str): The name of the metric that was not found.
    importance (:obj:`Importance`): Whether the metric was marked
        required or optional.
    message (str): A message explaining the error
c                    > U[         R                  :X  a  SOSnSR                  XA5      nXl        X l        U=(       d    UU l        [        TU ]  U R
                  5        g )NRequiredOptionalz {} metric {} could not be found.)r   r   formatr   r   messagesuper__init__)selfr   r   r%   importance_strdefault_message	__class__s         r   r'   MetricNotFoundError.__init__.   sN    '1Z5H5H'Hj<CCNY	$1/&r   )r   r%   r   N)r   r   r   r   r   r'   r   __classcell__r+   s   @r   r   r   $   s    ' 'r   r   c                   z    \ rS rSrSr\R                  rS
S jr\	S 5       r
\	S 5       r\	S 5       r\	S 5       rS	rg)RequestedMetric8   a{  Wrapper class for available (loaded) metrics.

Args:
    name (str): The name of the metric.
    metric (:obj:`IMetric`): An IMetric object representing the metric.
    importance (:obj:`Importance`, optional): Whether the metric was marked
        required or optional. Defaults to Importance.REQUIRED.
    alias (str, optional): An alias for the metric name. Defaults to None.
Nc                 d    Xl         X l        U=(       d    [        R                  U l        X@l        g r-   )_name_metricr1   _DEFAULT_IMPORTANCE_importance_alias)r(   r   metricr   r   s        r   r'   RequestedMetric.__init__E   s$    
%L)L)Lr   c                     U R                   $ r-   )r4   r(   s    r   r   RequestedMetric.nameK   s    zzr   c                     U R                   $ r-   r5   r<   s    r   r9   RequestedMetric.metricO   s    ||r   c                     U R                   $ r-   )r7   r<   s    r   r   RequestedMetric.importanceS   s    r   c                     U R                   $ r-   )r8   r<   s    r   r   RequestedMetric.aliasW   s    {{r   )r8   r7   r5   r4   )NN)r   r   r   r   r   r   r   r6   r'   propertyr   r9   r   r   r   r
   r   r   r1   r1   8   sj     %--          r   r1   c                   B    \ rS rSrSr\R                  S4S jr S rSr	g)RequestedMetricBuilder\   a  Builds a RequestedMetric from an IMetric contained in an IAction.

Args:
    metrics (:obj:`IAction`): An IAction object (potentially) containing the IMetric
        associated with `name`.
    name (str): The name of the metric.
    importance (:obj:`Importance`, optional): Whether the metric was marked
        required or optional. Defaults to Importance.REQUIRED.
    alias (str, optional): An alias for the metric name. Defaults to None.

Raises:
    MetricNotFoundError: If no IMetric object associated with `name` is available.
Nc                 p    S U l          X   n[        X%X45      U l         g ! [         a    [        X#5      ef = fr-   )r5   r1   KeyErrorr   )r(   metricsr   r   r   r9   s         r   r'   RequestedMetricBuilder.__init__k   s=    	8]F*4KDL 	8%d77	8s    5c                     U R                   $ r-   r?   r<   s    r   buildRequestedMetricBuilder.buildu   s    ||r   r?   )
r   r   r   r   r   r   r   r'   rN   r   r
   r   r   rG   rG   \   s#     2<1D1DD 8 4r   rG   c                   P   ^  \ rS rSrSrU 4S jrU 4S jrU 4S jrU 4S jrSr	U =r
$ )RequestedMetricDicty   aC  Custom dict for IMetric lookup using either the metric's name or alias.

Allows insertion of RequestedMetric values using a key that matches either
the metric's name or its alias. Lookup will return the IMetric object directly.
Lookup can be done using name or alias, independently of how the RequestedMetric
was inserted.
c                 0   > 0 U l         [        TU ]	  5         g r-   )aliasToNamer&   r'   )r(   r+   s    r   r'   RequestedMetricDict.__init__   s    r   c                    >  [         TU ]  U5      R                  $ ! [         a     Of = f [         TU ]  U R                  U   5      R                  $ ! [         a     Of = f[        U5      er-   )r&   __getitem__r9   rJ   rT   )r(   keyr+   s     r   rW   RequestedMetricDict.__getitem__   sr    	7&s+222 			7&t'7'7'<=DDD 		sms    
))%A 
A A c                   > UR                   nUR                  nX:w  a  X:w  a  [        S5      eUbJ  X@R                  ;   a-  U R                  U   U:w  a  [        SR	                  XA5      5      eX0R                  U'   [
        TU ]  X25      $ )Nz1Key must match either the metric's name or alias.z%Alias {} is already used by metric {})r   r   rJ   rT   r$   r&   __setitem__)r(   rX   itemr   r   r+   s        r   r[   RequestedMetricDict.__setitem__   s    yy

KclNOO)))0@0@0G40OFMMeYZZ&*U#w"4..r   c                 P   > XR                   ;   n[        TU ]	  U5      =(       d    U$ r-   )rT   r&   __contains__)r(   rX   is_aliasr+   s      r   r_    RequestedMetricDict.__contains__   s&    ***w#C(4H4r   )rT   )r   r   r   r   r   r'   rW   r[   r_   r   r.   r/   s   @r   rQ   rQ   y   s!    	/ 5 5r   rQ   c                   :    \ rS rSrSrSrS rS rS rS r	S r
S	rg
)RequestedMetricsParser   zConvenience class to query IMetric objects from an IAction.

Args:
    handle: The NvRules ContextHandle.
    action (:obj:`IAction`): IAction object containing the IMetrics to be requested.
z:Some required metrics are missing; aborted rule execution.c                 n    Xl         [        R                  " U5      R                  5       U l        X l        g r-   )handleNvRulesget_contextfrontendrK   )r(   rf   actions      r   r'   RequestedMetricsParser.__init__   s'    ++F3<<>r   c           	         [        5       nSnSnU HR  n [        UR                  U R                  UR                  UR
                  S9R                  5       X%R                  '   MT     U(       a/  [        R,                  " U R.                  [0        R2                  5        U$ ! [         GaK  nUR                  [        R                  :X  a  [        UR                  U R                  U5      UR                  UR
                  S9X%R                  '   UR                  (       aZ  U(       dL  U R                  R                  [        R                   R"                  U R%                  U5      5        Sn SnAGM`   SnAGMg   SnAGMn  UR                  [        R&                  :X  aL  SnU R                  R                  [        R                   R(                  U R+                  U5      5         SnAGM   SnAGM  SnAff = f)ak  Parse a list of `MetricRequest`s and return a custom dict of `IMetric` objects.

Args:
    requested_metrics (List[MetricRequest]): A list of requested metrics.

Returns:
    A RequestedMetricDict, which returns an IMetric object for any valid metric
        name or alias.

Raises:
    SystemError: If any REQUIRED metric is not contained in the IAction object.
F)r   rK   r   r   )r   r9   r   r   TN)rQ   rG   r   rK   r   r   rN   r   r   r   r1   _create_fallback_metricr   ri   r%   rg   	IFrontendMsgType_MSG_WARNING$_get_missing_optional_metric_warningr   MsgType_MSG_ERROR$_get_missing_required_metric_warningraise_exceptionrf   rc   !_MISSING_REQUIRED_METRICS_MESSAGE)r(   requested_metricsparsed_metricsfound_missing_required_metricsfound_missing_optional_metricsr9   errors          r   parseRequestedMetricsParser.parse   s    -.).&).&'F.D LL%00 ,,	/
 %' {{+ (< *##3UU 7 ' ##z':'::2A#[[#;;FC#)#4#4$ll	3N;;/ //8V--#--AA EEeL :>6 9W/ %%)<)<<592MM))));;AA%H  =s    ABG5*C G0?A#G00G5c                 (   UR                   c  g UR                   nU R                  R                  UR                  5      n[	        U[
        5      (       a2  US:  a,  UR                  [        R                  R                  U5        U$ [	        U[        5      (       a,  UR                  [        R                  R                  U5        U$ [	        U[        5      (       a,  UR                  [        R                  R                  U5        U$ [!        S5      e)Nr   z8Can only create fallback metric from uint, float or str.)r   rK   
add_metricr   
isinstanceint
set_uint64rg   IMetricValueKind_UINT64float
set_doubleValueKind_DOUBLEstr
set_stringValueKind_STRING
ValueError)r(   metric_requestvaluefallback_metrics       r   rm   .RequestedMetricsParser._create_fallback_metric   s    ''/,,,,11.2E2EFeS!!eqj&&w'G'GO  u%%&&w'G'GO  s##&&w'G'GO  WXXr   c                 J    [        SR                  UR                  5      5      $ )NzThe optional metric {} could not be found. Collecting it as an additional metric could enable the rule to provide more guidance.)r   r$   r   r(   ry   s     r   rp   ;RequestedMetricsParser._get_missing_optional_metric_warning   s"    ((.uzz(:
 	
r   c                     UR                   $ r-   )r%   r   s     r   rr   ;RequestedMetricsParser._get_missing_required_metric_warning  s    }}r   )ri   rf   rK   N)r   r   r   r   r   rt   r'   rz   rm   rp   rr   r   r
   r   r   rc   rc      s,     	E &
4l$
r   rc   )collectionsr   r   rg   r   r   _MetricRequestr   	Exceptionr   r1   rG   rQ   rc   r
   r   r   <module>r      s    ,  
 ID*--sD9	N 	"') '(! !H :*5( *5Za ar   