Your IP : 3.129.216.248


Current Path : /usr/lib/python3.6/site-packages/pyudev/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/pyudev/__pycache__/discover.cpython-36.opt-1.pyc

3

u1�W�,�@s�dZddlmZddlmZddlmZddlmZddlZddlZddlZddl	Z	ddl
Z
ddlmZddlm
Z
d	d
�Ze
jej�Gdd�de��ZGd
d�de�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�ZdS)z�
    pyudev.discover
    ===============

    Tools to discover a device given limited information.

    .. moduleauthor::  mulhern <amulhern@redhat.com>
�)�absolute_import)�division)�print_function)�unicode_literalsN)�Devices)�DeviceNotFoundErrorcstj���fdd��}|S)z\
    Allow Device discovery methods to return None instead of raising an
    exception.
    cs$y
�||�Stk
rdSXdS)z�
        Returns result of calling ``func`` on ``args``, ``kwargs``.
        Returns None if ``func`` raises :exc:`DeviceNotFoundError`.
        N)r)�args�kwargs)�func��/usr/lib/python3.6/discover.py�the_func1s
z wrap_exception.<locals>.the_func)�	functools�wraps)r
r
r)r
r�wrap_exception+src@sLeZdZdZeejdd���Zeejdd���Zedd��Z	edd	��Z
d
S)�
HypothesiszM
    Represents a hypothesis about the meaning of the device identifier.
    cCs
t��dS)a�
        Match the given string according to the hypothesis.

        The purpose of this method is to obtain a value corresponding to
        ``value`` if that is possible. It may use a regular expression, but
        in general it should just return ``value`` and let the lookup method
        sort out the rest.

        :param str value: the string to inspect
        :returns: the matched thing or None if unmatched
        :rtype: the type of lookup's key parameter or NoneType
        N)�NotImplementedError)�cls�valuerrr�matchDszHypothesis.matchcCs
t��dS)aN
        Lookup the given string according to the hypothesis.

        :param Context context: the pyudev context
        :param key: a key with which to lookup the device
        :type key: the type of match's return value if not None
        :returns: a list of Devices obtained
        :rtype: frozenset of :class:`Device`
        N)r)r�context�keyrrr�lookupUszHypothesis.lookupcCsdS)z�
        A potentially expensive method that may allow an :class:`Hypothesis`
        to find devices more rapidly or to find a device that it would
        otherwise miss.

        :param Context context: the pyudev context
        Nr)rrrrr�setupcs	zHypothesis.setupcCs$|j|�}|dk	r|j||�St�S)a
        Get any devices that may correspond to the given string.

        :param Context context: the pyudev context
        :param str value: the value to look for
        :returns: a list of devices obtained
        :rtype: set of :class:`Device`
        N)rr�	frozenset)rrrrrrr�get_devicesns

zHypothesis.get_devicesN)�__name__�
__module__�__qualname__�__doc__�classmethod�abc�abstractmethodrrrrrrrrr>s
rc@sLeZdZdZedd��Zedd��Zedd��Zedd	��Zed
d��Z	dS)
�DeviceNumberHypothesisz�
    Represents the hypothesis that the device is a device number.

    The device may be separated into major/minor number or a composite number.
    cCs8tjd�}|j|�}|o6tjt|jd��t|jd���S)z�
        Match the number under the assumption that it is a major,minor pair.

        :param str value: value to match
        :returns: the device number or None
        :rtype: int or NoneType
        z#^(?P<major>\d+)(\D+)(?P<minor>\d+)$�major�minor)�re�compiler�os�makedev�int�group)rrZmajor_minor_rerrrr�_match_major_minor�s	
z)DeviceNumberHypothesis._match_major_minorcCs&tjd�}|j|�}|o$t|jd��S)z�
        Match the number under the assumption that it is a single number.

        :param str value: value to match
        :returns: the device number or None
        :rtype: int or NoneType
        z^(?P<number>\d+)$Znumber)r&r'rr*r+)rrZ	number_rerrrr�
_match_number�s	

z$DeviceNumberHypothesis._match_numbercCs|j|�p|j|�S)z�
        Match the number under the assumption that it is a device number.

        :returns: the device number or None
        :rtype: int or NoneType
        )r,r-)rrrrrr�szDeviceNumberHypothesis.matchcCs|j}tjtjj|d��S)z�
        Find subsystems in /sys/dev.

        :param Context context: the context
        :returns: a lis of available subsystems
        :rtype: list of str
        Zdev)�sys_pathr(�listdir�path�join)rrr.rrr�find_subsystems�s	z&DeviceNumberHypothesis.find_subsystemscs8ttj�����fdd�|j��D�}tdd�|D��S)z�
        Lookup by the device number.

        :param Context context: the context
        :param int key: the device number
        :returns: a list of matching devices
        :rtype: frozenset of :class:`Device`
        c3s|]}��|��VqdS)Nr)�.0�s)rr
rrr�	<genexpr>�sz0DeviceNumberHypothesis.lookup.<locals>.<genexpr>css|]}|dk	r|VqdS)Nr)r3�rrrrr5�s)rrZfrom_device_numberr2r)rrr�resr)rr
rrr�s

zDeviceNumberHypothesis.lookupN)
rrrrr r,r-rr2rrrrrr#|s

r#c@s(eZdZdZedd��Zedd��ZdS)�DevicePathHypothesiszG
    Discover the device assuming the identifier is a device path.
    cCs|S)z�
        Match ``value`` under the assumption that it is a device path.

        :returns: the device path or None
        :rtype: str or NoneType
        r)rrrrrr�szDevicePathHypothesis.matchcCs(ttj�||�}|dk	r"t|f�St�S)z�
        Lookup by the path.

        :param Context context: the context
        :param str key: the device path
        :returns: a list of matching devices
        :rtype: frozenset of :class:`Device`
        N)rrZ	from_pathr)rrrr7rrrr�s
zDevicePathHypothesis.lookupN)rrrrr rrrrrrr8�s
r8c@s4eZdZdZedd��Zedd��Zedd��ZdS)	�DeviceNameHypothesiszf
    Discover the device assuming the input is a device name.

    Try every available subsystem.
    cs<|j�d}�fdd�|D�}dd�|D�}tdd�|D��S)	z�
        Find all subsystems in sysfs.

        :param Context context: the context
        :rtype: frozenset
        :returns: subsystems in sysfs
        �bus�class�	subsystemc3s|]}tjj�|�VqdS)N)r(r0r1)r3�name)r.rrr5�sz7DeviceNameHypothesis.find_subsystems.<locals>.<genexpr>css|]}tjj|�r|VqdS)N)r(r0�isdir)r3�drrrr5�scss"|]}tj|�D]
}|VqqdS)N)r(r/)r3r?�nrrrr5�s)r:r;r<)r.r)rrZdirnamesZabsnamesZ	realnamesr)r.rr2�s
	z$DeviceNameHypothesis.find_subsystemscCs|S)z�
        Match ``value`` under the assumption that it is a device name.

        :returns: the device path or None
        :rtype: str or NoneType
        r)rrrrrr�szDeviceNameHypothesis.matchcs8ttj�����fdd�|j��D�}tdd�|D��S)z�
        Lookup by the path.

        :param Context context: the context
        :param str key: the device path
        :returns: a list of matching devices
        :rtype: frozenset of :class:`Device`
        c3s|]}��|��VqdS)Nr)r3r4)rr
rrrr5sz.DeviceNameHypothesis.lookup.<locals>.<genexpr>css|]}|dk	r|VqdS)Nr)r3r6rrrr5s)rr�	from_namer2r)rrrr7r)rr
rrrs

zDeviceNameHypothesis.lookupN)rrrrr r2rrrrrrr9�s
r9c@sZeZdZdZdddddddd	d
ddgZed
d��Zedd��Zedd��Zedd��Z	dS)�DeviceFileHypothesisz�
    Discover the device assuming the value is some portion of a device file.

    The device file may be a link to a device node.
    z/devz/dev/disk/by-idz/dev/disk/by-labelz/dev/disk/by-partlabelz/dev/disk/by-partuuidz/dev/disk/by-pathz/dev/disk/by-uuidz/dev/input/by-pathz/dev/mapperz/dev/mdz/dev/vgcCs:|j�}dd�|D�}dd�|D�}ttdd�|D���S)a7
        Get all directories that may contain links to device nodes.

        This method checks the device links of every device, so it is very
        expensive.

        :param Context context: the context
        :returns: a sorted list of directories that contain device links
        :rtype: list
        css|]}t|j�r|VqdS)N)�list�device_links)r3r?rrrr55sz5DeviceFileHypothesis.get_link_dirs.<locals>.<genexpr>css|]}|jD]
}|VqqdS)N)rD)r3r?�lrrrr56scss|]}tjj|�VqdS)N)r(r0�dirname)r3rErrrr57s)Zlist_devices�sorted�set)rr�devicesZdevices_with_linksZlinksrrr�
get_link_dirs(sz"DeviceFileHypothesis.get_link_dirscCs|j|�|_dS)z�
        Set the link directories to be used when discovering by file.

        Uses `get_link_dirs`, so is as expensive as it is.

        :param Context context: the context
        N)rJ�
_LINK_DIRS)rrrrrr9s	zDeviceFileHypothesis.setupcCs|S)Nr)rrrrrrDszDeviceFileHypothesis.matchcsrttj��d�kr4����}|dk	r.t|f�St�S�fdd�|jD�}��fdd�|D�}tdd�|D��SdS)a�
        Lookup the device under the assumption that the key is part of
        the name of a device file.

        :param Context context: the context
        :param str key: a portion of the device file name

        It is assumed that either it is the whole name of the device file
        or it is the basename.

        A device file may be a device node or a device link.
        �/Nc3s|]}tjj|��VqdS)N)r(r0r1)r3Zld)rrrr5[sz.DeviceFileHypothesis.lookup.<locals>.<genexpr>c3s|]}��|�VqdS)Nr)r3�f)rr
rrr5\scss|]}|dk	r|VqdS)Nr)r3r?rrrr5]s)rrZfrom_device_filerrK)rrrZdevice�filesrIr)rr
rrrHs

zDeviceFileHypothesis.lookupN)
rrrrrKr rJrrrrrrrrBs rBc@s4eZdZdZeeeegZdd�Z	dd�Z
dd�ZdS)	�	Discoveryz1
    Provides discovery methods for devices.
    cCs|j|_dS)N)�_HYPOTHESES�_hypotheses)�selfrrr�__init__mszDiscovery.__init__cCsx|jD]}|j|�qWdS)z
        Set up individual hypotheses.

        May be an expensive call.

        :param Context context: the context
        N)rQr)rRrZhyprrrrpszDiscovery.setupcst��fdd�|jD��S)z�
        Get the devices corresponding to value.

        :param Context context: the context
        :param str value: some identifier of the device
        :returns: a list of corresponding devices
        :rtype: frozenset of :class:`Device`
        c3s$|]}|j���D]
}|VqqdS)N)r)r3�hr?)rrrrr5�sz(Discovery.get_devices.<locals>.<genexpr>)rrQ)rRrrr)rrrr{s	zDiscovery.get_devicesN)rrrrrBr9r#r8rPrSrrrrrrrO`srO)rZ
__future__rrrrr!rr(r&ZsixZ
pyudev.devicerrrZ
add_metaclass�ABCMeta�objectrr#r8r9rBrOrrrr�<module>s&=K/M

?>