Skip to content

target: make _get_driver() prioritize drivers named "default" #1163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3162,6 +3162,9 @@ To bind the correct driver to the correct resource, explicit ``name`` and
The property name for the binding (e.g. `port` in the example above) is
documented for each individual driver in this chapter.

If name is set to "default", this resource/driver will be used when no
specific constraint is given.

The YAML configuration file also supports templating for some substitutions,
these are:

Expand Down
9 changes: 9 additions & 0 deletions labgrid/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,27 @@ def _get_driver(self, cls, *, name=None, activate=True, active=False):

found = []
other_names = []
default = None
if isinstance(cls, str):
cls = target_factory.class_from_string(cls)

for drv in self.drivers:
if not isinstance(drv, cls):
continue
if drv.name == "default":
default = drv
if name and drv.name != name:
other_names.append(drv.name)
continue
if active and drv.state != BindingState.active:
continue
found.append(drv)

# if no explicit driver name is requested and a "default" driver was saved and
# multiple drivers were found, use the default driver
if not name and default and len(found) != 1:
found = [default]

if not found:
name_msg = f" named '{name}'" if name else ""
if other_names:
Expand Down
38 changes: 38 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,44 @@ def test_create_multi_drivers(self, tmpdir):
assert d_a.port is r_a
assert d_b.port is r_b

def test_create_multi_drivers_with_default(self, tmpdir):
p = tmpdir.join("config.yaml")
p.write(
"""
targets:
test1:
resources:
- RawSerialPort:
name: "serial_a"
port: "/dev/ttyUSB0"
speed: 115200
- RawSerialPort:
name: "default"
port: "/dev/ttyUSB0"
speed: 115200
drivers:
- SerialDriver:
name: "serial_a"
bindings:
port: "serial_a"
- SerialDriver:
name: "default"
bindings:
port: "default"
"""
)
e = Environment(str(p))
t = e.get_target("test1")
r_a = t.get_resource(RawSerialPort, name="serial_a")
r_b = t.get_resource(RawSerialPort)
assert r_a is not r_b
d_a = t.get_driver(ConsoleProtocol, name="serial_a", activate=False)
d_b = t.get_driver(ConsoleProtocol, activate=False)
assert d_a is not d_b

assert d_a.port is r_a
assert d_b.port is r_b

def test_usbserialport_warning(self, tmpdir):
p = tmpdir.join("config.yaml")
p.write(
Expand Down
13 changes: 13 additions & 0 deletions tests/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ class A(Driver):
assert target.get_driver(A) is a
assert target.get_driver(A, name="adriver") is a

# make sure drivers named "default" are prioritized
b = A(target, "default")
assert target.get_driver(A) is b
assert target.get_driver(A, name="adriver") is a

def test_get_driver_multiple_no_default(target):
class A(Driver):
pass

a = A(target, "adriver")
b = A(target, "default")
with pytest.raises(NoDriverFoundError) as excinfo:
target.get_driver(A, name="nosuchdriver")

def test_getitem(target):
class AProtocol(abc.ABC):
Expand Down