HEX
Server: LiteSpeed
System: Linux us-phx-web1284.main-hosting.eu 4.18.0-553.109.1.lve.el8.x86_64 #1 SMP Thu Mar 5 20:23:46 UTC 2026 x86_64
User: u300739242 (300739242)
PHP: 8.2.30
Disabled: system, shell_exec, passthru, mysql_list_dbs, ini_alter, dl, symlink, link, chgrp, leak, popen, apache_child_terminate, virtual, mb_send_mail
Upload Files
File: //opt/alt/python311/lib/python3.11/site-packages/pyroute2/ndb/__pycache__/view.cpython-311.pyc
�

�;fB��H�dZddlZddlZddlZddlZddlZddlZddlmZddl	m
Z
ddlmZm
Z
ddlmZddlmZdd	lmZdd
lmZddlmZmZddlmZmZdd
lmZddlmZddl m!Z!ddl"m#Z#m$Z$ddl%m&Z&m'Z'Gd�d��Z(Gd�de)��Z*Gd�de*��Z+dS)a�
Accessing objects
=================

NDB objects are grouped into "views":

    * interfaces
    * addresses
    * routes
    * neighbours
    * rules
    * netns
    * ...

Views are dictionary-like objects that accept strings or dict selectors::

    # access eth0
    ndb.interfaces["eth0"]

    # access eth0 in the netns test01
    ndb.sources.add(netns="test01")
    ndb.interfaces[{"target": "test01", "ifname": "eth0"}]

    # access a route to 10.4.0.0/24
    ndb.routes["10.4.0.0/24"]

    # same with a dict selector
    ndb.routes[{"dst": "10.4.0.0", "dst_len": 24}]

Objects cache
=============

NDB create objects on demand, it doesn't create thousands of route objects
for thousands of routes by default. The object is being created only when
accessed for the first time, and stays in the cache as long as it has any
not committed changes. To inspect cached objects, use views' `.cache`::

    >>> ndb.interfaces.cache.keys()
    [(('target', u'localhost'), ('tflags', 0), ('index', 1)),  # lo
     (('target', u'localhost'), ('tflags', 0), ('index', 5))]  # eth3

There is no asynchronous cache invalidation, the cache is being cleaned up
every time when an object is accessed.

API
===
�N)�OrderedDict)�partial)�cli�config)�
basestring�)�
check_auth)�RSLV_DELETE)�Address)�	Interface�Vlan)�	FDBRecord�	Neighbour)�NetNS)�Route)�Rule)�Record�	RecordSet)�Source�SourceProxyc� �eZdZd�Zd�Zd�ZdS)�
TmpHandlerc�0�||_||_||_dS�N)�ndb�event�handler)�selfrrrs    �D/opt/alt/python311/lib/python3.11/site-packages/pyroute2/ndb/view.py�__init__zTmpHandler.__init__Ls�������
������c��|jj�|jjj|j|j��|Sr)r�task_manager�register_handler�schema�classesrr�rs r�	__enter__zTmpHandler.__enter__Qs;�����.�.��H�O�#�D�J�/���	
�	
�	
��r!c��|jj�|jjj|j|j��dSr)rr#�unregister_handlerr%r&rr�r�exc_type�	exc_value�	tracebacks    r�__exit__zTmpHandler.__exit__Ws>�����0�0��H�O�#�D�J�/���	
�	
�	
�	
�	
r!N)�__name__�
__module__�__qualname__r r(r/�r!rrrKsA���������
���
�
�
�
�
r!rc�D�eZdZdZd$d�Zd�Zd�Zed���Zed���Z	d%d�Z
d%d	�Zej
ed
��d$d�����Zd%d�Zej
ed
��d�����Zej
ed
��d�����Zej
ed
��d�����Zed
��d���Zed
��d$d���Zed
��d%d���Zd%d�Zd�Zd�Zd�Zd�Zed��d���Zed��d���Zed��d���Zejd���Z d�Z!d�Z"d �Z#ejed��d!�����Z$ejed��d"�����Z%d#�Z&dS)&�Viewz�
    The View() object returns RTNL objects on demand::

        ifobj1 = ndb.interfaces['eth0']
        ifobj2 = ndb.interfaces['eth0']
        # ifobj1 != ifobj2
    Nc���||_|j�d|z��|_||_||_||_i|_|�g}|r
||jz
}||_i|_t��|_
t|j
d<t|j
d<t|j
d<t|j
d<t|j
d<t |j
d<t"|j
d<t$|j
d	<dS)
Nzview.%s�
interfaces�	addresses�
neighbours�
af_bridge_fdb�routes�rules�netns�af_bridge_vlans)r�log�channel�tabler�chain�cache�
auth_managers�constraintsrr&rrrrrrrr
)rrrArBrDs     rr z
View.__init__fs�������7�?�?�9�u�#4�5�5�����
���
���
���
�� ��M��	1��U�0�0�M�*������"�}�}���%.���\�"�$+���[�!�%.���\�"�(1���_�%�!&���X�� $���W�� %���W��*.���&�'�'�'r!c��|Srr3r's rr(zView.__enter__}s���r!c��dSrr3r+s    rr/z
View.__exit__�s���r!c�H�|jdkr|jjS|jjS)Nr=)rAr�	nsmanager�	localhostr's r�default_targetzView.default_target�s%���:�� � ��8�%�%��8�%�%r!c�,�|j�|jjSiSr)rB�contextr's rrMzView.context�s���:�!��:�%�%��Ir!c�P�|jj�|p|j|��Sr)rr#�db_getrA)r�specrAs   r�getmanyzView.getmany�s$���x�$�+�+�E�,?�T�Z��F�F�Fr!c�>�|�||��D]}|cSdSr)rQ)rrPrA�objs    r�getonezView.getone�s2���<�<��e�,�,�	�	�C��J�J�J�	�	r!zobj:readc�Z�|p|}	|�||��S#t$rYdSwxYwr)�__getitem__�KeyError)rrPrA�kwargs    r�getzView.get�sJ���}�u��	��#�#�D�%�0�0�0���	�	�	��4�4�	���s��
*�*c���|jr
|jj}ni}|j|p|j}|�|||j��}|||d|j|j���S)NF)�load�masterrD)rBrMr&rA�new_specrKrD)r�keyrArM�iclassrPs      r�templatez
View.template�su���:�	��j�(�G�G��G���e�1�t�z�2�����s�G�T�-@�A�A���v�����:��,�
�
�
�	
r!z
obj:modifyc���|j|j}|jr
|jj}ni}|�|p|d||j��}|jr
|j|d<d|d<||S)Nr�	ndb_chainT�create)r&rArBrMr]rK)r�argspec�kwspecr_rMrPs      rrczView.create�s~����d�j�)���:�	��j�(�G�G��G����� �g�a�j�'�4�+>�
�
���:�	+� $�
�D�����X���D�z�r!c��	|jdi|��}n#t$r|jdi|��}YnwxYw|���D]
\}}|||<�|S)Nr3)�locaterWrc�items)rrdrerSr^�values      r�ensurezView.ensure�s���	(��$�+�'�'��'�'�C�C���	(�	(�	(��$�+�'�'��'�'�C�C�C�	(���� �,�,�.�.�	�	�J�C���C��H�H��
s�
�*�*c�P�|j�d��|j|i|��S)Na�

        The name add() will be removed in future releases, use create()
        instead. If you believe that the idea to rename is wrong, please
        file your opinion to the project's bugtracker.

        The reason behind the rename is not to confuse interfaces.add() with
        bridge and bond port operations, that don't create any new interfaces
        but work on existing ones.
        )r?�warningrc)rrdres   r�addzView.add�s<��	
����
�
	
�
	
�
	
��t�{�G�.�v�.�.�.r!c�H�d}|�dd��}|�dd��}tj��}tjd���}d�}t	|j|jt||����5	|�|��}|r|dks|�|d	kr|cddd��S	|�d
���\}}	n#tj	$rYnwxYw|dkr(||ztj��krt�����#1swxYwYdS)N�timeout����actionrm�d)�maxsizec�b�	|�||f��dS#tj$rYdSwxYwr)�
put_nowait�queue�Full)�evq�targetrs   rrzView.wait.<locals>.handler�sG��
�������/�/�/�/�/���:�
�
�
����
���s��.�.T�remover)ro)�pop�timerv�QueuerrrrrY�Empty�TimeoutError)
rrP�retrorq�ctimerxrry�msgs
          r�waitz	View.wait�s������(�(�9�b�)�)�����(�E�*�*���	�����k�#�&�&�&��	�	�	����$�*�g�g�s�.C�.C�
D�
D�
	-�
	-�
-��h�h�t�n�n����F�e�O�O��K�F�h�$6�$6��

	-�
	-�
	-�
	-�
	-�
	-�
	-�
	-��"%�'�'�!�'�"4�"4�K�F�C�C���{�����D������R�<�<��w������4�4�*�n�n�,�
-�
	-�
	-�
	-�
	-����
	-�
	-�
	-�
	-�
	-�
	-s6�(D�9C�D�C%�"D�$C%�%2D�D�Dc��t|t��r|���}|p|}|std���|p|j}|j|}|�|��}|jjj	|d}|jjj	|d}i}|D]B}|�
|��}||vr||||<�'||vr||}	|	|vr||	||	<�C|std���||S)a�
        This method works like `__getitem__()`, but the important
        difference is that it uses only key fields to locate the
        object in the DB, ignoring all other keys.

        It is useful to locate objects that may change attributes
        during request, like an interface may come up/down, or an
        address may become primary/secondary, so plain
        `__getitem__()` will not match while the object still
        exists.
        zgot an empty spec�norm_idx�lookup_fallbackszgot an empty key)�
isinstancer�_as_dict�	TypeErrorrAr&r]rr%�compiled�nla2namerW)
rrPrArXr_�kspecr��request�name�fallbacks
          rrgzView.locates(���d�F�#�#�	#��=�=�?�?�D��}�u���	1��/�0�0�0��#������e�$�����t�$�$�����(��/�
�;���8�?�3�E�:�;M�N�����	7�	7�D��?�?�4�(�(�D��t�|�|� $�T�
���
�
��)�)�)�+�D�1���t�#�#�(,�X��G�H�%���	/��-�.�.�.��G�}�r!c�:�|�||��}t|j�����}t	j��}t|j��D]�}||kr�	||j|jz
tjkri|j|j	rWtj|j|��r8|j�
d|����|j�|d����||jvr4|j�
d|����~|j|}||_|S|�|��r;|���|j�
d|����||j|<|���|S)Nz
cache del z
cache hit z
cache add )r`�tupler^rhr|rC�atimer�cache_expire�clean�gc�
get_referrersr?�debugr{�exists�load_sql�register)rr^rAr��	cache_key�rtime�ckeys       rrVzView.__getitem__'s����m�m�C��'�'��
�#�'�-�-�/�/�*�*�	��	�����$�*�%�%�	+�	+�D��y� � ����
�4�(�.�.��1D�D�D��J�t�$�*�E��$�T�Z��%5�6�6�E��������7�8�8�8��
���t�T�*�*�*����
�"�"��H�N�N�N�Y�Y�8�9�9�9���*�Y�'�C��C�I��J��{�{�3���
,��������������<�=�=�=�(+��
�9�%��������
r!c��|jr
|jj}ni}|j|j}|�|||j��}|�|||jt���|p|j}|j	j
}|j	j}|j|jd}|j
�d|�d|����g}g}	|���D]�\}
}|�|
��}||vr|}
|�u|
|vrq|�d|
�d|j����t'|t(t*t,t.f��rt1j|��}|	�|����|�d|j�d	d
�|����|	��}
|
�|j
�d��dS|j
�d
��dS)z�
        Check if the specified object exists in the database::

            ndb.interfaces.exists('eth0')
            ndb.interfaces.exists({'ifname': 'eth0', 'target': 'localhost'})
            ndb.addresses.exists('127.0.0.1/8')
        )�viewrP�fields�policy�	all_nameszcheck if the key z exists in table N�f_z = zSELECT * FROM z WHERE z AND r�Tz
not existsF)rBrMr&rAr]rK�resolve�resolve_fieldsr
rr%r#r�r?r�rh�name2nla�append�plchr��dict�listr��set�json�dumps�db_fetchone�join)rr^rArMr_r%r#�names�keys�valuesr�ri�nla_namerPs              rr�zView.existsYs����:�	��j�(�G�G��G���d�j�)���o�o�c�7�D�,?�@�@��������(��		�	
�	
�	
��#���������x�,�����
�+�K�8�������3�3�3���N�O�O�O������9�9�;�;�	%�	%�K�D�%����t�,�,�H��5� � ���� �T�U�]�]�����4�4�4����=�>�>�>��e�d�D�%��%=�>�>�.� �J�u�-�-�E��
�
�e�$�$�$���'�'�'�+/�:�:�:�w�|�|�D�7I�7I�7I�J��
�
�����H�N�N�8�$�$�$��4��H�N�N�<�(�(�(��5r!c��t���r��NotImplementedError)rr^ris   r�__setitem__zView.__setitem__����!�#�#�#r!c��t���rr��rr^s  r�__delitem__zView.__delitem__�r�r!c�*�|���Sr�r�r's r�__iter__z
View.__iter__�s���y�y�{�{�r!c�.�||���vSr��dumpr�s  r�__contains__zView.__contains__�s���d�i�i�k�k�!�!r!�obj:listc#�@K�|���D]}|V��dSrr�)r�records  rr�z	View.keys�s2�����i�i�k�k�	�	�F��L�L�L�L�	�	r!c#�LK�|���D]}||V��
dSrr�r�s  rr�zView.values�s6�����9�9�;�;�	�	�C��s�)�O�O�O�O�	�	r!c#�PK�|���D]}|||fV��dSrr�r�s  rrhz
View.items�s?�����9�9�;�;�	#�	#�C���S�	�"�"�"�"�"�	#�	#r!c�X�|j|j�|��dS)Nr)r&rA�_countr's r�countz
View.count�s$���|�D�J�'�.�.�t�4�4�Q�7�7r!c�*�|���Sr)r�r's r�__len__zView.__len__�s���z�z�|�|�r!c�^�ddg|jjj|jp|jdzS)Nry�tflagsr�)rr%r�r�rA�rr_s  r�_keysz
View._keys�s6���(�#�d�h�o�&>��K�'�6�<�'
�
�'��	r!c#�xK�t|��}|D]%}t|||j|j��V��&dSr)�nextrr&rA)rr��fnamesr�s    r�_nativezView._native�sR�����d�����	C�	C�F������d�j�)A�B�B�B�B�B�B�	C�	Cr!c���|j|j}t|�|�|����d|jj�dd��i���S�N�recordset_pipe�false)r)r&rArr�r�rrrYr�s  rr�z	View.dump�sg����d�j�)����L�L����T�*�*�+�+� �$�(�/�"5�"5�$�g�#�#��
�
�
�	
r!c���|j|j}t|�|�|����d|jj�dd��i���Sr�)r&rArr��summaryrrrYr�s  rr�zView.summary�sg����d�j�)����L�L�����-�-�.�.� �$�(�/�"5�"5�$�g�#�#��
�
�
�	
r!c��|jrd|jvr|jd�d�}nd}d|�|j�d|����d�S)N�ifname�/�z
NDB view for z
Number of objects: z�

to list objects use .summary() or .dump()
    -> RecordSet (generator)
        -> Record

key: Union[Record, dict, spec]
to get objects use ...[key] / .__getitem__(key)
    -> RTNL_Object
)rBrAr�)r�parents  r�__repr__z
View.__repr__�so���:�	�(�d�j�0�0��
�8�,�/�/�/�F�F��F�����j����J�J�L�L����	r!)NNr)'r0r1r2�__doc__r r(r/�propertyrKrMrQrTr�change_pointerr	rYr`rcrjrmr�rgrVr�r�r�r�r�r�r�rh�show_resultr�r�r�r�r�r�r�r3r!rr5r5]s���������/�/�/�/�.���
�
�
��&�&��X�&�����X��G�G�G�G�����	���Z�
�����������

�

�

�

�	���Z�����������	���Z�����������	���Z����/�/�����/��Z�
���#-�#-���#-�J�Z�
���#�#�#���#�J�Z�
���/�/�/���/�b1�1�1�1�f$�$�$�$�$�$����"�"�"��Z�
���������Z�
���������Z�
���#�#���#�	�_�8�8��_�8�������
C�C�C�
	�_��Z�
���	
�	
����_�	
�	�_��Z�
���	
�	
����_�	
�����r!r5c���eZdZd
�fd�	Zd�Zd�Zejdfd�Ze	d��d���Z
d	�Zd
�Zd�Z
d
d�Z�xZS)�SourcesViewNc����tt|���|d��t|jd<i|_i|_tj��|_	|�g}||_
dS)N�sources)�superr�r rr&rC�proxy�	threading�Lock�lockrD)rrrD�	__class__s   �rr zSourcesView.__init__�se���
�k�4� � �)�)�#�y�9�9�9�"(���Y����
���
��N�$�$��	�� ��M�*����r!c���ttj|����}t|jfi|�����|j|d<|j|dS�Nry)r�r�defaultsr�startrC�rrPs  r�	async_addzSourcesView.async_add�sX���F�O�D�)�)�*�*��%+�D�H�%=�%=��%=�%=�%C�%C�%E�%E��
�4��>�"��z�$�x�.�)�)r!c��ttj|����}|d}||vrtd|�d����d|vrd}t	j��|d<nd}t|jfi|�����|j|d<|r*|j|dj	�
��|j|dS)Nry�source z existsrTF)r�rr�rWr��Eventrr�rCrr�)rrPry�syncs    rrmzSourcesView.add�s����F�O�D�)�)�*�*���h����T�>�>��4�V�4�4�4�5�5�5��$����D�%�O�-�-�D��M�M��D�%+�D�H�%=�%=��%=�%=�%C�%C�%E�%E��
�4��>�"��	4��J�t�H�~�&�,�1�1�3�3�3��z�$�x�.�)�)r!Tc��||vrtd|�d����|j5||jvrJ|j|}|�||���|j�|��cddd��S	ddd��dS#1swxYwYdS)Nr�z does not exist)�coder�)rWr�rC�closer{)rryr�r��sources     rrzzSourcesView.removes��������<�V�<�<�<�=�=�=�
�Y�	.�	.����#�#���F�+�����$�T��2�2�2��z�~�~�f�-�-�		.�	.�	.�	.�	.�	.�	.�	.�#�	.�	.�	.�	.�	.�	.�	.�	.�	.�	.�	.�	.����	.�	.�	.�	.�	.�	.s�AB�B�Br�c#�&K�|jD]}|V��dSr)rCr�s  rr�zSourcesView.keys
s,�����:�	�	�C��I�I�I�I�	�	r!c�
�ddgS)Nry�kindr3r�s  rr�zSourcesView._keyss
���&�!�!r!c��t���rr�r�s  rr�zSourcesView.waitr�r!c��|j|i|��Sr)�_dump)r�argvrXs   r�_summaryzSourcesView._summarys���t�z�4�)�5�)�)�)r!c�P�t|t��r|}nBt|t��rd|���vr	|d}nt	���||jvr
|j|S||jvr
|j|St|j|��}||j|<|Sr�)	r�rr�r�rWrCr�rr)rr^rAryr�s     rrVzSourcesView.__getitem__s����c�:�&�&�	��F�F�
��T�
"�
"�	�x�3�8�8�:�:�'=�'=���]�F�F��*�*���T�Z����:�f�%�%�
�t�z�
!�
!��:�f�%�%����&�1�1�E�!&�D�J�v���Lr!r)r0r1r2r r�rm�errno�
ECONNRESETrzr	r�r�r�rrV�
__classcell__)r�s@rr�r��s��������+�+�+�+�+�+�*�*�*�

*�
*�
*�#(�"2��.�.�.�.��Z�
��������"�"�"�$�$�$�*�*�*��������r!r�),r�rr�r�rvr�r|�collectionsr�	functoolsr�pyroute2rr�pyroute2.commonr�auth_managerr	�objectsr
�objects.addressr�objects.interfacerr
�objects.neighbourrr�
objects.netnsr�
objects.router�objects.ruler�reportrrr�rrrr�r5r�r3r!r�<module>rs��.�.�`
����	�	�	�	�����������������#�#�#�#�#�#������� � � � � � � � �&�&�&�&�&�&�%�$�$�$�$�$� � � � � � �$�$�$�$�$�$�.�.�.�.�.�.�.�.�3�3�3�3�3�3�3�3� � � � � � � � � � � � �������%�%�%�%�%�%�%�%�'�'�'�'�'�'�'�'�
�
�
�
�
�
�
�
�$E�E�E�E�E�4�E�E�E�PE�E�E�E�E�$�E�E�E�E�Er!