[IMP] hw_drivers: Give a priority order to drivers
Previously, we tested the `supported` methods of all drivers sequentially in alphabetical order of file names. If you wanted to extend a driver, the `supported` method of your driver could never be tested if the one of its parent was tested first. We've now added a priority order to drivers so that by default the extending driver will always be tested before the extended driver. You can still do something similar to what `is_tested_last` did by manually setting the priority of a driver to 0. Ex: The way barcode scanners is really similar to keyboards, but still requires a few modifications in the driver. We couldn't extend the keyboard driver easily because the supported method of barcode scanners had to be more strict than the one of keyboards. If the KeyboardUSBDriver was tested first, scanners were recognized as keyboard and didn't have all the specific features of scanners. TaskID: 2082282
This commit is contained in:
@@ -144,7 +144,7 @@ class Interface(Thread, metaclass=InterfaceMetaClass):
|
||||
|
||||
def __init__(self):
|
||||
super(Interface, self).__init__()
|
||||
self.drivers = [d for d in drivers if d.connection_type == self.connection_type]
|
||||
self.drivers = sorted([d for d in drivers if d.connection_type == self.connection_type], key=lambda d: d.priority, reverse=True)
|
||||
|
||||
def run(self):
|
||||
while self.connection_type and self.drivers:
|
||||
@@ -179,11 +179,11 @@ class Interface(Thread, metaclass=InterfaceMetaClass):
|
||||
class DriverMetaClass(type):
|
||||
def __new__(cls, clsname, bases, attrs):
|
||||
newclass = super(DriverMetaClass, cls).__new__(cls, clsname, bases, attrs)
|
||||
# Some drivers must be tried only when all the others have been ruled out. These are kept at the bottom of the list.
|
||||
if newclass.is_tested_last:
|
||||
drivers.append(newclass)
|
||||
if hasattr(newclass, 'priority'):
|
||||
newclass.priority += 1
|
||||
else:
|
||||
drivers.insert(0, newclass)
|
||||
newclass.priority = 0
|
||||
drivers.append(newclass)
|
||||
return newclass
|
||||
|
||||
class Driver(Thread, metaclass=DriverMetaClass):
|
||||
@@ -191,7 +191,6 @@ class Driver(Thread, metaclass=DriverMetaClass):
|
||||
Hook to register the driver into the drivers list
|
||||
"""
|
||||
connection_type = ""
|
||||
is_tested_last = False
|
||||
|
||||
def __init__(self, device):
|
||||
super(Driver, self).__init__()
|
||||
|
||||
@@ -247,7 +247,7 @@ class AdamEquipmentDriver(ScaleDriver):
|
||||
"""Driver for the Adam Equipment serial scale."""
|
||||
|
||||
_protocol = ADAMEquipmentProtocol
|
||||
is_tested_last = True
|
||||
priority = 0 # Test the supported method of this driver last, after all other serial drivers
|
||||
|
||||
def __init__(self, device):
|
||||
super().__init__(device)
|
||||
|
||||
Reference in New Issue
Block a user