pyudev.discover module

Tools to discover a device given limited information.

class pyudev.discover.DeviceFileHypothesis

Bases: Hypothesis

Discover the device assuming the value is some portion of a device file.

The device file may be a link to a device node.

Get all directories that may contain links to device nodes.

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

Parameters:

context (Context) – the context

Returns:

a sorted list of directories that contain device links

Return type:

list

classmethod lookup(context, key)

Lookup the device under the assumption that the key is part of the name of a device file.

Parameters:
  • context (Context) – the context

  • key (str) – 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.

classmethod match(value)

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.

Parameters:

value (str) – the string to inspect

Returns:

the matched thing or None if unmatched

Return type:

the type of lookup’s key parameter or NoneType

classmethod setup(context)

Set the link directories to be used when discovering by file.

Uses get_link_dirs, so is as expensive as it is.

Parameters:

context (Context) – the context

class pyudev.discover.DeviceNameHypothesis

Bases: Hypothesis

Discover the device assuming the input is a device name.

Try every available subsystem.

classmethod find_subsystems(context)

Find all subsystems in sysfs.

Parameters:

context (Context) – the context

Return type:

frozenset

Returns:

subsystems in sysfs

classmethod lookup(context, key)

Lookup by the path.

Parameters:
  • context (Context) – the context

  • key (str) – the device path

Returns:

a list of matching devices

Return type:

frozenset of Device

classmethod match(value)

Match value under the assumption that it is a device name.

Returns:

the device path or None

Return type:

str or NoneType

class pyudev.discover.DeviceNumberHypothesis

Bases: Hypothesis

Represents the hypothesis that the device is a device number.

The device may be separated into major/minor number or a composite number.

classmethod find_subsystems(context)

Find subsystems in /sys/dev.

Parameters:

context (Context) – the context

Returns:

a lis of available subsystems

Return type:

list of str

classmethod lookup(context, key)

Lookup by the device number.

Parameters:
  • context (Context) – the context

  • key (int) – the device number

Returns:

a list of matching devices

Return type:

frozenset of Device

classmethod match(value)

Match the number under the assumption that it is a device number.

Returns:

the device number or None

Return type:

int or NoneType

class pyudev.discover.DevicePathHypothesis

Bases: Hypothesis

Discover the device assuming the identifier is a device path.

classmethod lookup(context, key)

Lookup by the path.

Parameters:
  • context (Context) – the context

  • key (str) – the device path

Returns:

a list of matching devices

Return type:

frozenset of Device

classmethod match(value)

Match value under the assumption that it is a device path.

Returns:

the device path or None

Return type:

str or NoneType

class pyudev.discover.Discovery

Bases: object

Provides discovery methods for devices.

get_devices(context, value)

Get the devices corresponding to value.

Parameters:
  • context (Context) – the context

  • value (str) – some identifier of the device

Returns:

a list of corresponding devices

Return type:

frozenset of Device

setup(context)

Set up individual hypotheses.

May be an expensive call.

Parameters:

context (Context) – the context

class pyudev.discover.Hypothesis

Bases: object

Represents a hypothesis about the meaning of the device identifier.

classmethod get_devices(context, value)

Get any devices that may correspond to the given string.

Parameters:
  • context (Context) – the pyudev context

  • value (str) – the value to look for

Returns:

a list of devices obtained

Return type:

set of Device

abstract classmethod lookup(context, key)

Lookup the given string according to the hypothesis.

Parameters:
  • context (Context) – the pyudev context

  • key (the type of match's return value if not None) – a key with which to lookup the device

Returns:

a list of Devices obtained

Return type:

frozenset of Device

abstract classmethod match(value)

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.

Parameters:

value (str) – the string to inspect

Returns:

the matched thing or None if unmatched

Return type:

the type of lookup’s key parameter or NoneType

classmethod setup(context)

A potentially expensive method that may allow an Hypothesis to find devices more rapidly or to find a device that it would otherwise miss.

Parameters:

context (Context) – the pyudev context

pyudev.discover.wrap_exception(func)

Allow Device discovery methods to return None instead of raising an exception.