3 u1‘Wô,ã@sìdZddlmZddlmZddlmZddlmZddlZddlZddlZddl Z ddl Z ddl m Z ddl m Z d d „Ze jejƒ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)ÚDevices)ÚDeviceNotFoundErrorcstjˆƒ‡fdd„ƒ}|S)z\ Allow Device discovery methods to return None instead of raising an exception. c s$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..the_func)Ú functoolsÚwraps)r r r )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` N)r)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$|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)rrrrr r r Ú get_devicesns zHypothesis.get_devicesN) Ú__name__Ú __module__Ú __qualname__Ú__doc__Ú classmethodÚabcÚabstractmethodrrrrr r r r 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\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&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\d+)$Znumber)r&r'rr*r+)rrZ number_rerr r r Ú _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-)rrr r r 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.r r r Ú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 rr r ú Ãsz0DeviceNumberHypothesis.lookup..css|]}|dk r|VqdS)Nr )r3Úrr r r r5Äs)rrZfrom_device_numberr2r)rrrÚresr )rr rr r¸s zDeviceNumberHypothesis.lookupN) rrrrr r,r-rr2rr r r r 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 )rrr r r 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)rrrr7r r r rÖs zDevicePathHypothesis.lookupN)rrrrr rrr r r r 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.r r r5ösz7DeviceNameHypothesis.find_subsystems..css|]}tjj|ƒr|VqdS)N)r(r0Úisdir)r3Údr r r r5÷scss"|]}tj|ƒD] }|VqqdS)N)r(r/)r3r?Únr r r 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 )rrr r r 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 rr r r5sz.DeviceNameHypothesis.lookup..css|]}|dk r|VqdS)Nr )r3r6r r r r5s)rrÚ from_namer2r)rrrr7r )rr rr rs zDeviceNameHypothesis.lookupN)rrrrr r2rrr r r r r9äs  r9c @sZeZdZdZdddddddd d d d g Zed 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?r r r r55sz5DeviceFileHypothesis.get_link_dirs..css|]}|jD] }|Vq qdS)N)rD)r3r?Úlr r r r56scss|]}tjj|ƒVqdS)N)r(r0Údirname)r3rEr r r r57s)Z list_devicesÚsortedÚset)rrÚdevicesZdevices_with_linksZlinksr r r Ú 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)rrr r r r9s zDeviceFileHypothesis.setupcCs|S)Nr )rrr r r 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)rr r r5[sz.DeviceFileHypothesis.lookup..c3s|]}ˆˆ|ƒVqdS)Nr )r3Úf)rr r r r5\scss|]}|dk r|VqdS)Nr )r3r?r r r r5]s)rrZfrom_device_filerrK)rrrZdeviceÚfilesrIr )rr rr rHs  zDeviceFileHypothesis.lookupN) rrrrrKr rJrrrr r r r rBs   rBc@s4eZdZdZeeeegZdd„Z dd„Z dd„Z dS) Ú Discoveryz1 Provides discovery methods for devices. cCs |j|_dS)N)Ú _HYPOTHESESÚ _hypotheses)Úselfr r r Ú__init__mszDiscovery.__init__cCsx|jD]}|j|ƒqWdS)z Set up individual hypotheses. May be an expensive call. :param Context context: the context N)rQr)rRrZhypr r r 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?)rrr r r5…sz(Discovery.get_devices..)rrQ)rRrrr )rrr r{s zDiscovery.get_devicesN) rrrrrBr9r#r8rPrSrrr r r r rO`s rO)rZ __future__rrrrr!rr(r&ZsixZ pyudev.devicerrrZ add_metaclassÚABCMetaÚobjectrr#r8r9rBrOr r r r Ús&      =K/M