o
     a                     @   s   d dl Z d dlmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d d	lmZ g d
ZG dd deZG dd deZG dd deZdS )    N)rfc1902rfc1905)v2cZipMibSource)addMibCompiler)SmiError)AbstractSimpleAsn1Item)PyAsn1Error)debug)ObjectIdentity
ObjectTypeNotificationTypec                   @   s   e Zd ZdZd\ZZdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z d9S ):r   a	  Create an object representing MIB variable ID.

    At the protocol level, MIB variable is only identified by an OID.
    However, when interacting with humans, MIB variable can also be referred
    to by its MIB name. The *ObjectIdentity* class supports various forms
    of MIB variable identification, providing automatic conversion from
    one to others. At the same time *ObjectIdentity* objects behave like
    :py:obj:`tuples` of py:obj:`int` sub-OIDs.

    See :RFC:`1902#section-2` for more information on OBJECT-IDENTITY
    SMI definitions.

    Parameters
    ----------
    args
        initial MIB variable identity. Recognized variants:

        * single :py:obj:`tuple` or integers representing OID
        * single :py:obj:`str` representing OID in dot-separated
          integers form
        * single :py:obj:`str` representing MIB variable in
          dot-separated labels form
        * single :py:obj:`str` representing MIB name. First variable
          defined in MIB is assumed.
        * pair of :py:obj:`str` representing MIB name and variable name
        * pair of :py:obj:`str` representing MIB name and variable name
          followed by an arbitrary number of :py:obj:`str` and/or
          :py:obj:`int` values representing MIB variable instance
          identification.

    Other parameters
    ----------------
    kwargs
        MIB resolution options(object):

        * whenever only MIB name is given, resolve into last variable defined
          in MIB if last=True.  Otherwise resolves to first variable (default).

    Notes
    -----
        Actual conversion between MIB variable representation formats occurs
        upon :py:meth:`~pysnmp.smi.rfc1902.ObjectIdentity.resolveWithMib`
        invocation.

    Examples
    --------
    >>> from pysnmp.smi.rfc1902 import ObjectIdentity
    >>> ObjectIdentity((1, 3, 6, 1, 2, 1, 1, 1, 0))
    ObjectIdentity((1, 3, 6, 1, 2, 1, 1, 1, 0))
    >>> ObjectIdentity('1.3.6.1.2.1.1.1.0')
    ObjectIdentity('1.3.6.1.2.1.1.1.0')
    >>> ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0')
    ObjectIdentity('iso.org.dod.internet.mgmt.mib-2.system.sysDescr.0')
    >>> ObjectIdentity('SNMPv2-MIB', 'system')
    ObjectIdentity('SNMPv2-MIB', 'system')
    >>> ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
    ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
    >>> ObjectIdentity('IP-MIB', 'ipAdEntAddr', '127.0.0.1', 123)
    ObjectIdentity('IP-MIB', 'ipAdEntAddr', '127.0.0.1', 123)

          c                 O   sT   || _ || _d  | _| _d  | _| _| j| _d | _ | _	| _
d | _| _d | _d S )N  )_ObjectIdentity__args_ObjectIdentity__kwargs _ObjectIdentity__mibSourcesToAdd_ObjectIdentity__modNamesToLoad!_ObjectIdentity__asn1SourcesToAdd#_ObjectIdentity__asn1SourcesOptionsstDirty_ObjectIdentity__state_ObjectIdentity__indices_ObjectIdentity__oid_ObjectIdentity__label_ObjectIdentity__modName_ObjectIdentity__symName_ObjectIdentity__mibNode)selfargskwargsr   r   4/usr/lib/python3/dist-packages/pysnmp/smi/rfc1902.py__init__T   s   
zObjectIdentity.__init__c                 C   s,   | j | j@ r| j| j| jfS td| jj )a  Returns MIB variable symbolic identification.

        Returns
        -------
        str
             MIB module name
        str
             MIB variable symbolic name
        : :py:class:`~pysnmp.proto.rfc1902.ObjectName`
             class instance representing MIB variable instance index.

        Raises
        ------
        SmiError
            If MIB variable conversion has not been performed.

        Examples
        --------
        >>> objectIdentity = ObjectIdentity('1.3.6.1.2.1.1.1.0')
        >>> objectIdentity.resolveWithMib(mibViewController)
        >>> objectIdentity.getMibSymbol()
        ('SNMPv2-MIB', 'sysDescr', (0,))
        >>>

        %s object not fully initialized)r   stCleanr   r    r   r   	__class____name__r"   r   r   r%   getMibSymbol^   s   zObjectIdentity.getMibSymbolc                 C   "   | j | j@ r	| jS td| jj )aC  Returns OID identifying MIB variable.

        Returns
        -------
        : :py:class:`~pysnmp.proto.rfc1902.ObjectName`
            full OID identifying MIB variable including possible index part.

        Raises
        ------
        SmiError
           If MIB variable conversion has not been performed.

        Examples
        --------
        >>> objectIdentity = ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
        >>> objectIdentity.resolveWithMib(mibViewController)
        >>> objectIdentity.getOid()
        ObjectName('1.3.6.1.2.1.1.1.0')
        >>>

        r'   r   r(   r   r   r)   r*   r+   r   r   r%   getOid}   s   zObjectIdentity.getOidc                 C   r-   )a  Returns symbolic path to this MIB variable.

        Meaning a sequence of symbolic identifications for each of parent
        MIB objects in MIB tree.

        Returns
        -------
        tuple
            sequence of names of nodes in a MIB tree from the top of the tree
            towards this MIB variable.

        Raises
        ------
        SmiError
           If MIB variable conversion has not been performed.

        Notes
        -----
        Returned sequence may not contain full path to this MIB variable
        if some symbols are now known at the moment of MIB look up.

        Examples
        --------
        >>> objectIdentity = ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)
        >>> objectIdentity.resolveWithMib(mibViewController)
        >>> objectIdentity.getOid()
        ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'system', 'sysDescr')
        >>>

        r'   )r   r(   r   r   r)   r*   r+   r   r   r%   getLabel   s   zObjectIdentity.getLabelc                 C   r-   Nr'   )r   r(   r!   r   r)   r*   r+   r   r   r%   
getMibNode   s   zObjectIdentity.getMibNodec                 C      | j | j@ S N)r   r(   r+   r   r   r%   isFullyResolved      zObjectIdentity.isFullyResolvedc                 O   s@   | j du r	|| _ n|  j |7  _ | jr| j| | S || _| S )a  Adds path to a repository to search ASN.1 MIB files.

        Parameters
        ----------
        *asn1Sources :
            one or more URL in form of :py:obj:`str` identifying local or
            remote ASN.1 MIB repositories. Path must include the *@mib@*
            component which will be replaced with MIB module name at the
            time of search.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
            reference to itself

        Notes
        -----
        Please refer to :py:class:`~pysmi.reader.localfile.FileReader`,
        :py:class:`~pysmi.reader.httpclient.HttpReader` and
        :py:class:`~pysmi.reader.ftpclient.FtpReader` classes for
        in-depth information on ASN.1 MIB lookup.

        Examples
        --------
        >>> ObjectIdentity('SNMPv2-MIB', 'sysDescr').addAsn1Source('http://mibs.snmplabs.com/asn1/@mib@')
        ObjectIdentity('SNMPv2-MIB', 'sysDescr')
        >>>

        N)r   r   updater"   Zasn1Sourcesr$   r   r   r%   addAsn1MibSource   s   
zObjectIdentity.addAsn1MibSourcec                 G   &   | j du r
|| _ | S |  j |7  _ | S )ag  Adds path to repository to search PySNMP MIB files.

        Parameters
        ----------
        *mibSources :
            one or more paths to search or Python package names to import
            and search for PySNMP MIB modules.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
            reference to itself

        Notes
        -----
        Normally, ASN.1-to-Python MIB modules conversion is performed
        automatically through PySNMP/PySMI interaction. ASN1 MIB modules
        could also be manually compiled into Python via the
        `mibdump.py <http://snmplabs.com/pysmi/mibdump.html>`_
        tool.

        Examples
        --------
        >>> ObjectIdentity('SNMPv2-MIB', 'sysDescr').addMibSource('/opt/pysnmp/mibs', 'pysnmp_mibs')
        ObjectIdentity('SNMPv2-MIB', 'sysDescr')
        >>>

        N)r   r"   Z
mibSourcesr   r   r%   addMibSource   s
   
zObjectIdentity.addMibSourcec                 G   r:   )a  Schedules search and load of given MIB modules.

        Parameters
        ----------
        *modNames:
            one or more MIB module names to load up and use for MIB
            variables resolution purposes.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
            reference to itself

        Examples
        --------
        >>> ObjectIdentity('SNMPv2-MIB', 'sysDescr').loadMibs('IF-MIB', 'TCP-MIB')
        ObjectIdentity('SNMPv2-MIB', 'sysDescr')
        >>>

        N)r   r"   ZmodNamesr   r   r%   loadMibs  s
   
zObjectIdentity.loadMibsc                 C   s  | j dur%tjtj@ otdd| j   |jjdd | j D   d| _ | jdu r3t|jddd n8tjtj@ oCtdd| j  t|j| j| j	
d	| j	
d
| j	
d| j	
d| j	
dd d | _| _	| jdurtjtj@ otdd| j  |jj| j  d| _| j| j@ r| S |jddd\}}d| _t| jd tr| jd | t| jdkrtjtj@ otd| j  zt| jd | _W n tyS   t| jd ttfr|| jd \}}}n<d| jd v r|t| jd d\}}}n#| jd }|j| | j
dr#| |\}}}n|!|\}}}|rIztdd |D }W n t"yH   t#d|f w t|| | _Y n
w || j\}}}tjtj@ ontd| j||f  |$|\}}}	|| _%|| _&|| _'|j||\}
|
| _(tjtj@ otd||
f  t|
|r|r|$|
j)dd \}}}	|j||\}|*|| _nt|
|r|rt|f| _n
|rt|f| _|  j| jO  _tjtj@ otd| jf  | S  | S t| jdkr?| jd r| jd r| jd | _%| jd | _&nO| jd rQ|j| jd  | j
dr;| | jd \}}}n|!| jd \}}}|$|\| _%| _&}	n|| jdd \}}}|$|\| _%| _&}	|j| j%| j&\}
|
| _(t|
+ | _|| j\}}}|| _'tjtj@ otd| j||f  t|
|r|$|
j)dd \}}}	|j||\}| jdd rz|j,| jdd  }|  j|7  _|*|| _W nJ ty   t#d | jdd |
- t./ d f w n-| jdd r"| jdd r"tdd!d | jdd D }|  j|7  _|f| _|  j| jO  _tjtj@ o<td| jf  | S  | S t#d")#at  Perform MIB variable ID conversion.

        Parameters
        ----------
        mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
            class instance representing MIB browsing functionality.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
            reference to itself

        Raises
        ------
        SmiError
           In case of fatal MIB hanling errora

        Notes
        -----
        Calling this method might cause the following sequence of
        events (exact details depends on many factors):

        * ASN.1 MIB file downloaded and handed over to
          :py:class:`~pysmi.compiler.MibCompiler` for conversion into
          Python MIB module (based on pysnmp classes)
        * Python MIB module is imported by SNMP engine, internal indices
          created
        * :py:class:`~pysnmp.smi.view.MibViewController` looks up the
          rest of MIB identification information based on whatever information
          is already available, :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
          class instance
          gets updated and ready for further use.

        Examples
        --------
        >>> objectIdentity = ObjectIdentity('SNMPv2-MIB', 'sysDescr')
        >>> objectIdentity.resolveWithMib(mibViewController)
        ObjectIdentity('SNMPv2-MIB', 'sysDescr')
        >>>

        Nzadding MIB sources %s, c                 S      g | ]}t |qS r   r   .0xr   r   r%   
<listcomp>_      z1ObjectIdentity.resolveWithMib.<locals>.<listcomp>T)ifAvailable
ifNotAddedz(adding MIB compiler with source paths %s	searchers	borrowersdestinationrF   rG   )ZsourcesrH   rI   rJ   rF   rG   zloading MIB modules %s
SNMPv2-SMI	MibScalarMibTableColumnr   r   r   zresolving %s as OID or label.Zlastc                 S   r@   r   )intrA   r   r   r%   rD     rE   z Unknown object name component %rz(resolved %r into prefix %r and suffix %rz#resolved prefix %r into MIB node %rzresolved indices are %rr   z<Instance index %r to OID conversion failure at object %r: %sc                 S   r@   r   )strrA   r   r   r%   rD     rE   zNon-OID, label or MIB symbol)0r   r   loggerflagMIBjoin
mibBuilderZaddMibSourcesr   r   r   getr   ZloadModulesr   r(   importSymbolsr   
isinstancer   r   resolveWithMiblenr   Z
ObjectNamer   r
   listtupleZgetNodeNameZgetNodeNameByOidsplitr   ZgetLastNodeNameZgetFirstNodeName
ValueErrorr   ZgetNodeLocationr   r    r   r!   nameZgetIndicesFromInstIdZgetNameZgetInstIdFromIndicesr0   sysexc_info)r"   mibViewControllerrL   rM   prefixZlabelsuffixZmodNameZsymName_mibNodeZ
rowModNameZ
rowSymNameZrowNodeZinstIdsZinstIdr   r   r%   rY   2  s>  
* 





	
 



 

 zObjectIdentity.resolveWithMibc              	      sX   | j | j@ r$t  d| j| j| jrdpdd fdd| jD f S td| j	j
 )Nz
%s::%s%s%srN   r   c                    s.   g | ]}|j  d drd|  p| qS )FZmatchConstraintsz"%s")isSuperTypeOfprettyPrintrA   sr   r%   rD     s   . z.ObjectIdentity.prettyPrint.<locals>.<listcomp>r'   )r   r(   r   ZOctetStringr   r    r   rT   r   r)   r*   r+   r   rj   r%   ri     s   zObjectIdentity.prettyPrintc                 C   "   d| j jddd | jD f S )N%s(%s)r?   c                 S   r@   r   reprrA   r   r   r%   rD     rE   z+ObjectIdentity.__repr__.<locals>.<listcomp>)r)   r*   rT   r   r+   r   r   r%   __repr__     "zObjectIdentity.__repr__c                 C   &   | j | j@ rt| jS td| jj N"%s object not properly initialized)r   r(   rQ   r   r   r)   r*   r+   r   r   r%   __str__     
zObjectIdentity.__str__c                 C   s&   | j | j@ r| j|kS td| jj rs   r.   r"   otherr   r   r%   __eq__$  rv   zObjectIdentity.__eq__c                 C   s&   | j | j@ r| j|kS td| jj rs   r.   rw   r   r   r%   __ne__*  rv   zObjectIdentity.__ne__c                 C   s&   | j | j@ r| j|k S td| jj rs   r.   rw   r   r   r%   __lt__0  rv   zObjectIdentity.__lt__c                 C   s&   | j | j@ r| j|kS td| jj rs   r.   rw   r   r   r%   __le__6  rv   zObjectIdentity.__le__c                 C   &   | j | j@ r| j|kS td| jj rs   r.   rw   r   r   r%   __gt__<  rv   zObjectIdentity.__gt__c                 C   r}   rs   r.   rw   r   r   r%   __ge__B  rv   zObjectIdentity.__ge__c                 C   s&   | j | j@ r| jdkS td| jj )Nr   rt   r.   r+   r   r   r%   __nonzero__H  rv   zObjectIdentity.__nonzero__c                 C   rr   rs   )r   r(   boolr   r   r)   r*   r+   r   r   r%   __bool__N  rv   zObjectIdentity.__bool__c                 C   &   | j | j@ r| j| S td| jj rs   r.   r"   ir   r   r%   __getitem__T  rv   zObjectIdentity.__getitem__c                 C   rr   rs   )r   r(   rZ   r   r   r)   r*   r+   r   r   r%   __len__Z  rv   zObjectIdentity.__len__c                 C   s&   | j | j@ r| j| S td| jj rs   r.   rw   r   r   r%   __add__`  rv   zObjectIdentity.__add__c                 C   s&   | j | j@ r|| j S td| jj rs   r.   rw   r   r   r%   __radd__f  rv   zObjectIdentity.__radd__c                 C   rr   rs   )r   r(   hashr   r   r)   r*   r+   r   r   r%   __hash__l  rv   zObjectIdentity.__hash__c                 C   s<   | j | j@ r|dv rt| j|S t|td| jj|f )N)ZasTuplecloneZsubtypeZ
isPrefixOfZisSameTypeWithrh   Z	getTagSetZgetEffectiveTagSetZ	getTagMapZtagSetindexz3%s object not properly initialized for accessing %s)r   r(   getattrr   AttributeErrorr   r)   r*   )r"   attrr   r   r%   __getattr__r  s
   zObjectIdentity.__getattr__N)!r*   
__module____qualname____doc__r   r(   r&   r,   r/   r0   r2   r5   r9   r<   r>   rY   ri   rp   ru   ry   rz   r{   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r%   r      s>    =
$($ ]r   c                   @   sl   e Zd ZdZd\ZZdddZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd ZdddZdd ZdS )r   a=  Create an object representing MIB variable.

    Instances of :py:class:`~pysnmp.smi.rfc1902.ObjectType` class are
    containers incorporating :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
    class instance (identifying MIB variable) and optional value belonging
    to one of SNMP types (:RFC:`1902`).

    Typical MIB variable is defined like this (from *SNMPv2-MIB.txt*):

    .. code-block:: bash

       sysDescr OBJECT-TYPE
           SYNTAX      DisplayString (SIZE (0..255))
           MAX-ACCESS  read-only
           STATUS      current
           DESCRIPTION
                   "A textual description of the entity.  This value should..."
           ::= { system 1 }

    Corresponding ObjectType instantiation would look like this:

    .. code-block:: python

        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'), 'Linux i386 box')

    In order to behave like SNMP variable-binding (:RFC:`1157#section-4.1.1`),
    :py:class:`~pysnmp.smi.rfc1902.ObjectType` objects also support
    sequence protocol addressing `objectIdentity` as its 0-th element
    and `objectSyntax` as 1-st.

    See :RFC:`1902#section-2` for more information on OBJECT-TYPE SMI
    definitions.

    Parameters
    ----------
    objectIdentity : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
        Class instance representing MIB variable identification.
    objectSyntax :
        Represents a value associated with this MIB variable. Values of
        built-in Python types will be automatically converted into SNMP
        object as specified in OBJECT-TYPE->SYNTAX field.

    Notes
    -----
        Actual conversion between MIB variable representation formats occurs
        upon :py:meth:`~pysnmp.smi.rfc1902.ObjectType.resolveWithMib`
        invocation.

    Examples
    --------
    >>> from pysnmp.smi.rfc1902 import *
    >>> ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'))
    ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0'), Null(''))
    >>> ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0), 'Linux i386')
    ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0), 'Linux i386')

    r   Nc                 C   s<   |d u rt j}t|tstd|f ||g| _| j| _d S Nz5initializer should be ObjectIdentity instance, not %r)r   unSpecifiedrX   r   r   _ObjectType__argsr   _ObjectType__state)r"   objectIdentityZobjectSyntaxr   r   r%   r&     s   

zObjectType.__init__c                 C   r   r1   )r   r(   r   r   r)   r*   r   r   r   r%   r     rv   zObjectType.__getitem__c                 C   s   |   S r4   ri   r+   r   r   r%   ru     s   zObjectType.__str__c                 C   rl   )Nrm   r?   c                 S   r@   r   rn   rA   r   r   r%   rD     rE   z'ObjectType.__repr__.<locals>.<listcomp>)r)   r*   rT   r   r+   r   r   r%   rp     rq   zObjectType.__repr__c                 C   r3   r4   )r   r(   r+   r   r   r%   r5     r6   zObjectType.isFullyResolvedc                 O   s   | j d j|i | | S )a  Adds path to a repository to search ASN.1 MIB files.

        Parameters
        ----------
        *asn1Sources :
            one or more URL in form of :py:obj:`str` identifying local or
            remote ASN.1 MIB repositories. Path must include the *@mib@*
            component which will be replaced with MIB module name at the
            time of search.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectType`
            reference to itself

        Notes
        -----
        Please refer to :py:class:`~pysmi.reader.localfile.FileReader`,
        :py:class:`~pysmi.reader.httpclient.HttpReader` and
        :py:class:`~pysmi.reader.ftpclient.FtpReader` classes for
        in-depth information on ASN.1 MIB lookup.

        Examples
        --------
        >>> ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')).addAsn1Source('http://mibs.snmplabs.com/asn1/@mib@')
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))
        >>>

        r   )r   r9   r8   r   r   r%   r9     s   zObjectType.addAsn1MibSourcec                 G      | j d j|  | S )a{  Adds path to repository to search PySNMP MIB files.

        Parameters
        ----------
        *mibSources :
            one or more paths to search or Python package names to import
            and search for PySNMP MIB modules.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectType`
            reference to itself

        Notes
        -----
        Normally, ASN.1-to-Python MIB modules conversion is performed
        automatically through PySNMP/PySMI interaction. ASN1 MIB modules
        could also be manually compiled into Python via the
        `mibdump.py <http://snmplabs.com/pysmi/mibdump.html>`_
        tool.

        Examples
        --------
        >>> ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')).addMibSource('/opt/pysnmp/mibs', 'pysnmp_mibs')
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))
        >>>

        r   )r   r<   r;   r   r   r%   r<     s   zObjectType.addMibSourcec                 G   r   )a#  Schedules search and load of given MIB modules.

        Parameters
        ----------
        *modNames:
            one or more MIB module names to load up and use for MIB
            variables resolution purposes.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectType`
            reference to itself

        Examples
        --------
        >>> ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr')).loadMibs('IF-MIB', 'TCP-MIB')
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'))
        >>>

        r   )r   r>   r=   r   r   r%   r>     s   zObjectType.loadMibsTc              	   C   s  | j | j@ r| S | jd | |jddd\}}t| jd  ||fsD|r:t| jd ts:t	d| jd f |  j | jO  _ | S t| jd t
jt
jt
jt
jfr^|  j | jO  _ | S z| jd   | jd | jd< W n4 ty   d| jd  | jd   jj| jd t d f }|rt| jd tst	|Y nw t j| jd dd	rt| jd || jd< |  j | jO  _ tjtj@ otd
| jd | jd f  | S  | S )a$  Perform MIB variable ID and associated value conversion.

        Parameters
        ----------
        mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
            class instance representing MIB browsing functionality.

        Other Parameters
        ----------------
        ignoreErrors: :py:class:`bool`
            If `True` (default), ignore MIB object name or value casting
            failures if possible.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.ObjectType`
            reference to itself

        Raises
        ------
        SmiError
           In case of fatal MIB hanling errora

        Notes
        -----
        Calling this method involves
        :py:meth:`~pysnmp.smi.rfc1902.ObjectIdentity.resolveWithMib`
        method invocation.

        Examples
        --------
        >>> from pysmi.hlapi import varbinds
        >>> mibViewController = varbinds.AbstractVarBinds.getMibViewController( engine )
        >>> objectType = ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'), 'Linux i386')
        >>> objectType.resolveWithMib(mibViewController)
        ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr'), DisplayString('Linux i386'))
        >>> str(objectType)
        'SNMPv2-MIB::sysDescr."0" = Linux i386'
        >>>

        r   rK   rL   rM   r   z2MIB object %r is not OBJECT-TYPE (MIB not loaded?)z8MIB object %r having type %r failed to cast value %r: %sFrg   zresolved %r syntax is %r)r   r(   r   rY   rU   rW   rX   r2   r	   r   r   ZUnSpecifiedZNoSuchObjectZNoSuchInstanceZEndOfMibViewZ	getSyntaxr   r
   ri   r)   r*   r`   ra   r   ZObjectIdentifierrh   r   r   rR   rS   )r"   rb   ignoreErrorsrL   rM   errr   r   r%   rY   *  sZ   *
(
*zObjectType.resolveWithMibc                 C   s<   | j | j@ rd| jd  | jd  f S td| jj )N%s = %sr   r   r'   )r   r(   r   ri   r   r)   r*   r+   r   r   r%   ri     s
   zObjectType.prettyPrintr4   T)r*   r   r   r   r   r(   r&   r   ru   rp   r5   r9   r<   r>   rY   ri   r   r   r   r%   r   ~  s    9
! 
Wr   c                   @   sp   e Zd ZdZd\ZZdi fddZdd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd ZdddZdd ZdS )r   a*
  Create an object representing SNMP Notification.

    Instances of :py:class:`~pysnmp.smi.rfc1902.NotificationType` class are
    containers incorporating :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
    class instance (identifying particular notification) and a collection
    of MIB variables IDs that
    :py:class:`~pysnmp.entity.rfc3413.oneliner.cmdgen.NotificationOriginator`
    should gather and put into notification message.

    Typical notification is defined like this (from *IF-MIB.txt*):

    .. code-block:: bash

       linkDown NOTIFICATION-TYPE
           OBJECTS { ifIndex, ifAdminStatus, ifOperStatus }
           STATUS  current
           DESCRIPTION
                  "A linkDown trap signifies that the SNMP entity..."
           ::= { snmpTraps 3 }

    Corresponding NotificationType instantiation would look like this:

    .. code-block:: python

        NotificationType(ObjectIdentity('IF-MIB', 'linkDown'))

    To retain similarity with SNMP variable-bindings,
    :py:class:`~pysnmp.smi.rfc1902.NotificationType` objects behave like
    a sequence of :py:class:`~pysnmp.smi.rfc1902.ObjectType` class
    instances.

    See :RFC:`1902#section-2` for more information on NOTIFICATION-TYPE SMI
    definitions.

    Parameters
    ----------
    objectIdentity : :py:class:`~pysnmp.smi.rfc1902.ObjectIdentity`
        Class instance representing MIB notification type identification.
    instanceIndex : :py:class:`~pysnmp.proto.rfc1902.ObjectName`
        Trailing part of MIB variables OID identification that represents
        concrete instance of a MIB variable. When notification is prepared,
        `instanceIndex` is appended to each MIB variable identification
        listed in NOTIFICATION-TYPE->OBJECTS clause.
    objects : dict
        Dictionary-like object that may return values by OID key. The
        `objects` dictionary is consulted when notification is being
        prepared. OIDs are taken from MIB variables listed in
        NOTIFICATION-TYPE->OBJECTS with `instanceIndex` part appended.

    Notes
    -----
        Actual notification type and MIB variables look up occurs
        upon :py:meth:`~pysnmp.smi.rfc1902.NotificationType.resolveWithMib`
        invocation.

    Examples
    --------
    >>> from pysnmp.smi.rfc1902 import *
    >>> NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.3'))
    NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.3'), (), {})
    >>> NotificationType(ObjectIdentity('IP-MIB', 'linkDown'), ObjectName('3.5'))
    NotificationType(ObjectIdentity('1.3.6.1.6.3.1.1.5.3'), ObjectName('3.5'), {})

    r   r   c                 C   sB   t |tstd|f || _|| _|| _g | _g | _| j| _	d S r   )
rX   r   r   !_NotificationType__objectIdentity _NotificationType__instanceIndex_NotificationType__objects_NotificationType__varBinds%_NotificationType__additionalVarBindsr   _NotificationType__state)r"   r   ZinstanceIndexZobjectsr   r   r%   r&     s   
zNotificationType.__init__c                 C   r   r1   )r   r(   r   r   r)   r*   r   r   r   r%   r     rv   zNotificationType.__getitem__c                 C   s   d| j j| j| j| jf S )Nz%s(%r, %r, %r))r)   r*   r   r   r   r+   r   r   r%   rp     s   zNotificationType.__repr__c                 G   sH   t jt j@ ot d|f  | j| j@ rtd| jj | j	| | S )aY  Appends variable-binding to notification.

        Parameters
        ----------
        *varBinds : :py:class:`~pysnmp.smi.rfc1902.ObjectType`
            One or more :py:class:`~pysnmp.smi.rfc1902.ObjectType` class
            instances.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.NotificationType`
            reference to itself

        Notes
        -----
        This method can be used to add custom variable-bindings to
        notification message in addition to MIB variables specified
        in NOTIFICATION-TYPE->OBJECTS clause.

        Examples
        --------
        >>> nt = NotificationType(ObjectIdentity('IP-MIB', 'linkDown'))
        >>> nt.addVarBinds(ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))
        NotificationType(ObjectIdentity('IP-MIB', 'linkDown'), (), {})
        >>>

        zadditional var-binds: %rz%s object is already sealed)
r   rR   rS   r   r(   r   r)   r*   r   extend)r"   varBindsr   r   r%   addVarBinds  s   zNotificationType.addVarBindsc                 O   s   | j j|i | | S )a  Adds path to a repository to search ASN.1 MIB files.

        Parameters
        ----------
        *asn1Sources :
            one or more URL in form of :py:obj:`str` identifying local or
            remote ASN.1 MIB repositories. Path must include the *@mib@*
            component which will be replaced with MIB module name at the
            time of search.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.NotificationType`
            reference to itself

        Notes
        -----
        Please refer to :py:class:`~pysmi.reader.localfile.FileReader`,
        :py:class:`~pysmi.reader.httpclient.HttpReader` and
        :py:class:`~pysmi.reader.ftpclient.FtpReader` classes for
        in-depth information on ASN.1 MIB lookup.

        Examples
        --------
        >>> NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {}).addAsn1Source('http://mibs.snmplabs.com/asn1/@mib@')
        NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {})
        >>>

        )r   r9   r8   r   r   r%   r9     s   z!NotificationType.addAsn1MibSourcec                 G      | j j|  | S )a  Adds path to repository to search PySNMP MIB files.

        Parameters
        ----------
        *mibSources :
            one or more paths to search or Python package names to import
            and search for PySNMP MIB modules.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.NotificationType`
            reference to itself

        Notes
        -----
        Normally, ASN.1-to-Python MIB modules conversion is performed
        automatically through PySNMP/PySMI interaction. ASN1 MIB modules
        could also be manually compiled into Python via the
        `mibdump.py <http://snmplabs.com/pysmi/mibdump.html>`_
        tool.

        Examples
        --------
        >>> NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {}).addMibSource('/opt/pysnmp/mibs', 'pysnmp_mibs')
        NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {})
        >>>

        )r   r<   r;   r   r   r%   r<   #  s   zNotificationType.addMibSourcec                 G   r   )a=  Schedules search and load of given MIB modules.

        Parameters
        ----------
        *modNames:
            one or more MIB module names to load up and use for MIB
            variables resolution purposes.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.NotificationType`
            reference to itself

        Examples
        --------
        >>> NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {}).loadMibs('IF-MIB', 'TCP-MIB')
        NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {})
        >>>

        )r   r>   r=   r   r   r%   r>   C  s   zNotificationType.loadMibsc                 C   r3   r4   )r   r(   r+   r   r   r%   r5   [  r6   z NotificationType.isFullyResolvedTc           	   	   C   s  | j | j@ r| S | j| | jtttj	j
| j|| |jdd\}| j }i }t||rc| D ])}t|| j  ||}| jt|| j|tj|| t| jd ||< q8ntjtj@ oqtd| jf  | jD ].}t|tstt|d |d }||| |d |v r|| j||d  < qu| j| qug | _|  j | jO  _ tjtj@ otd| j| jf  | S  | S )a  Perform MIB variable ID conversion and notification objects expansion.

        Parameters
        ----------
        mibViewController : :py:class:`~pysnmp.smi.view.MibViewController`
            class instance representing MIB browsing functionality.

        Other Parameters
        ----------------
        ignoreErrors: :py:class:`bool`
            If `True` (default), ignore MIB object name or value casting
            failures if possible.

        Returns
        -------
        : :py:class:`~pysnmp.smi.rfc1902.NotificationType`
            reference to itself

        Raises
        ------
        SmiError
           In case of fatal MIB hanling errora

        Notes
        -----
        Calling this method might cause the following sequence of
        events (exact details depends on many factors):

        * :py:meth:`pysnmp.smi.rfc1902.ObjectIdentity.resolveWithMib` is called
        * MIB variables names are read from NOTIFICATION-TYPE->OBJECTS clause,
          :py:class:`~pysnmp.smi.rfc1902.ObjectType` instances are created
          from MIB variable OID and `indexInstance` suffix.
        * `objects` dictionary is queried for each MIB variable OID,
          acquired values are added to corresponding MIB variable

        Examples
        --------
        >>> notificationType = NotificationType(ObjectIdentity('IF-MIB', 'linkDown'))
        >>> notificationType.resolveWithMib(mibViewController)
        NotificationType(ObjectIdentity('IF-MIB', 'linkDown'), (), {})
        >>>

        rK   r   r   zAWARNING: MIB object %r is not NOTIFICATION-TYPE (MIB not loaded?)r   zresolved %r into %r)r   r(   r   rY   r   appendr   r   r   Z
apiTrapPDUZsnmpTrapOIDrU   rW   r2   rX   Z
getObjectsr   r   rV   r   r   rZ   r   rR   rS   r   )	r"   rb   r   ZSmiNotificationTyperf   ZvarBindsLocationZnotificationObjectr   r   r   r   r%   rY   ^  sX   ,





"zNotificationType.resolveWithMibc                 C   s2   | j | j@ rddd | jD S td| jj )N c                 S   s(   g | ]}d |d   |d   f qS )r   r   r   r   rA   r   r   r%   rD     s   ( z0NotificationType.prettyPrint.<locals>.<listcomp>r'   )r   r(   rT   r   r   r)   r*   r+   r   r   r%   ri     s   zNotificationType.prettyPrintNr   )r*   r   r   r   r   r(   r&   r   rp   r   r9   r<   r>   r5   rY   ri   r   r   r   r%   r     s    @
#! 
[r   )r`   Zpysnmp.protor   r   Zpysnmp.proto.apir   Zpysnmp.smi.builderr   Zpysnmp.smi.compilerr   Zpysnmp.smi.errorr   Zpyasn1.type.baser	   Zpyasn1.errorr
   Zpysnmpr   __all__objectr   r   r   r   r   r   r%   <module>   s&       n  