File: //lib/python3.12/site-packages/pyudev/device/__pycache__/_device.cpython-312.opt-1.pyc
�
9*�b
� � � � d Z ddlZddlZddlZddlZddlmZ ddlmZm Z m
Z
mZmZm
Z
mZ ddlmZmZmZmZmZ G d� de� Z G d� d ej. j0 � Z G d
� dej. j0 � Z G d� d
e� Z G d� dej. j8 ej. j: � Zy)z�
pyudev.device._device
=====================
Device class implementation of :mod:`pyudev`.
.. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
� N)� timedelta)�DeviceNotFoundAtPathError�DeviceNotFoundByFileError�#DeviceNotFoundByInterfaceIndexError�!DeviceNotFoundByKernelDeviceError�DeviceNotFoundByNameError�DeviceNotFoundByNumberError� DeviceNotFoundInEnvironmentError)�ensure_byte_string�ensure_unicode_string�get_device_type�string_to_bool�udev_list_iteratec � � e Zd ZdZed� � Zed� � Zed� � Zed� � Zed� � Z ed� � Z
ed� � Zed � � Zed
� � Z
y)�DeviceszT
Class for constructing :class:`Device` objects from various kinds of data.
c �� � |j |j � sGt j j |j |j t j � � }| j ||� S )a�
Create a device from a device ``path``. The ``path`` may or may not
start with the ``sysfs`` mount point:
>>> from pyudev import Context, Device
>>> context = Context()
>>> Devices.from_path(context, '/devices/platform')
Device(u'/sys/devices/platform')
>>> Devices.from_path(context, '/sys/devices/platform')
Device(u'/sys/devices/platform')
``context`` is the :class:`Context` in which to search the device.
``path`` is a device path as unicode or byte string.
Return a :class:`Device` object for the device. Raise
:exc:`DeviceNotFoundAtPathError`, if no device was found for ``path``.
.. versionadded:: 0.18
)�
startswith�sys_path�os�path�join�lstrip�sep�
from_sys_path)�cls�contextr s �:/usr/lib/python3.12/site-packages/pyudev/device/_device.py� from_pathzDevices.from_path; sP � �* ���w�/�/�0��7�7�<�<�� 0� 0�$�+�+�b�f�f�2E�F�D�� � ��$�/�/� c �~ � |j j |t |� � }|st |� �t ||� S )a�
Create a new device from a given ``sys_path``:
>>> from pyudev import Context, Device
>>> context = Context()
>>> Devices.from_sys_path(context, '/sys/devices/platform')
Device(u'/sys/devices/platform')
``context`` is the :class:`Context` in which to search the device.
``sys_path`` is a unicode or byte string containing the path of the
device inside ``sysfs`` with the mount point included.
Return a :class:`Device` object for the device. Raise
:exc:`DeviceNotFoundAtPathError`, if no device was found for
``sys_path``.
.. versionadded:: 0.18
)�_libudev�udev_device_new_from_syspathr r �Device)r r r �devices r r zDevices.from_sys_pathT sB � �( �!�!�>�>��'��1�
�� �+�H�5�5��g�v�&�&r c � � |j dd� }|j j |t |� t |� � }|st ||� �t ||� S )a.
Create a new device from a given ``subsystem`` and a given
``sys_name``:
>>> from pyudev import Context, Device
>>> context = Context()
>>> sda = Devices.from_name(context, 'block', 'sda')
>>> sda
Device(u'/sys/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda')
>>> sda == Devices.from_path(context, '/block/sda')
``context`` is the :class:`Context` in which to search the device.
``subsystem`` and ``sys_name`` are byte or unicode strings, which
denote the subsystem and the name of the device to create.
Return a :class:`Device` object for the device. Raise
:exc:`DeviceNotFoundByNameError`, if no device was found with the given
name.
.. versionadded:: 0.18
�/�!)�replacer! �&udev_device_new_from_subsystem_sysnamer r r# )r r � subsystem�sys_namer$ s r � from_namezDevices.from_nameo s^ � �. �#�#�C��-���!�!�H�H��'� �2�4F�x�4P�
�� �+�I�x�@�@��g�v�&�&r c � � |j j |t |d � |� }|st ||� �t ||� S )a�
Create a new device from a device ``number`` with the given device
``type``:
>>> import os
>>> from pyudev import Context, Device
>>> ctx = Context()
>>> major, minor = 8, 0
>>> device = Devices.from_device_number(context, 'block',
... os.makedev(major, minor))
>>> device
Device(u'/sys/devices/pci0000:00/0000:00:11.0/host0/target0:0:0/0:0:0:0/block/sda')
>>> os.major(device.device_number), os.minor(device.device_number)
(8, 0)
Use :func:`os.makedev` to construct a device number from a major and a
minor device number, as shown in the example above.
.. warning::
Device numbers are not unique across different device types.
Passing a correct number with a wrong type may silently yield a
wrong device object, so make sure to pass the correct device type.
``context`` is the :class:`Context`, in which to search the device.
``type`` is either ``'char'`` or ``'block'``, according to whether the
device is a character or block device. ``number`` is the device number
as integer.
Return a :class:`Device` object for the device with the given device
``number``. Raise :exc:`DeviceNotFoundByNumberError`, if no device was
found with the given device type and number.
.. versionadded:: 0.18
r )r! �udev_device_new_from_devnumr r r# )r r �typ�numberr$ s r �from_device_numberzDevices.from_device_number� sK � �J �!�!�=�=��'��A��/��
�� �-�c�6�:�:��g�v�&�&r c �� � t |� }t j |� j }| j |||� S # t t
f$ r}t
|� �d}~ww xY w)a�
Create a new device from the given device file:
>>> from pyudev import Context, Device
>>> context = Context()
>>> device = Devices.from_device_file(context, '/dev/sda')
>>> device
Device(u'/sys/devices/pci0000:00/0000:00:0d.0/host2/target2:0:0/2:0:0:0/block/sda')
>>> device.device_node
u'/dev/sda'
.. warning::
Though the example seems to suggest that ``device.device_node ==
filename`` holds with ``device = Devices.from_device_file(context,
filename)``, this is only true in a majority of cases. There *can*
be devices, for which this relation is actually false! Thus, do
*not* expect :attr:`~Device.device_node` to be equal to the given
``filename`` for the returned :class:`Device`. Especially, use
:attr:`~Device.device_node` if you need the device file of a
:class:`Device` created with this method afterwards.
``context`` is the :class:`Context` in which to search the device.
``filename`` is a string containing the path of a device file.
Return a :class:`Device` representing the given device file. Raise
:exc:`DeviceNotFoundByFileError` if ``filename`` is no device file
at all or if ``filename`` does not exist or if its metadata was
inaccessible.
.. versionadded:: 0.18
N)r
r �stat�st_rdev�EnvironmentError�
ValueErrorr r1 )r r �filename�device_type�
device_number�errs r �from_device_filezDevices.from_device_file� s_ � �D 1�)�(�3�K��G�G�H�-�5�5�M� �%�%�g�{�M�J�J�� !�*�-� 1�+�C�0�0�� 1�s �*? �A�A�Ac �p �� |j d�� }t �fd�|D � d� }|�|S t �� �)a?
Locate a device based on the interface index.
:param `Context` context: the libudev context
:param int ifindex: the interface index
:returns: the device corresponding to the interface index
:rtype: `Device`
This method is only appropriate for network devices.
�net)r* c 3 �` �K � | ]% }|j j d � �k( s�"|�� �' y�w)�ifindexN)�
attributes�get)�.0�dr? s �r � <genexpr>z/Devices.from_interface_index.<locals>.<genexpr>� s( �� �� �R��1�1�<�<�+;�+;�I�+F�'�+Q�Q��s �#.�.N)�list_devices�nextr )r r r? �network_devices�devs ` r �from_interface_indexzDevices.from_interface_index� sD �� � "�.�.��.�?���R��R�TX�
�� �?��J�5�g�>�>r c �� � |d }|dd }|dv r�t j d� }|j |� }|rYt j t |j
d� � t |j
d� � � }| j |||� S t |� �|dk( r| j ||� S |d k( r7|j d
� \ }} }
|
r|r| j |||
� S t |� �t |� �)a
Locate a device based on the kernel device.
:param `Context` context: the libudev context
:param str kernel_device: the kernel device
:returns: the device corresponding to ``kernel_device``
:rtype: `Device`
r � N)�b�cz^(?P<major>\d+):(?P<minor>\d+)$�major�minor�n�+�:)�re�compile�matchr �makedev�int�groupr1 r rI � partitionr, )r r �
kernel_device�switch_char�rest� number_rerU r0 r* �_�kernel_device_names r �from_kernel_devicezDevices.from_kernel_device� s� � � $�A�&���Q�R� ���*�$��
�
�#E�F�I��O�O�D�)�E���������G�,�-�s�5�;�;�w�3G�/H��� �-�-�g�{�F�K�K�7�
�F�F�
�C�
��+�+�G�T�:�:�
�C�
�15����1D�.�Y��-�!�i��}�}�W�i�9K�L�L�7�
�F�F�3�M�B�Br c �h � |j j |� }|s
t � �t ||� S )a�
Create a new device from the process environment (as in
:data:`os.environ`).
This only works reliable, if the current process is called from an
udev rule, and is usually used for tools executed from ``IMPORT=``
rules. Use this method to create device objects in Python scripts
called from udev rules.
``context`` is the library :class:`Context`.
Return a :class:`Device` object constructed from the environment.
Raise :exc:`DeviceNotFoundInEnvironmentError`, if no device could be
created from the environment.
.. udevversion:: 152
.. versionadded:: 0.18
)r! � udev_device_new_from_environmentr
r# )r r r$ s r �from_environmentzDevices.from_environment s3 � �* �!�!�B�B�7�K���2�4�4��g�v�&�&r c �t � | j | j | j | j | j gS )z�
Return methods that obtain a :class:`Device` from a variety of
different data.
:return: a list of from_* methods.
:rtype: list of class methods
.. versionadded:: 0.18
)r; r1 r, r r )r s r �METHODSzDevices.METHODS4 s7 � �
� � ��"�"��M�M��M�M����
�
r N)�__name__�
__module__�__qualname__�__doc__�classmethodr r r, r1 r; rI r` rc re � r r r r 6 s� � �� �0� �0�0 �'� �'�4 �'� �'�<