Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib/python3..../site-pac.../pyudev
File: _util.py
# -*- coding: utf-8 -*-
[0] Fix | Delete
# Copyright (C) 2010, 2011, 2012 Sebastian Wiesner <lunaryorn@gmail.com>
[1] Fix | Delete
[2] Fix | Delete
# This library is free software; you can redistribute it and/or modify it
[3] Fix | Delete
# under the terms of the GNU Lesser General Public License as published by the
[4] Fix | Delete
# Free Software Foundation; either version 2.1 of the License, or (at your
[5] Fix | Delete
# option) any later version.
[6] Fix | Delete
[7] Fix | Delete
# This library is distributed in the hope that it will be useful, but WITHOUT
[8] Fix | Delete
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
[9] Fix | Delete
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
[10] Fix | Delete
# for more details.
[11] Fix | Delete
[12] Fix | Delete
# You should have received a copy of the GNU Lesser General Public License
[13] Fix | Delete
# along with this library; if not, write to the Free Software Foundation,
[14] Fix | Delete
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
"""
[18] Fix | Delete
pyudev._util
[19] Fix | Delete
============
[20] Fix | Delete
[21] Fix | Delete
Internal utilities
[22] Fix | Delete
[23] Fix | Delete
.. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
[24] Fix | Delete
"""
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
from __future__ import (print_function, division, unicode_literals,
[28] Fix | Delete
absolute_import)
[29] Fix | Delete
[30] Fix | Delete
try:
[31] Fix | Delete
from subprocess import check_output
[32] Fix | Delete
except ImportError:
[33] Fix | Delete
from pyudev._compat import check_output
[34] Fix | Delete
[35] Fix | Delete
import os
[36] Fix | Delete
import sys
[37] Fix | Delete
import stat
[38] Fix | Delete
import errno
[39] Fix | Delete
[40] Fix | Delete
import six
[41] Fix | Delete
[42] Fix | Delete
[43] Fix | Delete
def ensure_byte_string(value):
[44] Fix | Delete
"""
[45] Fix | Delete
Return the given ``value`` as bytestring.
[46] Fix | Delete
[47] Fix | Delete
If the given ``value`` is not a byte string, but a real unicode string, it
[48] Fix | Delete
is encoded with the filesystem encoding (as in
[49] Fix | Delete
:func:`sys.getfilesystemencoding()`).
[50] Fix | Delete
"""
[51] Fix | Delete
if not isinstance(value, bytes):
[52] Fix | Delete
value = value.encode(sys.getfilesystemencoding())
[53] Fix | Delete
return value
[54] Fix | Delete
[55] Fix | Delete
[56] Fix | Delete
def ensure_unicode_string(value):
[57] Fix | Delete
"""
[58] Fix | Delete
Return the given ``value`` as unicode string.
[59] Fix | Delete
[60] Fix | Delete
If the given ``value`` is not a unicode string, but a byte string, it is
[61] Fix | Delete
decoded with the filesystem encoding (as in
[62] Fix | Delete
:func:`sys.getfilesystemencoding()`).
[63] Fix | Delete
"""
[64] Fix | Delete
if not isinstance(value, six.text_type):
[65] Fix | Delete
value = value.decode(sys.getfilesystemencoding())
[66] Fix | Delete
return value
[67] Fix | Delete
[68] Fix | Delete
[69] Fix | Delete
def property_value_to_bytes(value):
[70] Fix | Delete
"""
[71] Fix | Delete
Return a byte string, which represents the given ``value`` in a way
[72] Fix | Delete
suitable as raw value of an udev property.
[73] Fix | Delete
[74] Fix | Delete
If ``value`` is a boolean object, it is converted to ``'1'`` or ``'0'``,
[75] Fix | Delete
depending on whether ``value`` is ``True`` or ``False``. If ``value`` is a
[76] Fix | Delete
byte string already, it is returned unchanged. Anything else is simply
[77] Fix | Delete
converted to a unicode string, and then passed to
[78] Fix | Delete
:func:`ensure_byte_string`.
[79] Fix | Delete
"""
[80] Fix | Delete
# udev represents boolean values as 1 or 0, therefore an explicit
[81] Fix | Delete
# conversion to int is required for boolean values
[82] Fix | Delete
if isinstance(value, bool):
[83] Fix | Delete
value = int(value)
[84] Fix | Delete
if isinstance(value, bytes):
[85] Fix | Delete
return value
[86] Fix | Delete
else:
[87] Fix | Delete
return ensure_byte_string(six.text_type(value))
[88] Fix | Delete
[89] Fix | Delete
[90] Fix | Delete
def string_to_bool(value):
[91] Fix | Delete
"""
[92] Fix | Delete
Convert the given unicode string ``value`` to a boolean object.
[93] Fix | Delete
[94] Fix | Delete
If ``value`` is ``'1'``, ``True`` is returned. If ``value`` is ``'0'``,
[95] Fix | Delete
``False`` is returned. Any other value raises a
[96] Fix | Delete
:exc:`~exceptions.ValueError`.
[97] Fix | Delete
"""
[98] Fix | Delete
if value not in ('1', '0'):
[99] Fix | Delete
raise ValueError('Not a boolean value: {0!r}'.format(value))
[100] Fix | Delete
return value == '1'
[101] Fix | Delete
[102] Fix | Delete
[103] Fix | Delete
def udev_list_iterate(libudev, entry):
[104] Fix | Delete
"""
[105] Fix | Delete
Iteration helper for udev list entry objects.
[106] Fix | Delete
[107] Fix | Delete
Yield a tuple ``(name, value)``. ``name`` and ``value`` are bytestrings
[108] Fix | Delete
containing the name and the value of the list entry. The exact contents
[109] Fix | Delete
depend on the list iterated over.
[110] Fix | Delete
"""
[111] Fix | Delete
while entry:
[112] Fix | Delete
name = libudev.udev_list_entry_get_name(entry)
[113] Fix | Delete
value = libudev.udev_list_entry_get_value(entry)
[114] Fix | Delete
yield (name, value)
[115] Fix | Delete
entry = libudev.udev_list_entry_get_next(entry)
[116] Fix | Delete
[117] Fix | Delete
[118] Fix | Delete
def get_device_type(filename):
[119] Fix | Delete
"""
[120] Fix | Delete
Get the device type of a device file.
[121] Fix | Delete
[122] Fix | Delete
``filename`` is a string containing the path of a device file.
[123] Fix | Delete
[124] Fix | Delete
Return ``'char'`` if ``filename`` is a character device, or ``'block'`` if
[125] Fix | Delete
``filename`` is a block device. Raise :exc:`~exceptions.ValueError` if
[126] Fix | Delete
``filename`` is no device file at all. Raise
[127] Fix | Delete
:exc:`~exceptions.EnvironmentError` if ``filename`` does not exist or if
[128] Fix | Delete
its metadata was inaccessible.
[129] Fix | Delete
[130] Fix | Delete
.. versionadded:: 0.15
[131] Fix | Delete
"""
[132] Fix | Delete
mode = os.stat(filename).st_mode
[133] Fix | Delete
if stat.S_ISCHR(mode):
[134] Fix | Delete
return 'char'
[135] Fix | Delete
elif stat.S_ISBLK(mode):
[136] Fix | Delete
return 'block'
[137] Fix | Delete
else:
[138] Fix | Delete
raise ValueError('not a device file: {0!r}'.format(filename))
[139] Fix | Delete
[140] Fix | Delete
[141] Fix | Delete
def eintr_retry_call(func, *args, **kwargs):
[142] Fix | Delete
"""
[143] Fix | Delete
Handle interruptions to an interruptible system call.
[144] Fix | Delete
[145] Fix | Delete
Run an interruptible system call in a loop and retry if it raises EINTR.
[146] Fix | Delete
The signal calls that may raise EINTR prior to Python 3.5 are listed in
[147] Fix | Delete
PEP 0475. Any calls to these functions must be wrapped in eintr_retry_call
[148] Fix | Delete
in order to handle EINTR returns in older versions of Python.
[149] Fix | Delete
[150] Fix | Delete
This function is safe to use under Python 3.5 and newer since the wrapped
[151] Fix | Delete
function will simply return without raising EINTR.
[152] Fix | Delete
[153] Fix | Delete
This function is based on _eintr_retry_call in python's subprocess.py.
[154] Fix | Delete
"""
[155] Fix | Delete
[156] Fix | Delete
# select.error inherits from Exception instead of OSError in Python 2
[157] Fix | Delete
import select
[158] Fix | Delete
[159] Fix | Delete
while True:
[160] Fix | Delete
try:
[161] Fix | Delete
return func(*args, **kwargs)
[162] Fix | Delete
except (OSError, IOError, select.error) as err:
[163] Fix | Delete
# If this is not an IOError or OSError, it's the old select.error
[164] Fix | Delete
# type, which means that the errno is only accessible via subscript
[165] Fix | Delete
if isinstance(err, (OSError, IOError)):
[166] Fix | Delete
error_code = err.errno
[167] Fix | Delete
else:
[168] Fix | Delete
error_code = err.args[0]
[169] Fix | Delete
[170] Fix | Delete
if error_code == errno.EINTR:
[171] Fix | Delete
continue
[172] Fix | Delete
raise
[173] Fix | Delete
[174] Fix | Delete
def udev_version():
[175] Fix | Delete
"""
[176] Fix | Delete
Get the version of the underlying udev library.
[177] Fix | Delete
[178] Fix | Delete
udev doesn't use a standard major-minor versioning scheme, but instead
[179] Fix | Delete
labels releases with a single consecutive number. Consequently, the
[180] Fix | Delete
version number returned by this function is a single integer, and not a
[181] Fix | Delete
tuple (like for instance the interpreter version in
[182] Fix | Delete
:data:`sys.version_info`).
[183] Fix | Delete
[184] Fix | Delete
As libudev itself does not provide a function to query the version number,
[185] Fix | Delete
this function calls the ``udevadm`` utility, so be prepared to catch
[186] Fix | Delete
:exc:`~exceptions.EnvironmentError` and
[187] Fix | Delete
:exc:`~subprocess.CalledProcessError` if you call this function.
[188] Fix | Delete
[189] Fix | Delete
Return the version number as single integer. Raise
[190] Fix | Delete
:exc:`~exceptions.ValueError`, if the version number retrieved from udev
[191] Fix | Delete
could not be converted to an integer. Raise
[192] Fix | Delete
:exc:`~exceptions.EnvironmentError`, if ``udevadm`` was not found, or could
[193] Fix | Delete
not be executed. Raise :exc:`subprocess.CalledProcessError`, if
[194] Fix | Delete
``udevadm`` returned a non-zero exit code. On Python 2.7 or newer, the
[195] Fix | Delete
``output`` attribute of this exception is correctly set.
[196] Fix | Delete
[197] Fix | Delete
.. versionadded:: 0.8
[198] Fix | Delete
"""
[199] Fix | Delete
output = ensure_unicode_string(check_output(['udevadm', '--version']))
[200] Fix | Delete
return int(output.strip())
[201] Fix | Delete
[202] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function