a 8g+^ã@s¸dZddlmZmZmZmZzddlmZWneyJddl mZYn0ddl Z ddl Z ddl Z ddl Z ddlZdd„Zdd„Zd d „Zd d „Zd d„Zdd„Zdd„Zdd„ZdS)z| pyudev._util ============ Internal utilities .. moduleauthor:: Sebastian Wiesner é)Úprint_functionÚdivisionÚunicode_literalsÚabsolute_import)Ú check_outputNcCst|tƒs| t ¡¡}|S)zà Return the given ``value`` as bytestring. If the given ``value`` is not a byte string, but a real unicode string, it is encoded with the filesystem encoding (as in :func:`sys.getfilesystemencoding()`). )Ú isinstanceÚbytesÚencodeÚsysÚgetfilesystemencoding©Úvalue©rú0/usr/lib/python3.9/site-packages/pyudev/_util.pyÚensure_byte_string*s rcCst|tjƒs| t ¡¡}|S)zß Return the given ``value`` as unicode string. If the given ``value`` is not a unicode string, but a byte string, it is decoded with the filesystem encoding (as in :func:`sys.getfilesystemencoding()`). )rÚsixÚ text_typeÚdecoder r r rrrÚensure_unicode_string7s rcCs.t|tƒrt|ƒ}t|tƒr |Stt |¡ƒS)a¼ Return a byte string, which represents the given ``value`` in a way suitable as raw value of an udev property. If ``value`` is a boolean object, it is converted to ``'1'`` or ``'0'``, depending on whether ``value`` is ``True`` or ``False``. If ``value`` is a byte string already, it is returned unchanged. Anything else is simply converted to a unicode string, and then passed to :func:`ensure_byte_string`. )rÚboolÚintrrrrr rrrÚproperty_value_to_bytesDs  rcCs|dvrtd |¡ƒ‚|dkS)zï Convert the given unicode string ``value`` to a boolean object. If ``value`` is ``'1'``, ``True`` is returned. If ``value`` is ``'0'``, ``False`` is returned. Any other value raises a :exc:`~exceptions.ValueError`. )Ú1Ú0zNot a boolean value: {0!r}r)Ú ValueErrorÚformatr rrrÚstring_to_boolXsrccs2|r.| |¡}| |¡}||fV| |¡}qdS)zø Iteration helper for udev list entry objects. Yield a tuple ``(name, value)``. ``name`` and ``value`` are bytestrings containing the name and the value of the list entry. The exact contents depend on the list iterated over. N)Zudev_list_entry_get_nameZudev_list_entry_get_valueZudev_list_entry_get_next)ZlibudevÚentryÚnamer rrrÚudev_list_iteratees    rcCs:t |¡j}t |¡rdSt |¡r(dStd |¡ƒ‚dS)aÍ Get the device type of a device file. ``filename`` is a string containing the path of a device file. Return ``'char'`` if ``filename`` is a character device, or ``'block'`` if ``filename`` is a block device. Raise :exc:`~exceptions.ValueError` if ``filename`` is no device file at all. Raise :exc:`~exceptions.EnvironmentError` if ``filename`` does not exist or if its metadata was inaccessible. .. versionadded:: 0.15 ÚcharÚblockznot a device file: {0!r}N)ÚosÚstatÚst_modeÚS_ISCHRÚS_ISBLKrr)ÚfilenameÚmoderrrÚget_device_typets    r)c Os„ddl}z||i|¤ŽWStt|jfy|}zDt|ttfƒrF|j}n |jd}|tjkrfWYd}~q‚WYd}~qd}~00qdS)a= Handle interruptions to an interruptible system call. Run an interruptible system call in a loop and retry if it raises EINTR. The signal calls that may raise EINTR prior to Python 3.5 are listed in PEP 0475. Any calls to these functions must be wrapped in eintr_retry_call in order to handle EINTR returns in older versions of Python. This function is safe to use under Python 3.5 and newer since the wrapped function will simply return without raising EINTR. This function is based on _eintr_retry_call in python's subprocess.py. rN)ÚselectÚOSErrorÚIOErrorÚerrorrÚerrnoÚargsZEINTR)Úfuncr/Úkwargsr*ÚerrZ error_coderrrÚeintr_retry_call‹s   r3cCsttddgƒƒ}t| ¡ƒS)ak Get the version of the underlying udev library. udev doesn't use a standard major-minor versioning scheme, but instead labels releases with a single consecutive number. Consequently, the version number returned by this function is a single integer, and not a tuple (like for instance the interpreter version in :data:`sys.version_info`). As libudev itself does not provide a function to query the version number, this function calls the ``udevadm`` utility, so be prepared to catch :exc:`~exceptions.EnvironmentError` and :exc:`~subprocess.CalledProcessError` if you call this function. Return the version number as single integer. Raise :exc:`~exceptions.ValueError`, if the version number retrieved from udev could not be converted to an integer. Raise :exc:`~exceptions.EnvironmentError`, if ``udevadm`` was not found, or could not be executed. Raise :exc:`subprocess.CalledProcessError`, if ``udevadm`` returned a non-zero exit code. On Python 2.7 or newer, the ``output`` attribute of this exception is correctly set. .. versionadded:: 0.8 Zudevadmz --version)rrrÚstrip)ÚoutputrrrÚ udev_version­sr6)Ú__doc__Z __future__rrrrÚ subprocessrÚ ImportErrorZpyudev._compatr"r r#r.rrrrrrr)r3r6rrrrÚs$     "