a 8g+^w,ã@sìdZddlmZddlmZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl m Z ddl mZd d „Ze  ej¡Gd d „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 é)Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN©ÚDeviceNotFoundError)ÚDevicescst ˆ¡‡fdd„ƒ}|S)z\ Allow Device discovery methods to return None instead of raising an exception. cs*zˆ|i|¤ŽWSty$YdS0dS)z‘ Returns result of calling ``func`` on ``args``, ``kwargs``. Returns None if ``func`` raises :exc:`DeviceNotFoundError`. Nr)ÚargsÚkwargs©Úfunc©ú3/usr/lib/python3.9/site-packages/pyudev/discover.pyÚthe_func0s z wrap_exception..the_func)Ú functoolsÚwraps)r rr r rÚwrap_exception*s rc@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Úvaluer r rÚ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` Nr)rÚcontextÚkeyr r rÚ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 ©rrr r rÚsetupcs zHypothesis.setupcCs$| |¡}|dur| ||¡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)rrrrr r rÚ get_devicesns zHypothesis.get_devicesN) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú classmethodÚabcÚabstractmethodrrrr r r r rr>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. cCs8t d¡}| |¡}|o6t t| d¡ƒt| 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\d+)(\D+)(?P\d+)$ÚmajorÚminor)ÚreÚcompilerÚosÚmakedevÚintÚgroup)rrZmajor_minor_rerr r rÚ_match_major_minorƒs  ÿz)DeviceNumberHypothesis._match_major_minorcCs&t d¡}| |¡}|o$t| 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\d+)$Znumber)r+r,rr/r0)rrZ number_rerr r rÚ _match_number‘s  z$DeviceNumberHypothesis._match_numbercCs| |¡p| |¡S)z  Match the number under the assumption that it is a device number. :returns: the device number or None :rtype: int or NoneType )r1r2rr r rržszDeviceNumberHypothesis.matchcCs|j}t tj |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_pathr-ÚlistdirÚpathÚjoin)rrr4r r rÚfind_subsystems¨s z&DeviceNumberHypothesis.find_subsystemscs8ttjƒ‰‡‡‡fdd„| ˆ¡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 rr rÚ ¿óz0DeviceNumberHypothesis.lookup..css|]}|dur|VqdSr9r ©r;Úrr r rr>Àr?)rrZfrom_device_numberr8r©rrrÚresr r=rr´s zDeviceNumberHypothesis.lookupN) r!r"r#r$r%r1r2rr8rr r r rr(|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 rr r rrÈszDevicePathHypothesis.matchcCs(ttjƒ||ƒ}|dur"t|fƒStƒ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` N)rrZ from_pathrrBr r rrÒs zDevicePathHypothesis.lookupN)r!r"r#r$r%rrr r r rrDÃs  rDc@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 )ZbusÚclassZ subsystemc3s|]}tj ˆ|¡VqdSr9©r-r6r7)r;Úname©r4r rr>òr?z7DeviceNameHypothesis.find_subsystems..css|]}tj |¡r|VqdSr9)r-r6Úisdir©r;Údr r rr>ór?css"|]}t |¡D] }|VqqdSr9)r-r5)r;rMÚnr r rr>ôr?)r4r)rrÚdirnamesZabsnamesZ realnamesr rJrr8ç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 rr r rröszDeviceNameHypothesis.matchcs8ttjƒ‰‡‡‡fdd„| ˆ¡Dƒ}tdd„|DƒƒS)rEc3s|]}ˆˆ|ˆƒVqdSr9r r:r=r rr> r?z.DeviceNameHypothesis.lookup..css|]}|dur|VqdSr9r r@r r rr> r?)rrÚ from_namer8rrBr r=rrs zDeviceNameHypothesis.lookupN)r!r"r#r$r%r8rrr r r rrFàs  rFc@sHeZdZdZgd¢Zedd„ƒZedd„ƒZedd„ƒZed d „ƒZ d S) Ú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:| ¡}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|VqdSr9)ÚlistÚ device_linksrLr r rr>*r?z5DeviceFileHypothesis.get_link_dirs..css|]}|jD] }|Vq qdSr9)rS)r;rMÚlr r rr>+r?css|]}tj |¡VqdSr9)r-r6Údirname)r;rTr r rr>,r?)Z list_devicesÚsortedÚset)rrÚdevicesZdevices_with_linksZlinksr r rÚ get_link_dirss z"DeviceFileHypothesis.get_link_dirscCs| |¡|_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)rYÚ _LINK_DIRSrr r rr.s zDeviceFileHypothesis.setupcCs|Sr9r rr r rr9szDeviceFileHypothesis.matchcsnttjƒ‰dˆvr4ˆˆˆƒ}|dur.t|fƒStƒS‡fdd„|jDƒ}‡‡fdd„|Dƒ}tdd„|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. ú/Nc3s|]}tj |ˆ¡VqdSr9rH)r;Zld)rr rr>Pr?z.DeviceFileHypothesis.lookup..c3s|]}ˆˆ|ƒVqdSr9r )r;Úf)rr r rr>Qr?css|]}|dur|VqdSr9r rLr r rr>Rr?)rrZfrom_device_filerrZ)rrrZdeviceÚfilesrXr r=rr=s  zDeviceFileHypothesis.lookupN) r!r"r#r$rZr%rYrrrr r r rrQs   rQc@s4eZdZdZeeeegZdd„Z dd„Z dd„Z dS) Ú Discoveryz1 Provides discovery methods for devices. cCs |j|_dSr9)Ú _HYPOTHESESÚ _hypotheses)Úselfr r rÚ__init__`szDiscovery.__init__cCs|jD]}| |¡qdS)z Set up individual hypotheses. May be an expensive call. :param Context context: the context N)r`r)rarZhypr r rrcs 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$|]}| ˆˆ¡D] }|VqqdSr9)r )r;ÚhrM©rrr rr>wsz(Discovery.get_devices..)rr`)rarrr rdrr ns ÿzDiscovery.get_devicesN) r!r"r#r$rQrFr(rDr_rbrr r r r rr^Usþ r^)r$Z __future__rrrrr&rr-r+ZsixZpyudev._errorsrZ pyudev.devicerrZ add_metaclassÚABCMetaÚobjectrr(rDrFrQr^r r r rÚs&       =G/F