%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/pyudev/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/pyudev/__pycache__/monitor.cpython-312.pyc

�

9*�b R���dZddlZddlZddlmZddlmZddlmZm	Z	ddl
mZmZddl
mZGd�d	e�ZGd
�de�Zy)z�
    pyudev.monitor
    ==============

    Monitor implementation.

    .. moduleauthor::  Sebastian Wiesner  <lunaryorn@gmail.com>
�N)�partial)�Thread)�pipe�poll)�eintr_retry_call�ensure_byte_string)�Devicec��eZdZdZd�Zd�Zedd��Zed��Z	d�Z
dd�Zd	�Zd
�Z
d�Zd�Zd
�Zd�Zdd�Zd�Zd�Zy)�Monitorat
    A synchronous device event monitor.

    A :class:`Monitor` objects connects to the udev daemon and listens for
    changes to the device list.  A monitor is created by connecting to the
    kernel daemon through netlink (see :meth:`from_netlink`):

    >>> from pyudev import Context, Monitor
    >>> context = Context()
    >>> monitor = Monitor.from_netlink(context)

    Once the monitor is created, you can add a filter using :meth:`filter_by()`
    or :meth:`filter_by_tag()` to drop incoming events in subsystems, which are
    not of interest to the application:

    >>> monitor.filter_by('input')

    When the monitor is eventually set up, you can either poll for events
    synchronously:

    >>> device = monitor.poll(timeout=3)
    >>> if device:
    ...     print('{0.action}: {0}'.format(device))
    ...

    Or you can monitor events asynchronously with :class:`MonitorObserver`.

    To integrate into various event processing frameworks, the monitor provides
    a :func:`selectable <select.select>` file description by :meth:`fileno()`.
    However, do *not*  read or write directly on this file descriptor.

    Instances of this class can directly be given as ``udev_monitor *`` to
    functions wrapped through :mod:`ctypes`.

    .. versionchanged:: 0.16
       Remove :meth:`from_socket()` which is deprecated, and even removed in
       recent udev versions.
    c�P�||_||_|j|_d|_y)NF)�context�_as_parameter_�_libudev�_started)�selfr
�	monitor_ps   �0/usr/lib/python3/dist-packages/pyudev/monitor.py�__init__zMonitor.__init__Ns%�����'����(�(��
���
�c�:�|jj|�y�N)r�udev_monitor_unref�rs r�__del__zMonitor.__del__Ts���
�
�(�(��.rc��|dvrtdj|���|jj|t	|��}|std��|||�S)a�
        Create a monitor by connecting to the kernel daemon through netlink.

        ``context`` is the :class:`Context` to use.  ``source`` is a string,
        describing the event source.  Two sources are available:

        ``'udev'`` (the default)
          Events emitted after udev as registered and configured the device.
          This is the absolutely recommended source for applications.

        ``'kernel'``
          Events emitted directly after the kernel has seen the device.  The
          device has not yet been configured by udev and might not be usable
          at all.  **Never** use this, unless you know what you are doing.

        Return a new :class:`Monitor` object, which is connected to the
        given source.  Raise :exc:`~exceptions.ValueError`, if an invalid
        source has been specified.  Raise
        :exc:`~exceptions.EnvironmentError`, if the creation of the monitor
        failed.
        )�kernel�udevz8Invalid source: {0!r}. Must be one of "udev" or "kernel"zCould not create udev monitor)�
ValueError�formatr�udev_monitor_new_from_netlinkr�EnvironmentError)�clsr
�source�monitors    r�from_netlinkzMonitor.from_netlinkWsi��.�+�+���$�f�V�n��
��"�"�@�@��'��/�
���"�#B�C�C��7�G�$�$rc��|jS)z�
        ``True``, if this monitor was started, ``False`` otherwise. Readonly.

        .. seealso:: :meth:`start()`
        .. versionadded:: 0.16
        )rrs r�startedzMonitor.startedzs���}�}�rc�8�|jj|�S)z�
        Return the file description associated with this monitor as integer.

        This is really a real file descriptor ;), which can be watched and
        :func:`select.select`\ ed.
        )r�udev_monitor_get_fdrs r�filenozMonitor.fileno�s���}�}�0�0��6�6rNc��t|�}|�t|�}|jj|||�|jj|�y)a
        Filter incoming events.

        ``subsystem`` is a byte or unicode string with the name of a
        subsystem (e.g. ``'input'``).  Only events originating from the
        given subsystem pass the filter and are handed to the caller.

        If given, ``device_type`` is a byte or unicode string specifying the
        device type.  Only devices with the given device type are propagated
        to the caller.  If ``device_type`` is not given, no additional
        filter for a specific device type is installed.

        These filters are executed inside the kernel, and client processes
        will usually not be woken up for device, that do not match these
        filters.

        .. versionchanged:: 0.15
           This method can also be after :meth:`start()` now.
        N)rr�/udev_monitor_filter_add_match_subsystem_devtype�udev_monitor_filter_update)r�	subsystem�device_types   r�	filter_byzMonitor.filter_by�sK��('�y�1�	��"�,�[�9�K��
�
�E�E��)�[�	
�	
�
�
�0�0��6rc��|jj|t|��|jj|�y)aR
        Filter incoming events by the given ``tag``.

        ``tag`` is a byte or unicode string with the name of a tag.  Only
        events for devices which have this tag attached pass the filter and are
        handed to the caller.

        Like with :meth:`filter_by` this filter is also executed inside the
        kernel, so that client processes are usually not woken up for devices
        without the given ``tag``.

        .. udevversion:: 154

        .. versionadded:: 0.9

        .. versionchanged:: 0.15
           This method can also be after :meth:`start()` now.
        N)r�!udev_monitor_filter_add_match_tagrr-)r�tags  r�
filter_by_tagzMonitor.filter_by_tag�s1��&	
�
�
�7�7��>P�QT�>U�V��
�
�0�0��6rc�p�|jj|�|jj|�y)a
        Remove any filters installed with :meth:`filter_by()` or
        :meth:`filter_by_tag()` from this monitor.

        .. warning::

           Up to udev 181 (and possibly even later versions) the underlying
           ``udev_monitor_filter_remove()`` seems to be broken.  If used with
           affected versions this method always raises
           :exc:`~exceptions.ValueError`.

        Raise :exc:`~exceptions.EnvironmentError` if removal of installed
        filters failed.

        .. versionadded:: 0.15
        N)r�udev_monitor_filter_remover-rs r�
remove_filterzMonitor.remove_filter�s(��"	
�
�
�0�0��6��
�
�0�0��6rc�X�ddl}|jdt�|j�y)a�
        Switch the monitor into listing mode.

        Connect to the event source and receive incoming events.  Only after
        calling this method, the monitor listens for incoming events.

        .. note::

           This method is implicitly called by :meth:`__iter__`.  You don't
           need to call it explicitly, if you are iterating over the
           monitor.

        .. deprecated:: 0.16
           Will be removed in 1.0. Use :meth:`start()` instead.
        rNz4Will be removed in 1.0. Use Monitor.start() instead.)�warnings�warn�DeprecationWarning�start)rr9s  r�enable_receivingzMonitor.enable_receiving�s$��"	��
�
�B�DV�	
�	
�
�
�rc��|jsG|jj|�tj|t
j�d|_yy)a�
        Start this monitor.

        The monitor will not receive events until this method is called. This
        method does nothing if called on an already started :class:`Monitor`.

        .. note::

           Typically you don't need to call this method. It is implicitly
           called by :meth:`poll()` and :meth:`__iter__()`.

        .. seealso:: :attr:`started`
        .. versionchanged:: 0.16
           This method does nothing if the :class:`Monitor` was already
           started.
        TN)rr�udev_monitor_enable_receivingr�set_fd_status_flag�os�
O_NONBLOCKrs rr<z
Monitor.start�s=��"�}�}��M�M�7�7��=��#�#�D�"�-�-�8� �D�M�	rc�<�|jj||�y)aN
        Set the receive buffer ``size``.

        ``size`` is the requested buffer size in bytes, as integer.

        .. note::

           The CAP_NET_ADMIN capability must be contained in the effective
           capability set of the caller for this method to succeed.  Otherwise
           :exc:`~exceptions.EnvironmentError` will be raised, with ``errno``
           set to :data:`~errno.EPERM`.  Unprivileged processes typically lack
           this capability.  You can check the capabilities of the current
           process with the python-prctl_ module:

           >>> import prctl
           >>> prctl.cap_effective.net_admin

        Raise :exc:`~exceptions.EnvironmentError`, if the buffer size could not
        bet set.

        .. versionadded:: 0.13

        .. _python-prctl: http://packages.python.org/python-prctl
        N)r�$udev_monitor_set_receive_buffer_size)r�sizes  r�set_receive_buffer_sizezMonitor.set_receive_buffer_sizes��2	
�
�
�:�:�4��Frc�:�		|jj|�}|rt|j|�SdS#t$rY}|j
t
jt
jfvrYd}~y|j
t
jk(rYd}~���d}~wwxYw)z�Receive a single device from the monitor.

        Return the received :class:`Device`, or ``None`` if no device could be
        received.

        N)	r�udev_monitor_receive_devicer	r
r!�errno�EAGAIN�EWOULDBLOCK�EINTR)r�device_p�errors   r�_receive_devicezMonitor._receive_devices|���
��=�=�D�D�T�J��9A�v�d�l�l�H�5�K�t�K��#�
��;�;�5�<�<��1B�1B�"C�C���[�[�E�K�K�/����
�s%�28�8�	B�,B�2B�B�Bc���|�|dkDrt|dz�}|j�ttjj|df�j|�r|j
�Sy)a�
        Poll for a device event.

        You can use this method together with :func:`iter()` to synchronously
        monitor events in the current thread::

           for device in iter(monitor.poll, None):
               print('{0.action} on {0.device_path}'.format(device))

        Since this method will never return ``None`` if no ``timeout`` is
        specified, this is effectively an endless loop. With
        :func:`functools.partial()` you can also create a loop that only waits
        for a specified time::

           for device in iter(partial(monitor.poll, 3), None):
               print('{0.action} on {0.device_path}'.format(device))

        This loop will only wait three seconds for a new device event. If no
        device event occurred after three seconds, the loop will exit.

        ``timeout`` is a floating point number that specifies a time-out in
        seconds. If omitted or ``None``, this method blocks until a device
        event is available. If ``0``, this method just polls and will never
        block.

        .. note::

           This method implicitly calls :meth:`start()`.

        Return the received :class:`Device`, or ``None`` if a timeout
        occurred. Raise :exc:`~exceptions.EnvironmentError` if event retrieval
        failed.

        .. seealso::

           :attr:`Device.action`
              The action that created this event.

           :attr:`Device.sequence_number`
              The sequence number of this event.

        .. versionadded:: 0.16
        Nri��r)�intr<rr�Poll�
for_eventsrO)r�timeouts  rrzMonitor.poll2s]��X��7�Q�;��'�D�.�)�G��
�
���D�I�I�0�0�$���=�B�B�G�L��'�'�)�)�rc�r�ddl}|jdt�|j�}|j|fS)a:
        Receive a single device from the monitor.

        .. warning::

           You *must* call :meth:`start()` before calling this method.

        The caller must make sure, that there are events available in the
        event queue.  The call blocks, until a device is available.

        If a device was available, return ``(action, device)``.  ``device``
        is the :class:`Device` object describing the device.  ``action`` is
        a string describing the action.  Usual actions are:

        ``'add'``
          A device has been added (e.g. a USB device was plugged in)
        ``'remove'``
          A device has been removed (e.g. a USB device was unplugged)
        ``'change'``
          Something about the device changed (e.g. a device property)
        ``'online'``
          The device is online now
        ``'offline'``
          The device is offline now

        Raise :exc:`~exceptions.EnvironmentError`, if no device could be
        read.

        .. deprecated:: 0.16
           Will be removed in 1.0. Use :meth:`Monitor.poll()` instead.
        rNz3Will be removed in 1.0. Use Monitor.poll() instead.)r9r:r;r�action�rr9�devices   r�receive_devicezMonitor.receive_devicefs6��B	��
�
�A�CU�	
�������}�}�f�$�$rc#�K�ddl}|jdt�|j�	|j	�}|�|j
|f���#�w)a�
        Wait for incoming events and receive them upon arrival.

        This methods implicitly calls :meth:`start()`, and starts polling the
        :meth:`fileno` of this monitor.  If a event comes in, it receives the
        corresponding device and yields it to the caller.

        The returned iterator is endless, and continues receiving devices
        without ever stopping.

        Yields ``(action, device)`` (see :meth:`receive_device` for a
        description).

        .. deprecated:: 0.16
           Will be removed in 1.0. Use an explicit loop over :meth:`poll()`
           instead, or monitor asynchronously with :class:`MonitorObserver`.
        rNzuWill be removed in 1.0. Use an explicit loop over "poll()" instead, or monitor asynchronously with "MonitorObserver".)r9r:r;r<rrWrXs   r�__iter__zMonitor.__iter__�sT����&	��
�
�
!�
�		
�	
�
�
����Y�Y�[�F��!��m�m�V�+�+��s�AA)rr)�__name__�
__module__�__qualname__�__doc__rr�classmethodr%�propertyr'r*r0r4r7r=r<rFrOrrZr\�rrrr&su��%�N�/�� %�� %�D����7�7�87�,7�(�0!�.G�6�*2�h'%�R,rrc�2�eZdZdZ	dd�Zd�Zd�Zd�Zd�Zy)	�MonitorObservera`
    An asynchronous observer for device events.

    This class subclasses :class:`~threading.Thread` class to asynchronously
    observe a :class:`Monitor` in a background thread:

    >>> from pyudev import Context, Monitor, MonitorObserver
    >>> context = Context()
    >>> monitor = Monitor.from_netlink(context)
    >>> monitor.filter_by(subsystem='input')
    >>> def print_device_event(device):
    ...     print('background event {0.action}: {0.device_path}'.format(device))
    >>> observer = MonitorObserver(monitor, callback=print_device_event, name='monitor-observer')
    >>> observer.daemon
    True
    >>> observer.start()

    In the above example, input device events will be printed in background,
    until :meth:`stop()` is called on ``observer``.

    .. note::

       Instances of this class are always created as daemon thread.  If you do
       not want to use daemon threads for monitoring, you need explicitly set
       :attr:`~threading.Thread.daemon` to ``False`` before invoking
       :meth:`~threading.Thread.start()`.

    .. seealso::

       :attr:`Device.action`
          The action that created this event.

       :attr:`Device.sequence_number`
          The sequence number of this event.

    .. versionadded:: 0.14

    .. versionchanged:: 0.15
       :meth:`Monitor.start()` is implicitly called when the thread is started.
    Nc����|�
��td��|�
��td��tj|g|��i|��||_d|_d|_��ddl}|jdt��fd�}||_	y)a
        Create a new observer for the given ``monitor``.

        ``monitor`` is the :class:`Monitor` to observe. ``callback`` is the
        callable to invoke on events, with the signature ``callback(device)``
        where ``device`` is the :class:`Device` that caused the event.

        .. warning::

           ``callback`` is invoked in the observer thread, hence the observer
           is blocked while callback executes.

        ``args`` and ``kwargs`` are passed unchanged to the constructor of
        :class:`~threading.Thread`.

        .. deprecated:: 0.16
           The ``event_handler`` argument will be removed in 1.0. Use
           the ``callback`` argument instead.
        .. versionchanged:: 0.16
           Add ``callback`` argument.
        Nzcallback missingz$Use either callback or event handlerTrzL"event_handler" argument will be removed in 1.0. Use Monitor.poll() instead.c�*���|j|�Sr)rW)�d�
event_handlers �r�<lambda>z*MonitorObserver.__init__.<locals>.<lambda>s����q�x�x��!;�r)
rrrr$�daemon�_stop_eventr9r:r;�	_callback)rr$ri�callback�args�kwargsr9s  `    rrzMonitorObserver.__init__�s����0��
� 5��/�0�0�
�
!�m�&?��C�D�D�����.�t�.�v�.�����������$���M�M�.�"�
�
<�H�!��rc��|j�s#tjj�|_tj|�y)zStart the observer thread.N)�is_aliver�Pipe�openrlrr<rs rr<zMonitorObserver.start	s*���}�}��#�y�y�~�~�/�D�����T�rc�|�|jj�tjj	|jdf|j
jdf�}	t|j�D]�\}}||j
jj�k(r&|j
jj�y||jj�k(rN|dk(rItt|jjd��}t|d�D]}|j|����td����)NrQr)rUzObserved monitor hung up)r$r<rrSrTrlr#rr*�closer�iterrmr!)r�notifier�file_descriptor�event�read_devicerYs      r�runzMonitorObserver.runs���������9�9�'�'�
�\�\�3��$�"2�"2�"9�"9�3�!?�
���*:�8�=�=�*I�

G�&���"�d�&6�&6�&=�&=�&D�&D�&F�F��$�$�+�+�1�1�3��$����(;�(;�(=�=�%�3�,�")�(�$�,�,�*;�*;�Q�#�K�#'�{�D�"9�/�����v�.�/�+�+E�F�F�

G�rc��|j�y|jj5t|jjjd�|jjj	�ddd�y#1swYyxYw)aT
        Send a stop signal to the background thread.

        The background thread will eventually exit, but it may still be running
        when this method returns.  This method is essentially the asynchronous
        equivalent to :meth:`stop()`.

        .. note::

           The underlying :attr:`monitor` is *not* stopped.
        N�)rl�sinkr�write�flushrs r�	send_stopzMonitorObserver.send_stop$sk�����#��
�
�
�
"�
"�	*��T�-�-�2�2�8�8�'�B����!�!�'�'�)�	*�	*�	*�s�AA<�<Bc�d�|j�	|j�y#t$rYywxYw)a
        Synchronously stop the background thread.

        .. note::

           This method can safely be called from the observer thread. In this
           case it is equivalent to :meth:`send_stop()`.

        Send a stop signal to the backgroud (see :meth:`send_stop`), and waits
        for the background thread to exit (see :meth:`~threading.Thread.join`)
        if the current thread is *not* the observer thread.

        After this method returns in a thread *that is not the observer
        thread*, the ``callback`` is guaranteed to not be invoked again
        anymore.

        .. note::

           The underlying :attr:`monitor` is *not* stopped.

        .. versionchanged:: 0.16
           This method can be called from the observer thread.
        N)r��join�RuntimeErrorrs r�stopzMonitorObserver.stop7s.��0	
����	��I�I�K���	��	�s�#�	/�/)NN)	r]r^r_r`rr<r|r�r�rcrrrere�s(��'�T59�,"�\�G�**�&rre)r`rIrA�	functoolsr�	threadingr�
pyudev._osrr�pyudev._utilrr�
pyudev.devicer	�objectrrercrr�<module>r�s@��"�
�	���"�=� �H,�f�H,�Vb�f�br

Zerion Mini Shell 1.0