Ë
    9*ªbO,  ã                   óÊ   — d Z ddlZddlZddlZddlZddlmZ ddlmZ d„ Z	 G d„ de
«      Z G d„ d	e«      Z G d
„ de«      Z G d„ de«      Z G d„ de«      Z G d„ de
«      Zy)zš
    pyudev.discover
    ===============

    Tools to discover a device given limited information.

    .. moduleauthor::  mulhern <amulhern@redhat.com>
é    N©ÚDeviceNotFoundError)ÚDevicesc                 óB   ‡ — t        j                  ‰ «      ˆ fd„«       }|S )z\
    Allow Device discovery methods to return None instead of raising an
    exception.
    c                  ó4   •— 	  ‰| i |¤ŽS # t         $ r Y yw xY w)z‘
        Returns result of calling ``func`` on ``args``, ``kwargs``.
        Returns None if ``func`` raises :exc:`DeviceNotFoundError`.
        Nr   )ÚargsÚkwargsÚfuncs     €ú1/usr/lib/python3/dist-packages/pyudev/discover.pyÚthe_funcz wrap_exception.<locals>.the_func+   s*   ø€ ð	Ù˜Ð( Ñ(Ð(øÜ"ò 	Ùð	ús   ƒ ‹	–)Ú	functoolsÚwraps)r
   r   s   ` r   Úwrap_exceptionr   %   s'   ø€ ô ‡_�_�TÓóó ðð €Oó    c                   ó¤   — e Zd ZdZej
                  Zeej                  d„ «       «       Z	eej                  d„ «       «       Z
ed„ «       Zed„ «       Zy)Ú
HypothesiszM
    Represents a hypothesis about the meaning of the device identifier.
    c                 ó   — t        «       ‚)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
        ©ÚNotImplementedError©ÚclsÚvalues     r   ÚmatchzHypothesis.match@   s   € ô "Ó#Ð#r   c                 ó   — t        «       ‚)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`
        r   )r   ÚcontextÚkeys      r   ÚlookupzHypothesis.lookupQ   s   € ô "Ó#Ð#r   c                  ó   — y)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
        N© ©r   r   s     r   ÚsetupzHypothesis.setup_   s   € ð 	r   c                 ó`   — | j                  |«      }|�| j                  ||«      S t        «       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`
        )r   r   Ú	frozenset)r   r   r   r   s       r   Úget_deviceszHypothesis.get_devicesj   s.   € ð �i‰i˜ÓˆØ+.¨?ˆs�z‰z˜' 3Ó'ÐKÄ	ÃÐKr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚabcÚABCMetaÚ__metaclass__ÚclassmethodÚabstractmethodr   r   r!   r$   r   r   r   r   r   9   s~   „ ñð —K‘K€MàØ×Ññ$ó ó ð$ð Ø×Ññ
$ó ó ð
$ð ñó ðð ñ
Ló ñ
Lr   r   c                   ó`   — e Zd ZdZed„ «       Zed„ «       Zed„ «       Zed„ «       Zed„ «       Z	y)ÚDeviceNumberHypothesisz—
    Represents the hypothesis that the device is a device number.

    The device may be separated into major/minor number or a composite number.
    c                 óâ   — t        j                  d«      }|j                  |«      }|xrF t        j                  t        |j                  d«      «      t        |j                  d«      «      «      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)r   r   Úmajor_minor_rer   s       r   Ú_match_major_minorz)DeviceNumberHypothesis._match_major_minor   s\   € ô Ÿ™Ð$JÓKˆØ×$Ñ$ UÓ+ˆØò 
œŸ™Ü�—‘˜GÓ$Ó%¤s¨5¯;©;°wÓ+?Ó'@ó
ð 	
r   c                 óŠ   — t        j                  d«      }|j                  |«      }|xr t        |j	                  d«      «      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+)$Únumber)r3   r4   r   r7   r8   )r   r   Ú	number_rer   s       r   Ú_match_numberz$DeviceNumberHypothesis._match_numberŽ   s:   € ô —J‘JÐ3Ó4ˆ	Ø—‘ Ó&ˆØÒ3œ˜UŸ[™[¨Ó2Ó3Ð3r   c                 óJ   — | j                  |«      xs | 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>   r   s     r   r   zDeviceNumberHypothesis.match›   s%   € ð ×%Ñ% eÓ,ÒH°×0AÑ0AÀ%Ó0HÐHr   c                 ó€   — |j                   }t        j                  t        j                  j	                  |d«      «      S )z§
        Find subsystems in /sys/dev.

        :param Context context: the context
        :returns: a lis of available subsystems
        :rtype: list of str
        Údev)Úsys_pathr5   ÚlistdirÚpathÚjoin)r   r   rB   s      r   Úfind_subsystemsz&DeviceNumberHypothesis.find_subsystems¥   s-   € ð ×#Ñ#ˆÜ�z‰zœ"Ÿ'™'Ÿ,™, x°Ó7Ó8Ð8r   c                 ó–   ‡‡‡— t        t        j                  «      Šˆˆˆfd„| j                  ‰«      D «       }t	        d„ |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`
        c              3   ó2   •K  — | ]  } ‰‰|‰«      –— Œ y ­w©Nr   ©Ú.0Úsr   r
   r   s     €€€r   ú	<genexpr>z0DeviceNumberHypothesis.lookup.<locals>.<genexpr>¼   ó   øè ø€ ÒK¨‰t�G˜Q ×$ÑKùó   ƒc              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrI   r   ©rK   Úrs     r   rM   z0DeviceNumberHypothesis.lookup.<locals>.<genexpr>½   ó   è ø€ Ò9˜q¨1©=œÑ9ùó   ‚Š)r   r   Úfrom_device_numberrF   r#   ©r   r   r   Úresr
   s    `` @r   r   zDeviceNumberHypothesis.lookup±   s;   ú€ ô œg×8Ñ8Ó9ˆÝK¨c×.AÑ.AÀ'Ó.JÔKˆÜÑ9 CÔ9Ó9Ð9r   N)
r%   r&   r'   r(   r,   r:   r>   r   rF   r   r   r   r   r/   r/   x   sm   „ ñð ñ
ó ð
ð ñ
4ó ð
4ð ñIó ðIð ñ	9ó ð	9ð ñ:ó ñ:r   r/   c                   ó0   — e Zd ZdZed„ «       Zed„ «       Zy)ÚDevicePathHypothesiszG
    Discover the device assuming the identifier is a device path.
    c                 ó   — |S )z›
        Match ``value`` under the assumption that it is a device path.

        :returns: the device path or None
        :rtype: str or NoneType
        r   r   s     r   r   zDevicePathHypothesis.matchÅ   ó	   € ð ˆr   c                 ór   —  t        t        j                  «      ||«      }|�t        |f«      S t        «       S )úÔ
        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`
        )r   r   Ú	from_pathr#   )r   r   r   rW   s       r   r   zDevicePathHypothesis.lookupÏ   s5   € ð 0ŒnœW×.Ñ.Ó/°¸Ó=ˆØ$' OŒy˜#˜Ó ÐD¼»ÐDr   N)r%   r&   r'   r(   r,   r   r   r   r   r   rY   rY   À   s1   „ ñð ñó ðð ñ
Eó ñ
Er   rY   c                   ó@   — e Zd ZdZed„ «       Zed„ «       Zed„ «       Zy)ÚDeviceNameHypothesiszf
    Discover the device assuming the input is a device name.

    Try every available subsystem.
    c                 ól   ‡— |j                   Šd}ˆfd„|D «       }d„ |D «       }t        d„ |D «       «      S )zœ
        Find all subsystems in sysfs.

        :param Context context: the context
        :rtype: frozenset
        :returns: subsystems in sysfs
        )ÚbusÚclassÚ	subsystemc              3   ó^   •K  — | ]$  }t         j                  j                  ‰|«      –— Œ& y ­wrI   ©r5   rD   rE   )rK   ÚnamerB   s     €r   rM   z7DeviceNameHypothesis.find_subsystems.<locals>.<genexpr>ï   s    øè ø€ ÒF°T”B—G‘G—L‘L ¨4×0ÑFùó   ƒ*-c              3   ó`   K  — | ]&  }t         j                  j                  |«      sŒ#|–— Œ( y ­wrI   )r5   rD   Úisdir©rK   Úds     r   rM   z7DeviceNameHypothesis.find_subsystems.<locals>.<genexpr>ð   s   è ø€ Ò=˜1¬B¯G©G¯M©M¸!Õ,<”QÑ=ùs   ‚$.§.c              3   óT   K  — | ]   }t        j                  |«      D ]  }|–— Œ Œ" y ­wrI   )r5   rC   )rK   rl   Úns      r   rM   z7DeviceNameHypothesis.find_subsystems.<locals>.<genexpr>ñ   s$   è ø€ ÒE˜q´r·z±zÀ!³}ÒE°!œÐE˜ÑEùs   ‚&()rB   r#   )r   r   ÚdirnamesÚabsnamesÚ	realnamesrB   s        @r   rF   z$DeviceNameHypothesis.find_subsystemsä   s:   ø€ ð ×#Ñ#ˆØ0ˆÛF¸XÔFˆÙ= Ô=ˆ	ÜÑE IÔEÓEÐEr   c                 ó   — |S )z›
        Match ``value`` under the assumption that it is a device name.

        :returns: the device path or None
        :rtype: str or NoneType
        r   r   s     r   r   zDeviceNameHypothesis.matchó   r[   r   c                 ó–   ‡‡‡— t        t        j                  «      Šˆˆˆfd„| j                  ‰«      D «       }t	        d„ |D «       «      S )r]   c              3   ó2   •K  — | ]  } ‰‰|‰«      –— Œ y ­wrI   r   rJ   s     €€€r   rM   z.DeviceNameHypothesis.lookup.<locals>.<genexpr>  rN   rO   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrI   r   rQ   s     r   rM   z.DeviceNameHypothesis.lookup.<locals>.<genexpr>	  rS   rT   )r   r   Ú	from_namerF   r#   rV   s    `` @r   r   zDeviceNameHypothesis.lookupý   s;   ú€ ô œg×/Ñ/Ó0ˆÝK¨c×.AÑ.AÀ'Ó.JÔKˆÜÑ9 CÔ9Ó9Ð9r   N)r%   r&   r'   r(   r,   rF   r   r   r   r   r   r`   r`   Ý   sE   „ ñð ñFó ðFð ñó ðð ñ:ó ñ:r   r`   c                   óX   — e Zd ZdZg d¢Zed„ «       Zed„ «       Zed„ «       Zed„ «       Z	y)Ú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/vgc                 ó|   — |j                  «       }d„ |D «       }d„ |D «       }t        t        d„ |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
        c              3   óL   K  — | ]  }t        |j                  «      sŒ|–— Œ y ­wrI   )ÚlistÚdevice_linksrk   s     r   rM   z5DeviceFileHypothesis.get_link_dirs.<locals>.<genexpr>.  s   è ø€ ÒI A´D¸¿¹Õ4HœaÑIùs   ‚$�$c              3   óB   K  — | ]  }|j                   D ]  }|–— Œ Œ y ­wrI   )r|   )rK   rl   Úls      r   rM   z5DeviceFileHypothesis.get_link_dirs.<locals>.<genexpr>/  s    è ø€ ÒG�q¸¿¹ÒG°1”ÐG�ÑGùs   ‚c              3   óZ   K  — | ]#  }t         j                  j                  |«      –— Œ% y ­wrI   )r5   rD   Údirname)rK   r~   s     r   rM   z5DeviceFileHypothesis.get_link_dirs.<locals>.<genexpr>0  s   è ø€ Ò<°œ"Ÿ'™'Ÿ/™/¨!×,Ñ<ùs   ‚)+)Úlist_devicesÚsortedÚset)r   r   ÚdevicesÚdevices_with_linksÚlinkss        r   Úget_link_dirsz"DeviceFileHypothesis.get_link_dirs!  s>   € ð ×&Ñ&Ó(ˆÙI¨ÔIÐÙGÐ.ÔGˆÜ”cÑ<°eÔ<Ó<Ó=Ð=r   c                 ó0   — | j                  |«      | _        y)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)r‡   Ú
_LINK_DIRSr    s     r   r!   zDeviceFileHypothesis.setup2  s   € ð ×*Ñ*¨7Ó3ˆ�r   c                 ó   — |S rI   r   r   s     r   r   zDeviceFileHypothesis.match=  s   € àˆr   c                 óê   ‡‡‡— t        t        j                  «      Šd‰v r! ‰‰‰«      }|�t        |f«      S t        «       S ˆfd„| j                  D «       }ˆˆfd„|D «       }t        d„ |D «       «      S )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.
        ú/c              3   ó^   •K  — | ]$  }t         j                  j                  |‰«      –— Œ& y ­wrI   rf   )rK   Úldr   s     €r   rM   z.DeviceFileHypothesis.lookup.<locals>.<genexpr>T  s    øè ø€ Ò@¨2”—‘—‘˜b #×&Ñ@ùrh   c              3   ó0   •K  — | ]  } ‰‰|«      –— Œ y ­wrI   r   )rK   Úfr   r
   s     €€r   rM   z.DeviceFileHypothesis.lookup.<locals>.<genexpr>U  s   øè ø€ Ò3¨‘4˜ ×#Ñ3ùs   ƒc              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrI   r   rk   s     r   rM   z.DeviceFileHypothesis.lookup.<locals>.<genexpr>V  s   è ø€ Ò=˜q¨q©}œÑ=ùrT   )r   r   Úfrom_device_filer#   r‰   )r   r   r   ÚdeviceÚfilesr„   r
   s    ``   @r   r   zDeviceFileHypothesis.lookupA  sj   ú€ ô œg×6Ñ6Ó7ˆØ�#‰:Ù˜' 3Ó'ˆFØ+1Ð+=”9˜f˜YÓ'ÐNÄ9Ã;ÐNã@°·±Ô@ˆÜ3¨UÔ3ˆÜÑ= GÔ=Ó=Ð=r   N)
r%   r&   r'   r(   r‰   r,   r‡   r!   r   r   r   r   r   rx   rx     s^   „ ñò€Jð ñ>ó ð>ð  ñ4ó ð4ð ñó ðð ñ>ó ñ>r   rx   c                   ó.   — e Zd ZdZeeeegZd„ Z	d„ Z
d„ Zy)Ú	Discoveryz1
    Provides discovery methods for devices.
    c                 ó&   — | j                   | _        y rI   )Ú_HYPOTHESESÚ_hypotheses)Úselfs    r   Ú__init__zDiscovery.__init__f  s   € Ø×+Ñ+ˆÕr   c                 óH   — | j                   D ]  }|j                  |«       Œ y)z
        Set up individual hypotheses.

        May be an expensive call.

        :param Context context: the context
        N)r™   r!   )rš   r   Úhyps      r   r!   zDiscovery.setupi  s%   € ð ×#Ñ#ò 	ˆCØ�I‰I�gÕñ	r   c                 óD   ‡‡— t        ˆˆfd„| j                  D «       «      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`
        c              3   óP   •K  — | ]  }|j                  ‰‰«      D ]  }|–— Œ Œ y ­wrI   )r$   )rK   Úhrl   r   r   s      €€r   rM   z(Discovery.get_devices.<locals>.<genexpr>}  s3   øè ø€ ò 
Ø°·±¸wÈÓ1Nò
Ø,-ŒAð
Øñ
ùs   ƒ#&)r#   r™   )rš   r   r   s    ``r   r$   zDiscovery.get_devicest  s%   ù€ ô ô 
Ø×'Ñ'ô
ó 
ð 	
r   N)r%   r&   r'   r(   rx   r`   r/   rY   r˜   r›   r!   r$   r   r   r   r–   r–   Y  s+   „ ñð
 	ØØØð	€Kò,ò	ó
r   r–   )r(   r)   r   r5   r3   Úpyudev._errorsr   Úpyudev.devicer   r   Úobjectr   r/   rY   r`   rx   r–   r   r   r   ú<module>r¤      sy   ðñ"ó Û Û 	Û 	õ /Ý !òô(<L�ô <Lô~E:˜Zô E:ôPE˜:ô Eô:,:˜:ô ,:ô^J>˜:ô J>ôZ&
�õ &
r   