
    >Tf                     h    d dl Z d dlZd dlmZ dZdZdZd Z G d d          Z	 G d	 d
          Z
dS )    N)gettext)	ConditionConditionSetandorc                     t          |           t          u oGt          |           dk    o4t          |                                           d         t
          t          fv S )zG
    Determine whether the given dictionary looks like a rule set.
       r   )typedictlenlistkeysANDOR)datas    6/var/www/html/netbox-4.1.3/netbox/extras/conditions.py
is_rulesetr      sH     ::V#d))q.VT$))++5F5Fq5IcSUY5VV    c                       e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zeeeeee	e
efZeee
efeee
feeeeeee
feeeeeee
feee	e
f ed
          efiZedfdZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd
S )r   a  
    An individual conditional rule that evaluates a single attribute and its value.

    :param attr: The name of the attribute being evaluated
    :param value: The value being compared
    :param op: The logical operation to use when evaluating the value (default: 'eq')
    eqgtgteltlteincontainsregexNFc                 ^   || j         vrIt          t          d                              |d                    | j                                       t          |          | j        vr=t          t          d                              t          |                              || j        t          |                   vr>t          t          d                              |t          |                              || _        || _        t          | d|           | _
        || _        d S )	Nz3Unknown operator: {op}. Must be one of: {operators}z, )op	operatorszUnsupported value type: {value}valuez(Invalid type for {op} operation: {value})r   r"   eval_)	OPERATORS
ValueError_formatjoinr
   TYPESattrr"   getattr	eval_funcnegate)selfr*   r"   r   r-   s        r   __init__zCondition.__init__4   s   T^##QTUU\\4>!:!: ]      ;;dj((Q@AAHHtTY{{H[[\\\TZU,,,QIJJQQUW_cdi_j_jQkklll	
 |r||44r   c                     d }	 t          j        || j                            d          |          }n# t          $ r d}Y nw xY w|                     |          }| j        r| S |S )z[
        Evaluate the provided data to determine whether it matches the condition.
        c                     t          | t                    rfd| D             S t                              |           S )Nc                 F    g | ]}t                               |          S  )r   get).0ikeys     r   
<listcomp>z0Condition.eval.<locals>._get.<locals>.<listcomp>I   s'    666QC((666r   )
isinstancer   r   r4   )objr7   s    `r   _getzCondition.eval.<locals>._getG   sC    #t$$ 76666#666688C%%%r   .N)	functoolsreducer*   split	TypeErrorr,   r-   )r.   r   r;   r"   results        r   evalzCondition.evalC   s    	& 	& 	&	$T49??3+?+?FFEE 	 	 	EEE	 &&; 	:s   .4 AAc                     || j         k    S Nr!   r.   r"   s     r   eval_eqzCondition.eval_eqZ       
""r   c                     || j         k    S rD   r!   rE   s     r   eval_neqzCondition.eval_neq]   rG   r   c                     || j         k    S rD   r!   rE   s     r   eval_gtzCondition.eval_gtb       tz!!r   c                     || j         k    S rD   r!   rE   s     r   eval_gtezCondition.eval_gtee   rG   r   c                     || j         k     S rD   r!   rE   s     r   eval_ltzCondition.eval_lth   rL   r   c                     || j         k    S rD   r!   rE   s     r   eval_ltezCondition.eval_ltek   rG   r   c                     || j         v S rD   r!   rE   s     r   eval_inzCondition.eval_inp   s    
""r   c                     | j         |v S rD   r!   rE   s     r   eval_containszCondition.eval_containss   s    zU""r   c                 :    t          j        | j        |          d uS rD   )rematchr"   rE   s     r   
eval_regexzCondition.eval_regexx   s    x
E**$66r   )__name__
__module____qualname____doc__EQGTGTELTLTEINCONTAINSREGEXr$   strboolintfloatr   r
   r)   r/   rB   rF   rI   rK   rN   rP   rR   rT   rV   rZ   r3   r   r   r   r      sp         
B	B
C	B
C	BHE 	BRb(EI
 	b(E"r8nb"c2sH-BRh/r2x T

REE (*%      .# # ## # #
" " "# # #" " "# # #
# # ## # #
7 7 7 7 7r   r   c                       e Zd ZdZd Zd ZdS )r   aJ  
    A set of one or more Condition to be evaluated per the prescribed logic (AND or OR). Example:

    {"and": [
        {"attr": "foo", "op": "eq", "value": 1},
        {"attr": "bar", "op": "eq", "value": 2, "negate": true}
    ]}

    :param ruleset: A dictionary mapping a logical operator to a list of conditional rules
    c                 t   t          |          t          ur=t          t          d                              t          |                              t          |          dk    rt          |                                          d                                         | _	        | j	        t          t          fvrt          t          d                    d || j	                 D             | _        d S 	 d | _	        t          di |g| _        d S # t          $ r t          t          d                    w xY w)	Nz,Ruleset must be a dictionary, not {ruleset}.)rulesetr	   r   zFInvalid logic type: must be 'AND' or 'OR'. Please check documentation.c                 b    g | ],}t          |          rt          |          nt          d i |-S )r3   )r   r   r   )r5   rules     r   r8   z)ConditionSet.__init__.<locals>.<listcomp>   sP        '1&6&6MT"""I<M<M<M<M  r   z6Incorrect key(s) informed. Please check documentation.r3   )r
   r   r%   r&   r'   r   r   r   lowerlogicr   r   
conditionsr   r@   )r.   rm   s     r   r/   zConditionSet.__init__   s0   ==$$QMNNUU^bcj^k^kUllmmmw<<1w||~~..q188::DJz#r** #k!l!lmmm #DJ/  DOOO
^!
#,#7#7w#7#7"8 ^ ^ ^ #[!\!\]]]^s   5D 'D7c                 n    | j         dk    rt          nt          } |fd| j        D                       S )zd
        Evaluate the provided data to determine whether it matches this set of conditions.
        r   c              3   B   K   | ]}|                               V  d S rD   )rB   )r5   dr   s     r   	<genexpr>z$ConditionSet.eval.<locals>.<genexpr>   s-      ::QAFF4LL::::::r   )rq   anyallrr   )r.   r   funcs    ` r   rB   zConditionSet.eval   sA     jD((ssct::::$/::::::r   N)r[   r\   r]   r^   r/   rB   r3   r   r   r   r   |   s?        	 	^ ^ ^*; ; ; ; ;r   r   )r=   rX   django.utils.translationr   r&   __all__r   r   r   r   r   r3   r   r   <module>r|      s        				 1 1 1 1 1 1 	W W Wc7 c7 c7 c7 c7 c7 c7 c7L%; %; %; %; %; %; %; %; %; %;r   