
    $g#                         d Z ddlmZ ddlZddlmZ ddlm	Z	m
Z
 i Zi Z G d de	          Z G d de
          Z G d	 d
e
          Zd Zd Zd Zd Zedk    r e             dS  e             dS )z
Provides access to public OUI and IAB registration data published by the IEEE.

More details can be found at the following URLs :-

    - IEEE Home Page - http://www.ieee.org/
    - Registration Authority Home Page - http://standards.ieee.org/regauth/
    N)_open_binary)
Subscriber	Publisherc                       e Zd ZdZd Zd ZdS )FileIndexerz
    A concrete Subscriber that receives OUI record offset information that is
    written to an index data file as a set of comma separated records.
    c                     t          |d          rt          |d          r|| _        nt          |d          | _        t          j        | j        d          | _        dS )z
        Constructor.

        :param index_file: a file-like object or name of index file where
            index records will be written.
        readlinetellw
)lineterminatorN)hasattrfhopen_csvwriter)self
index_files     P/var/www/html/netbox-4.1.3/venv/lib/python3.11/site-packages/netaddr/eui/ieee.py__init__zFileIndexer.__init__7   s]     :z** 	,wz6/J/J 	, DGG:s++DGk$'$???    c                 :    | j                             |           dS )z
        Receives and writes index data to a CSV data file.

        :param data: record containing offset record information.
        N)r   writerow)r   datas     r   updatezFileIndexer.updateE   s      	T"""""r   N)__name__
__module____qualname____doc__r   r    r   r   r   r   1   s?         
@ @ @# # # # #r   r   c                   (     e Zd ZdZ fdZd Z xZS )OUIIndexParsera  
    A concrete Publisher that parses OUI (Organisationally Unique Identifier)
    records from IEEE text-based registration files

    It notifies registered Subscribers as each record is encountered, passing
    on the record's position relative to the start of the file (offset) and
    the size of the record (in bytes).

    The file processed by this parser is available online from this URL :-

        - http://standards.ieee.org/regauth/oui/oui.txt

    This is a sample of the record structure expected::

        00-CA-FE   (hex)        ACME CORPORATION
        00CAFE     (base 16)        ACME CORPORATION
                        1 MAIN STREET
                        SPRINGFIELD
                        UNITED STATES
    c                     t          t          |                                            t          |d          rt          |d          r	|| _        dS t          |d          | _        dS )z
        Constructor.

        :param ieee_file: a file-like object or name of file containing OUI
            records. When using a file-like object always open it in binary
            mode otherwise offsets will probably misbehave.
        r	   r
   rbN)superr"   r   r   r   r   r   	ieee_file	__class__s     r   r   zOUIIndexParser.__init__d   d     	nd##,,...9j)) 	,gi.H.H 	,DGGG9d++DGGGr   c                 >   d}d}d}d}d}d}	 | j                                         }|sn|r||v rd}|r(||v r|*|                    |           |                     |           t	          |          }| j                                         t	          |          z
  }|                                d         }	t          |	                    ||          d          }
|
|g}n|t	          |          z  }|                    |           |                     |           dS )	z
        Starts the parsing process which detects records and notifies
        registered subscribers as it finds each OUI record.
        TNr      (hex)   -r   F   )	r   r	   appendnotifylenr
   splitintreplace)r   skip_headerrecordsizemarkerhyphenempty_stringlineoffsetouiindexs              r   parsezOUIIndexParser.parses   s9   
 	"7##%%D  $v~~# ~~%MM$'''KK'''4yy#d))3jjll1oCKK==rBB D		!7	"< 	dFr   r   r   r   r   r   r>   __classcell__r(   s   @r   r"   r"   N   sQ         *, , , , ,, , , , , , ,r   r"   c                   (     e Zd ZdZ fdZd Z xZS )IABIndexParsera  
    A concrete Publisher that parses IAB (Individual Address Block) records
    from IEEE text-based registration files

    It notifies registered Subscribers as each record is encountered, passing
    on the record's position relative to the start of the file (offset) and
    the size of the record (in bytes).

    The file processed by this parser is available online from this URL :-

        - http://standards.ieee.org/regauth/oui/iab.txt

    This is a sample of the record structure expected::

        00-50-C2   (hex)        ACME CORPORATION
        ABC000-ABCFFF     (base 16)        ACME CORPORATION
                        1 MAIN STREET
                        SPRINGFIELD
                        UNITED STATES
    c                     t          t          |                                            t          |d          rt          |d          r	|| _        dS t          |d          | _        dS )z
        Constructor.

        :param ieee_file: a file-like object or name of file containing IAB
            records. When using a file-like object always open it in binary
            mode otherwise offsets will probably misbehave.
        r	   r
   r$   N)r%   rC   r   r   r   r   r&   s     r   r   zIABIndexParser.__init__   r)   r   c                    d}d}d}d}d}d}d}	 | j                                         }|sn)|r||v rd}|r)||v r|*|                    |           |                     |           | j                                         t          |          z
  }	|                                d         }
|
}||	g}t          |          }n||v r}|t          |          z  }|d                             ||          }|                                d         }|                    |          d         }t          ||z   d	          d
z	  |d<   n|t          |          z  }F|                    |           |                     |           dS )z
        Starts the parsing process which detects records and notifies
        registered subscribers as it finds each IAB record.
        TNr   r+   s	   (base 16)r,   r   Fr-      )	r   r	   r.   r/   r
   r0   r1   r3   r2   )r   r4   r5   r6   
hex_markerbase16_markerr8   r9   r:   r;   
iab_prefixr=   prefixsuffixs                 r   r>   zIABIndexParser.parse   s   
 
$!	"7##%%D  $zT11# T!!%MM$'''KK'''#d))3!ZZ\\!_
"4yy$&&D		!**6<@@af--a0 &"55"<q		 D		!C!	"H 	dFr   r?   rA   s   @r   rC   rC      sQ         *, , , , ,3 3 3 3 3 3 3r   rC   c                      ||           }|                     t          |                     |                                 dS )z4Generate an index files from the IEEE registry file.N)attachr   r>   )registry_fh
index_pathparser
oui_parsers       r   create_index_from_registryrR      sE    $$Jk*--...r   c                     t          t          j        t          j        t                    d          t          j        t          j        t                    d          t
                     t          t          j        t          j        t                    d          t          j        t          j        t                    d          t                     dS )z1Create indices for OUI and IAB file based lookupszoui.txtoui.idxziab.txtiab.idxN)rR   _pathjoindirname__file__r"   rC   r    r   r   create_indicesrZ     s    
5=**I66
5=**I66  
 
5=**I66
5=**I66    r   c                 ,   	 t          j        d |D                       D ]E}d |D             \  }}}|                     |g            | |                             ||f           F	 |                                 dS # |                                 w xY w)z/Load index from file into index data structure.c                 8    g | ]}|                     d           S )zUTF-8)decode).0xs     r   
<listcomp>zload_index.<locals>.<listcomp>  s$    >>>a 1 1>>>r   c                 ,    g | ]}t          |          S r    )r2   )r^   _s     r   r`   zload_index.<locals>.<listcomp>  s    "7"7"7a3q66"7"7"7r   N)r   reader
setdefaultr.   close)r=   fprowkeyr;   r6   s         r   
load_indexri     s    ;>>2>>>?? 	. 	.C"7"73"7"7"7S&$S"%%%#Jvtn----	.
 	








s   A$A= =Bc                      t          t          t          t          d                     t          t          t          t          d                     dS )z+Load OUI and IAB lookup indices into memoryrT   rU   N)ri   	OUI_INDEXr   __package__	IAB_INDEXr    r   r   load_indicesrn     s<    y,{I>>???y,{I>>?????r   __main__)r   os.pathpathrV   csvr   netaddr.compatr   netaddr.corer   r   rk   rm   r   r"   rC   rR   rZ   ri   rn   r   r    r   r   <module>ru      sh  4            ' ' ' ' ' ' . . . . . . . . 	 	# # # # #* # # #:Q Q Q Q QY Q Q QhX X X X XY X X Xv      @ @ @ zN LNNNNNr   