3 u1‘WÉ­ã@sZdZddlmZddlmZddlmZddlmZddlZddlZddlm Z ddlm Z dd lm Z dd l m Z dd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZGdd„deƒZGdd„de ƒZGdd„de ƒZGdd„deƒZ Gdd „d e e ƒZ!dS)!z© pyudev.device._device ===================== Device class implementation of :mod:`pyudev`. .. moduleauthor:: Sebastian Wiesner é)Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Ú Container)ÚIterable)ÚMapping)Ú timedelta)ÚDeviceNotFoundAtPathError)ÚDeviceNotFoundByFileError)Ú#DeviceNotFoundByInterfaceIndexError)Ú!DeviceNotFoundByKernelDeviceError)ÚDeviceNotFoundByNameError)ÚDeviceNotFoundByNumberError)Ú DeviceNotFoundInEnvironmentError)Úensure_byte_string)Úensure_unicode_string)Úget_device_type)Ústring_to_bool)Úudev_list_iteratec@s|eZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ ed d „ƒZ edd„ƒZ edd„ƒZ edd„ƒZ dS)ÚDeviceszT Class for constructing :class:`Device` objects from various kinds of data. cCs0|j|jƒs$tjj|j|jtjƒƒ}|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©r!ú/usr/lib/python3.6/_device.pyÚ from_path<s zDevices.from_pathcCs(|jj|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 )Ú_libudevZudev_device_new_from_syspathrr ÚDevice)rr rÚdevicer!r!r"rUs  zDevices.from_sys_pathcCs<|jddƒ}|jj|t|ƒt|ƒƒ}|s2t||ƒ‚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$Z&udev_device_new_from_subsystem_sysnamerrr%)rr Ú subsystemÚsys_namer&r!r!r"Ú from_nameos   zDevices.from_namecCs0|jj|t|dƒ|ƒ}|s&t||ƒ‚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$Zudev_device_new_from_devnumrrr%)rr ÚtypÚnumberr&r!r!r"Úfrom_device_numberŽs % zDevices.from_device_numbercCsVyt|ƒ}tj|ƒj}Wn.ttfk rF}zt|ƒ‚WYdd}~XnX|j|||ƒS)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)rrÚstatÚst_rdevÚEnvironmentErrorÚ ValueErrorr r/)rr ÚfilenameÚ device_typeÚ device_numberÚerrr!r!r"Úfrom_device_file¹s "zDevices.from_device_filecs<|jdd�}t‡fdd„|Dƒdƒ}|dk r0|Stˆƒ‚dS)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. Znet)r*c3s"|]}|jjdƒˆkr|VqdS)ÚifindexN)Ú attributesÚget)Ú.0Úd)r9r!r"ú òsz/Devices.from_interface_index..N)Ú list_devicesÚnextr )rr r9Znetwork_devicesZdevr!)r9r"Úfrom_interface_indexäs zDevices.from_interface_indexc CsÄ|d}|dd…}|d krltjdƒ}|j|ƒ}|rbtjt|jdƒƒt|jdƒƒƒ}|j|||ƒSt|ƒ‚nT|d kr€|j ||ƒS|d kr¸|j d ƒ\}} } | r®|r®|j ||| ƒSt|ƒ‚nt|ƒ‚dS) 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\d+):(?P\d+)$ÚmajorÚminorÚnú+ú:)rCrD) ÚreÚcompileÚmatchrÚmakedevÚintÚgroupr/r rAÚ partitionr,) rr Z kernel_deviceZ switch_charÚrestZ number_rerLr.r*Ú_Zkernel_device_namer!r!r"Úfrom_kernel_deviceüs&        zDevices.from_kernel_devicecCs |jj|ƒ}|stƒ‚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$Z udev_device_new_from_environmentrr%)rr r&r!r!r"Úfrom_environments zDevices.from_environmentcCs|j|j|j|j|jgS)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 )r8r/r,r#r)rr!r!r"ÚMETHODS9s zDevices.METHODSN)Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú classmethodr#rr,r/r8rArSrTrUr!r!r!r"r7s    + +  # rc@sÒeZdZdZedd„ƒZedd„ƒZedd„ƒZedd „ƒZed d „ƒZ ed d „ƒZ dd„Z dd„Z dd„Z edd„ƒZedd„ƒZedd„ƒZdYdd„Zdd„Zedd „ƒZed!d"„ƒZed#d$„ƒZed%d&„ƒZed'd(„ƒZed)d*„ƒZed+d,„ƒZed-d.„ƒZed/d0„ƒZed1d2„ƒZed3d4„ƒZed5d6„ƒZed7d8„ƒZ ed9d:„ƒZ!ed;d<„ƒZ"ed=d>„ƒZ#ed?d@„ƒZ$dAdB„Z%dCdD„Z&dEdF„Z'dGdH„Z(dIdJ„Z)dKdL„Z*dMdN„Z+dOdP„Z,dQdR„Z-dSdT„Z.dUdV„Z/dWdX„Z0dS)Zr%aß A single device with attached attributes and properties. This class subclasses the ``Mapping`` ABC, providing a read-only dictionary mapping property names to the corresponding values. Therefore all well-known dicitionary methods and operators (e.g. ``.keys()``, ``.items()``, ``in``) are available to access device properties. Aside of the properties, a device also has a set of udev-specific attributes like the path inside ``sysfs``. :class:`Device` objects compare equal and unequal to other devices and to strings (based on :attr:`device_path`). However, there is no ordering on :class:`Device` objects, and the corresponding operators ``>``, ``<``, ``<=`` and ``>=`` raise :exc:`~exceptions.TypeError`. .. warning:: **Never** use object identity (``is`` operator) to compare :class:`Device` objects. :mod:`pyudev` may create multiple :class:`Device` objects for the same device. Instead compare devices by value using ``==`` or ``!=``. :class:`Device` objects are hashable and can therefore be used as keys in dictionaries and sets. They can also be given directly as ``udev_device *`` to functions wrapped through :mod:`ctypes`. cCs$ddl}|jdtdd�tj||ƒS)zw .. versionadded:: 0.4 .. deprecated:: 0.18 Use :class:`Devices.from_path` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.é)Ú stacklevel)ÚwarningsÚwarnÚDeprecationWarningrr#)rr rr]r!r!r"r#ns zDevice.from_pathcCs$ddl}|jdtdd�tj||ƒS)a| .. versionchanged:: 0.4 Raise :exc:`NoSuchDeviceError` instead of returning ``None``, if no device was found for ``sys_path``. .. versionchanged:: 0.5 Raise :exc:`DeviceNotFoundAtPathError` instead of :exc:`NoSuchDeviceError`. .. deprecated:: 0.18 Use :class:`Devices.from_sys_path` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.r[)r\)r]r^r_rr)rr rr]r!r!r"r}s zDevice.from_sys_pathcCs&ddl}|jdtdd�tj|||ƒS)zw .. versionadded:: 0.5 .. deprecated:: 0.18 Use :class:`Devices.from_name` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.r[)r\)r]r^r_rr,)rr r*r+r]r!r!r"r,‘s zDevice.from_namecCs&ddl}|jdtdd�tj|||ƒS)z� .. versionadded:: 0.11 .. deprecated:: 0.18 Use :class:`Devices.from_device_number` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.r[)r\)r]r^r_rr/)rr r-r.r]r!r!r"r/ s zDevice.from_device_numbercCs$ddl}|jdtdd�tj||ƒS)z .. versionadded:: 0.15 .. deprecated:: 0.18 Use :class:`Devices.from_device_file` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.r[)r\)r]r^r_rr8)rr r4r]r!r!r"r8¯s zDevice.from_device_filecCs"ddl}|jdtdd�tj|ƒS)z~ .. versionadded:: 0.6 .. deprecated:: 0.18 Use :class:`Devices.from_environment` instead. rNz>Will be removed in 1.0. Use equivalent Devices method instead.r[)r\)r]r^r_rrT)rr r]r!r!r"rT¾s zDevice.from_environmentcCs||_||_|j|_dS)N)r Z_as_parameter_r$)Úselfr Z_devicer!r!r"Ú__init__ÍszDevice.__init__cCs|jj|ƒdS)N)r$Zudev_device_unref)r`r!r!r"Ú__del__ÒszDevice.__del__cCs dj|ƒS)NzDevice({0.sys_path!r}))Úformat)r`r!r!r"Ú__repr__ÕszDevice.__repr__cCs(|jj|ƒ}|sdSt|j|jj|ƒƒS)z_ The parent :class:`Device` or ``None``, if there is no parent device. N)r$Zudev_device_get_parentr%r Úudev_device_ref)r`Úparentr!r!r"rfØs z Device.parentccs,x&|jjƒj|ƒD]}||kr|VqWdS)a” Yield all direct children of this device. .. note:: In udev, parent-child relationships are generally ambiguous, i.e. a parent can have multiple children, *and* a child can have multiple parents. Hence, `child.parent == parent` does generally *not* hold for all `child` objects in `parent.children`. In other words, the :attr:`parent` of a device in this property can be different from this device! .. note:: As the underlying library does not provide any means to directly query the children of a device, this property performs a linear search through all devices. Return an iterable yielding a :class:`Device` object for each direct child of this device. .. udevversion:: 172 .. versionchanged:: 0.13 Requires udev version 172 now. N)r r?Z match_parent)r`r&r!r!r"ÚchildrenåszDevice.childrenccs$|j}x|dk r|V|j}qWdS)zã Yield all ancestors of this device from bottom to top. Return an iterator yielding a :class:`Device` object for each ancestor of this device from bottom to top. .. versionadded:: 0.16 N)rf)r`rfr!r!r"Ú ancestorss  zDevice.ancestorsNcCsDt|ƒ}|dk rt|ƒ}|jj|||ƒ}|s0dSt|j|jj|ƒƒS)aÉ Find the parent device with the given ``subsystem`` and ``device_type``. ``subsystem`` is a byte or unicode string containing the name of the subsystem, in which to search for the parent. ``device_type`` is a byte or unicode string holding the expected device type of the parent. It can be ``None`` (the default), which means, that no specific device type is expected. Return a parent :class:`Device` within the given ``subsystem`` and, if ``device_type`` is not ``None``, with the given ``device_type``, or ``None``, if this device has no parent device matching these constraints. .. versionadded:: 0.9 N)rr$Z-udev_device_get_parent_with_subsystem_devtyper%r re)r`r*r5rfr!r!r"Ú find_parents zDevice.find_parentcCsddl}|jdtdd�|jS)a~ Traverse all parent devices of this device from bottom to top. Return an iterable yielding all parent devices as :class:`Device` objects, *not* including the current device. The last yielded :class:`Device` is the top of the device hierarchy. .. deprecated:: 0.16 Will be removed in 1.0. Use :attr:`ancestors` instead. rNz5Will be removed in 1.0. Use Device.ancestors instead.r[)r\)r]r^r_rh)r`r]r!r!r"Útraverse0s zDevice.traversecCst|jj|ƒƒS)zz Absolute path of this device in ``sysfs`` including the ``sysfs`` mount point as unicode string. )rr$Zudev_device_get_syspath)r`r!r!r"rCszDevice.sys_pathcCst|jj|ƒƒS)a Kernel device path as unicode string. This path uniquely identifies a single device. Unlike :attr:`sys_path`, this path does not contain the ``sysfs`` mount point. However, the path is absolute and starts with a slash ``'/'``. )rr$Zudev_device_get_devpath)r`r!r!r"Ú device_pathLs zDevice.device_pathcCs |jj|ƒ}|dkrdSt|ƒS)zµ Name of the subsystem this device is part of as unicode string. :returns: name of subsystem if found, else None :rtype: unicode string or NoneType N)r$Zudev_device_get_subsystemr)r`Zsubsysr!r!r"r*Ys zDevice.subsystemcCst|jj|ƒƒS)zF Device file name inside ``sysfs`` as unicode string. )rr$Zudev_device_get_sysname)r`r!r!r"r+dszDevice.sys_namecCs |jj|ƒ}|dk rt|ƒSdS)a¢ The trailing number of the :attr:`sys_name` as unicode string, or ``None``, if the device has no trailing number in its name. .. note:: The number is returned as unicode string to preserve the exact format of the number, especially any leading zeros: >>> from pyudev import Context, Device >>> context = Context() >>> device = Devices.from_path(context, '/sys/devices/LNXSYSTM:00') >>> device.sys_number u'00' To work with numbers, explicitly convert them to ints: >>> int(device.sys_number) 0 .. versionadded:: 0.11 N)r$Zudev_device_get_sysnumr)r`r.r!r!r"Ú sys_numberls zDevice.sys_numbercCs$|jj|ƒ}|dk rt|ƒS|SdS)aÚ Device type as unicode string, or ``None``, if the device type is unknown. >>> from pyudev import Context >>> context = Context() >>> for device in context.list_devices(subsystem='net'): ... '{0} - {1}'.format(device.sys_name, device.device_type or 'ethernet') ... u'eth0 - ethernet' u'wlan0 - wlan' u'lo - ethernet' u'vboxnet0 - ethernet' .. versionadded:: 0.10 N)r$Zudev_device_get_devtyper)r`r5r!r!r"r5‡s zDevice.device_typecCs |jj|ƒ}|dk rt|ƒSdS)z� The driver name as unicode string, or ``None``, if there is no driver for this device. .. versionadded:: 0.5 N)r$Zudev_device_get_driverr)r`Údriverr!r!r"rmŸs z Device.drivercCs |jj|ƒ}|dk rt|ƒSdS)a± Absolute path to the device node of this device as unicode string or ``None``, if this device doesn't have a device node. The path includes the device directory (see :attr:`Context.device_path`). This path always points to the actual device node associated with this device, and never to any symbolic links to this device node. See :attr:`device_links` to get a list of symbolic links to this device node. .. warning:: For devices created with :meth:`from_device_file()`, the value of this property is not necessary equal to the ``filename`` given to :meth:`from_device_file()`. N)r$Zudev_device_get_devnoder)r`Znoder!r!r"Ú device_nodeªs zDevice.device_nodecCs |jj|ƒS)a The device number of the associated device as integer, or ``0``, if no device number is associated. Use :func:`os.major` and :func:`os.minor` to decompose the device number into its major and minor number: >>> import os >>> from pyudev import Context, Device >>> context = Context() >>> sda = Devices.from_name(context, 'block', 'sda') >>> sda.device_number 2048L >>> (os.major(sda.device_number), os.minor(sda.device_number)) (8, 0) For devices with an associated :attr:`device_node`, this is the same as the ``st_rdev`` field of the stat result of the :attr:`device_node`: >>> os.stat(sda.device_node).st_rdev 2048 .. versionadded:: 0.11 )r$Zudev_device_get_devnum)r`r!r!r"r6¿szDevice.device_numbercCst|jj|ƒƒS)ai ``True``, if the device is initialized, ``False`` otherwise. A device is initialized, if udev has already handled this device and has set up device node permissions and context, or renamed a network device. Consequently, this property is only implemented for devices with a device node or for network devices. On all other devices this property is always ``True``. It is *not* recommended, that you use uninitialized devices. .. seealso:: :attr:`time_since_initialized` .. udevversion:: 165 .. versionadded:: 0.8 )Úboolr$Zudev_device_get_is_initialized)r`r!r!r"Úis_initializedÛszDevice.is_initializedcCs|jj|ƒ}t|d�S)a� The time elapsed since initialization as :class:`~datetime.timedelta`. This property is only implemented on devices, which need to store properties in the udev database. On all other devices this property is simply zero :class:`~datetime.timedelta`. .. seealso:: :attr:`is_initialized` .. udevversion:: 165 .. versionadded:: 0.8 )Ú microseconds)r$Z&udev_device_get_usec_since_initializedr )r`rqr!r!r"Útime_since_initializedòszDevice.time_since_initializedccs4|jj|ƒ}x"t|j|ƒD]\}}t|ƒVqWdS)aü An iterator, which yields the absolute paths (including the device directory, see :attr:`Context.device_path`) of all symbolic links pointing to the :attr:`device_node` of this device. The paths are unicode strings. UDev can create symlinks to the original device node (see :attr:`device_node`) inside the device directory. This is often used to assign a constant, fixed device node to devices like removeable media, which technically do not have a constant device node, or to map a single device into multiple device hierarchies. The property provides access to all such symbolic links, which were created by UDev for this device. .. warning:: Links are not necessarily resolved by :meth:`Devices.from_device_file()`. Hence do *not* rely on ``Devices.from_device_file(context, link).device_path == device.device_path`` from any ``link`` in ``device.device_links``. N)r$Z#udev_device_get_devlinks_list_entryrr)r`ZdevlinksÚnamerRr!r!r"Ú device_linkss zDevice.device_linkscCs |jj|ƒ}|dk rt|ƒSdS)a The device event action as string, or ``None``, if this device was not received from a :class:`Monitor`. 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 .. warning:: Though the actions listed above are the most common, this property *may* return other values, too, so be prepared to handle unknown actions! .. versionadded:: 0.16 N)r$Zudev_device_get_actionr)r`Úactionr!r!r"ru s z Device.actioncCs |jj|ƒS)zÅ The device event sequence number as integer, or ``0`` if this device has no sequence number, i.e. was not received from a :class:`Monitor`. .. versionadded:: 0.16 )r$Zudev_device_get_seqnum)r`r!r!r"Úsequence_number>szDevice.sequence_numbercCst|ƒS)aT The system attributes of this device as read-only :class:`Attributes` mapping. System attributes are basically normal files inside the the device directory. These files contain all sorts of information about the device, which may not be reflected by properties. These attributes are commonly used for matching in udev rules, and can be printed using ``udevadm info --attribute-walk``. The values of these attributes are not always proper strings, and can contain arbitrary bytes. .. versionadded:: 0.5 )Ú Attributes)r`r!r!r"r:HszDevice.attributescCst|ƒS)zu The udev properties of this object as read-only Properties mapping. .. versionadded:: 0.21 )Ú Properties)r`r!r!r"Ú properties_szDevice.propertiescCst|ƒS)a  A :class:`Tags` object representing the tags attached to this device. The :class:`Tags` object supports a test for a single tag as well as iteration over all tags: >>> from pyudev import Context >>> context = Context() >>> device = next(iter(context.list_devices(tag='systemd'))) >>> 'systemd' in device.tags True >>> list(device.tags) [u'seat', u'systemd', u'uaccess'] Tags are arbitrary classifiers that can be attached to devices by udev scripts and daemons. For instance, systemd_ uses tags for multi-seat_ support. .. _systemd: http://freedesktop.org/wiki/Software/systemd .. _multi-seat: http://www.freedesktop.org/wiki/Software/systemd/multiseat .. udevversion:: 154 .. versionadded:: 0.6 .. versionchanged:: 0.13 Return a :class:`Tags` object now. )ÚTags)r`r!r!r"Útagshsz Device.tagscCs"ddl}|jdtdd�|jjƒS)a* Iterate over the names of all properties defined for this device. Return a generator yielding the names of all properties of this device as unicode strings. .. deprecated:: 0.21 Will be removed in 1.0. Access properties with Device.properties. rNzAWill be removed in 1.0. Access properties with Device.properties.r[)r\)r]r^r_ryÚ__iter__)r`r]r!r!r"r|ˆs zDevice.__iter__cCs"ddl}|jdtdd�|jjƒS)zÀ Return the amount of properties defined for this device as integer. .. deprecated:: 0.21 Will be removed in 1.0. Access properties with Device.properties. rNzAWill be removed in 1.0. Access properties with Device.properties.r[)r\)r]r^r_ryÚ__len__)r`r]r!r!r"r}šs zDevice.__len__cCs$ddl}|jdtdd�|jj|ƒS)a¤ Get the given property from this device. ``prop`` is a unicode or byte string containing the name of the property. Return the property value as unicode string, or raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device. .. deprecated:: 0.21 Will be removed in 1.0. Access properties with Device.properties. rNzAWill be removed in 1.0. Access properties with Device.properties.r[)r\)r]r^r_ryÚ __getitem__)r`Úpropr]r!r!r"r~©s zDevice.__getitem__cCs$ddl}|jdtdd�|jj|ƒS)a Get the given property from this device as integer. ``prop`` is a unicode or byte string containing the name of the property. Return the property value as integer. Raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device, or a :exc:`~exceptions.ValueError`, if the property value cannot be converted to an integer. .. deprecated:: 0.21 Will be removed in 1.0. Use Device.properties.asint() instead. rNz*sz%Properties.__len__..)r$rŽr&Úsumr)r`ryr!r!r"r}$szProperties.__len__cCs,|jj|jt|ƒƒ}|dkr$t|ƒ‚t|ƒS)a9 Get the given property from this device. ``prop`` is a unicode or byte string containing the name of the property. Return the property value as unicode string, or raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device. N)r$Zudev_device_get_property_valuer&rÚKeyErrorr)r`rÚvaluer!r!r"r~,s  zProperties.__getitem__cCs t||ƒS)aŸ Get the given property from this device as integer. ``prop`` is a unicode or byte string containing the name of the property. Return the property value as integer. Raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device, or a :exc:`~exceptions.ValueError`, if the property value cannot be converted to an integer. )rN)r`rr!r!r"r€?s zProperties.asintcCs t||ƒS)a– Get the given property from this device as boolean. A boolean property has either a value of ``'1'`` or of ``'0'``, where ``'1'`` stands for ``True``, and ``'0'`` for ``False``. Any other value causes a :exc:`~exceptions.ValueError` to be raised. ``prop`` is a unicode or byte string containing the name of the property. Return ``True``, if the property value is ``'1'`` and ``False``, if the property value is ``'0'``. Any other value raises a :exc:`~exceptions.ValueError`. Raise a :exc:`~exceptions.KeyError`, if the given property is not defined for this device. )r)r`rr!r!r"r�MszProperties.asboolN) rVrWrXrYrar|r}r~r€r�r!r!r!r"rx s rxc@sNeZdZdZdd„Zedd„ƒZdd„Zdd d „Zd d „Z d d„Z dd„Z dS)rwzQ udev attributes for :class:`Device` objects. .. versionadded:: 0.5 cCs||_|j|_dS)N)r&r$)r`r&r!r!r"ragszAttributes.__init__ccsFt|jdƒsdS|jj|jƒ}x"t|j|ƒD]\}}t|ƒVq,WdS)aà Yield the ``available`` attributes for the device. It is not guaranteed that a key in this list will have a value. It is not guaranteed that a key not in this list will not have a value. It is guaranteed that the keys in this list are the keys that libudev considers to be "available" attributes. If libudev version does not define udev_device_get_sysattr_list_entry() yields nothing. See rhbz#1267584. Ú"udev_device_get_sysattr_list_entryN)Úhasattrr$r’r&rr)r`ÚattrsÚ attributerRr!r!r"Úavailable_attributesks  zAttributes.available_attributescCs(|jj|jt|ƒƒ}|dkr$t|ƒ‚|S)aD Get the given system ``attribute`` for the device. :param attribute: the key for an attribute value :type attribute: unicode or byte string :returns: the value corresponding to ``attribute`` :rtype: an arbitrary sequence of bytes :raises KeyError: if no value found N)r$Zudev_device_get_sysattr_valuer&rr�)r`r•r‘r!r!r"Ú_get�s  zAttributes._getNc Cs$y |j|ƒStk r|SXdS)a| Get the given system ``attribute`` for the device. :param attribute: the key for an attribute value :type attribute: unicode or byte string :param default: a default if no corresponding value found :type default: a sequence of bytes :returns: the value corresponding to ``attribute`` or ``default`` :rtype: object N)r—r�)r`r•Údefaultr!r!r"r;“s  zAttributes.getcCst|j|ƒƒS)a– Get the given ``attribute`` for the device as unicode string. :param attribute: the key for an attribute value :type attribute: unicode or byte string :returns: the value corresponding to ``attribute``, as unicode :rtype: unicode :raises KeyError: if no value found for ``attribute`` :raises UnicodeDecodeError: if value is not convertible )rr—)r`r•r!r!r"Úasstring£s zAttributes.asstringcCst|j|ƒƒS)aÑ Get the given ``attribute`` as an int. :param attribute: the key for an attribute value :type attribute: unicode or byte string :returns: the value corresponding to ``attribute``, as an int :rtype: int :raises KeyError: if no value found for ``attribute`` :raises UnicodeDecodeError: if value is not convertible to unicode :raises ValueError: if unicode value can not be converted to an int )rNr™)r`r•r!r!r"r€°s zAttributes.asintcCst|j|ƒƒS)a¿ Get the given ``attribute`` from this device as a bool. :param attribute: the key for an attribute value :type attribute: unicode or byte string :returns: the value corresponding to ``attribute``, as bool :rtype: bool :raises KeyError: if no value found for ``attribute`` :raises UnicodeDecodeError: if value is not convertible to unicode :raises ValueError: if unicode value can not be converted to a bool A boolean attribute has either a value of ``'1'`` or of ``'0'``, where ``'1'`` stands for ``True``, and ``'0'`` for ``False``. Any other value causes a :exc:`~exceptions.ValueError` to be raised. )rr™)r`r•r!r!r"r�¾szAttributes.asbool)N) rVrWrXrYrar�r–r—r;r™r€r�r!r!r!r"rw`s   rwc@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) rzzk A iterable over :class:`Device` tags. Subclasses the ``Container`` and the ``Iterable`` ABC. cCs||_|j|_dS)N)r&r$)r`r&r!r!r"raÚsz Tags.__init__cs>t|jdƒr$t|jj|jtˆƒƒƒSt‡fdd„|DƒƒSdS)z Whether ``tag`` exists. :param tag: unicode string with name of tag :rtype: bool Úudev_device_has_tagc3s|]}|ˆkVqdS)Nr!)r<Út)Útagr!r"r>ész Tags._has_tag..N)r“r$roršr&rÚany)r`rœr!)rœr"Ú_has_tagÞs z Tags._has_tagcCs |j|ƒS)z» Check for existence of ``tag``. ``tag`` is a tag as unicode string. Return ``True``, if ``tag`` is attached to the device, ``False`` otherwise. )rž)r`rœr!r!r"Ú __contains__ës zTags.__contains__ccs6|jj|jƒ}x"t|j|ƒD]\}}t|ƒVqWdS)zS Iterate over all tags. Yield each tag as unicode string. N)r$Zudev_device_get_tags_list_entryr&rr)r`r{rœrRr!r!r"r|ösz Tags.__iter__N)rVrWrXrYraržrŸr|r!r!r!r"rzÑs   rz)"rYZ __future__rrrrrrJÚ collectionsrrrZdatetimer Zpyudev.device._errorsr r r r rrrZ pyudev._utilrrrrrÚobjectrr%rxrwrzr!r!r!r"ÚsD                    ESq