3 u1‘Ws4ã@s¬dZddlmZmZmZmZddlmZddlm Z ddl m Z ddl m Z ddl mZddlmZdd lmZdd lmZdd lmZGd d „d eƒZGdd„deƒZdS)z’ pyudev.core =========== Core types and functions of :mod:`pyudev`. .. moduleauthor:: Sebastian Wiesner é)Úprint_functionÚdivisionÚunicode_literalsÚabsolute_import)ÚDevices)ÚDeviceNotFoundAtPathError)ÚERROR_CHECKERS)Ú SIGNATURES)Úload_ctypes_library)Úensure_byte_string)Úensure_unicode_string)Úproperty_value_to_bytes)Úudev_list_iteratec@sfeZdZdZdd„Zdd„Zedd„ƒZedd „ƒZed d „ƒZ ed d „ƒZ e j dd „ƒZ dd„Z dS)ÚContexta A device database connection. This class represents a connection to the udev device database, and is really *the* central object to access udev. You need an instance of this class for almost anything else in pyudev. This class itself gives access to various udev configuration data (e.g. :attr:`sys_path`, :attr:`device_path`), and provides device enumeration (:meth:`list_devices()`). Instances of this class can directly be given as ``udev *`` to functions wrapped through :mod:`ctypes`. cCstdttƒ|_|jjƒ|_dS)z' Create a new context. ZudevN)r r rÚ_libudevZudev_newÚ_as_parameter_)Úself©rú/usr/lib/python3.6/core.pyÚ__init__<szContext.__init__cCs|jj|ƒdS)N)rZ udev_unref)rrrrÚ__del__CszContext.__del__cCs$t|jdƒrt|jj|ƒƒSdSdS)zV The ``sysfs`` mount point defaulting to ``/sys'`` as unicode string. Úudev_get_sys_pathz/sysN)Úhasattrrr r)rrrrÚsys_pathFs zContext.sys_pathcCs$t|jdƒrt|jj|ƒƒSdSdS)zU The device directory path defaulting to ``/dev`` as unicode string. Úudev_get_dev_pathz/devN)rrr r)rrrrÚ device_pathQs zContext.device_pathcCs$t|jdƒrt|jj|ƒƒSdSdS)z  The run runtime directory path defaulting to ``/run`` as unicode string. .. udevversion:: 167 .. versionadded:: 0.10 Úudev_get_run_pathz /run/udevN)rrr r)rrrrÚrun_path\s zContext.run_pathcCs |jj|ƒS)a  The logging priority of the interal logging facitility of udev as integer with a standard :mod:`syslog` priority. Assign to this property to change the logging priority. UDev uses the standard :mod:`syslog` priorities. Constants for these priorities are defined in the :mod:`syslog` module in the standard library: >>> import syslog >>> context = pyudev.Context() >>> context.log_priority = syslog.LOG_DEBUG .. versionadded:: 0.9 )rZudev_get_log_priority)rrrrÚ log_prioritykszContext.log_prioritycCs|jj||ƒdS)zT Set the log priority. :param int value: the log priority. N)rZudev_set_log_priority)rÚvaluerrrr~scKst|ƒjf|ŽS)a" List all available devices. The arguments of this method are the same as for :meth:`Enumerator.match()`. In fact, the arguments are simply passed straight to method :meth:`~Enumerator.match()`. This function creates and returns an :class:`Enumerator` object, that can be used to filter the list of devices, and eventually retrieve :class:`Device` objects representing matching devices. .. versionchanged:: 0.8 Accept keyword arguments now for easy matching. )Ú EnumeratorÚmatch)rÚkwargsrrrÚ list_devices‡szContext.list_devicesN) Ú__name__Ú __module__Ú __qualname__Ú__doc__rrÚpropertyrrrrÚsetterr#rrrrr,s   rc@sleZdZdZdd„Zdd„Zdd„Zdd d „Zd d „Zd d„Z ddd„Z dd„Z dd„Z dd„Z dd„ZdS)r aÒ A filtered iterable of devices. To retrieve devices, simply iterate over an instance of this class. This operation yields :class:`Device` objects representing the available devices. Before iteration the device list can be filtered by subsystem or by property values using :meth:`match_subsystem` and :meth:`match_property`. Multiple subsystem (property) filters are combined using a logical OR, filters of different types are combined using a logical AND. The following filter for instance:: devices.match_subsystem('block').match_property( 'ID_TYPE', 'disk').match_property('DEVTYPE', 'disk') means the following:: subsystem == 'block' and (ID_TYPE == 'disk' or DEVTYPE == 'disk') Once added, a filter cannot be removed anymore. Create a new object instead. Instances of this class can directly be given as given ``udev_enumerate *`` to functions wrapped through :mod:`ctypes`. cCs2t|tƒstdƒ‚||_|jj|ƒ|_|j|_dS)zú Create a new enumerator with the given ``context`` (a :class:`Context` instance). While you can create objects of this class directly, this is not recommended. Call :method:`Context.list_devices()` instead. zInvalid context objectN)Ú isinstancerÚ TypeErrorÚcontextrZudev_enumerate_newr)rr,rrrrµs  zEnumerator.__init__cCs|jj|ƒdS)N)rZudev_enumerate_unref)rrrrrÃszEnumerator.__del__cKsž|jddƒ}|dk r|j|ƒ|jddƒ}|dk r<|j|ƒ|jddƒ}|dk rZ|j|ƒ|jddƒ}|dk rx|j|ƒx |jƒD]\}}|j||ƒq‚W|S)a2 Include devices according to the rules defined by the keyword arguments. These keyword arguments are interpreted as follows: - The value for the keyword argument ``subsystem`` is forwarded to :meth:`match_subsystem()`. - The value for the keyword argument ``sys_name`` is forwared to :meth:`match_sys_name()`. - The value for the keyword argument ``tag`` is forwared to :meth:`match_tag()`. - The value for the keyword argument ``parent`` is forwared to :meth:`match_parent()`. - All other keyword arguments are forwareded one by one to :meth:`match_property()`. The keyword argument itself is interpreted as property name, the value of the keyword argument as the property value. All keyword arguments are optional, calling this method without no arguments at all is simply a noop. Return the instance again. .. versionadded:: 0.8 .. versionchanged:: 0.13 Add ``parent`` keyword. Ú subsystemNÚsys_nameÚtagÚparent)ÚpopÚmatch_subsystemÚmatch_sys_nameÚ match_tagÚ match_parentÚitemsÚmatch_property)rr"r-r.r/r0Úproprrrrr!Æs        zEnumerator.matchFcCs&|r |jjn|jj}||t|ƒƒ|S)a$ Include all devices, which are part of the given ``subsystem``. ``subsystem`` is either a unicode string or a byte string, containing the name of the subsystem. If ``nomatch`` is ``True`` (default is ``False``), the match is inverted: A device is only included if it is *not* part of the given ``subsystem``. Note that, if a device has no subsystem, it is not included either with value of ``nomatch`` True or with value of ``nomatch`` False. Return the instance again. )rZ$udev_enumerate_add_nomatch_subsystemZ"udev_enumerate_add_match_subsystemr )rr-Únomatchr!rrrr2òszEnumerator.match_subsystemcCs|jj|t|ƒƒ|S)zË Include all devices with the given name. ``sys_name`` is a byte or unicode string containing the device name. Return the instance again. .. versionadded:: 0.8 )rZ udev_enumerate_add_match_sysnamer )rr.rrrr3s  zEnumerator.match_sys_namecCs|jj|t|ƒt|ƒƒ|S)aØ Include all devices, whose ``prop`` has the given ``value``. ``prop`` is either a unicode string or a byte string, containing the name of the property to match. ``value`` is a property value, being one of the following types: - :func:`int` - :func:`bool` - A byte string - Anything convertable to a unicode string (including a unicode string itself) Return the instance again. )rZ!udev_enumerate_add_match_propertyr r )rr8rrrrr7szEnumerator.match_propertycCs,|s |jjn|jj}||t|ƒt|ƒƒ|S)a¸ Include all devices, whose ``attribute`` has the given ``value``. ``attribute`` is either a unicode string or a byte string, containing the name of a sys attribute to match. ``value`` is an attribute value, being one of the following types: - :func:`int`, - :func:`bool` - A byte string - Anything convertable to a unicode string (including a unicode string itself) If ``nomatch`` is ``True`` (default is ``False``), the match is inverted: A device is include if the ``attribute`` does *not* match the given ``value``. .. note:: If ``nomatch`` is ``True``, devices which do not have the given ``attribute`` at all are also included. In other words, with ``nomatch=True`` the given ``attribute`` is *not* guaranteed to exist on all returned devices. Return the instance again. )rZ udev_enumerate_add_match_sysattrZ"udev_enumerate_add_nomatch_sysattrr r )rZ attributerr9r!rrrÚmatch_attribute's    zEnumerator.match_attributecCs|jj|t|ƒƒ|S)zô Include all devices, which have the given ``tag`` attached. ``tag`` is a byte or unicode string containing the tag name. Return the instance again. .. udevversion:: 154 .. versionadded:: 0.6 )rZudev_enumerate_add_match_tagr )rr/rrrr4Is zEnumerator.match_tagcCs|jj|ƒ|S)aÚ Include only devices, which are initialized. Initialized devices have properly set device node permissions and context, and are (in case of network devices) fully renamed. Currently this will not affect devices which do not have device nodes and are not network interfaces. Return the instance again. .. seealso:: :attr:`Device.is_initialized` .. udevversion:: 165 .. versionadded:: 0.8 )rZ'udev_enumerate_add_match_is_initialized)rrrrÚmatch_is_initializedXs zEnumerator.match_is_initializedcCs|jj||ƒ|S)a  Include all devices on the subtree of the given ``parent`` device. The ``parent`` device itself is also included. ``parent`` is a :class:`~pyudev.Device`. Return the instance again. .. udevversion:: 172 .. versionadded:: 0.13 )rZudev_enumerate_add_match_parent)rr0rrrr5mszEnumerator.match_parentc csb|jj|ƒ|jj|ƒ}xDt|j|ƒD]4\}}ytj|j|ƒVWq&tk rXw&Yq&Xq&WdS)z\ Iterate over all matching devices. Yield :class:`Device` objects. N)rZudev_enumerate_scan_devicesZudev_enumerate_get_list_entryrrZ from_sys_pathr,r)rÚentryÚnameÚ_rrrÚ__iter__~s  zEnumerator.__iter__N)F)F)r$r%r&r'rrr!r2r3r7r:r4r;r5r?rrrrr ™s,  "r N)r'Z __future__rrrrZ pyudev.devicerZpyudev.device._errorsrZpyudev._ctypeslib.libudevrr Zpyudev._ctypeslib.utilsr Z pyudev._utilr r r rÚobjectrr rrrrÚs         m