@@ -533,9 +533,6 @@ class Device(Model):
533
533
#: Protocols supported by this device (ex the HPGLProtocol)
534
534
protocols = List (extensions .DeviceProtocol )
535
535
536
- #: Transports supported by this device (ex the SerialPort
537
- transports = List (extensions .DeviceTransport )
538
-
539
536
#: Filters that this device applies to the output
540
537
filters = List (DeviceFilter ).tag (config = True )
541
538
@@ -571,6 +568,9 @@ class Device(Model):
571
568
status = Str ()
572
569
573
570
def __init__ (self , * args , ** kwargs ):
571
+ transports = None
572
+ if 'transports' in kwargs :
573
+ transports = kwargs .pop ('transports' )
574
574
super (Model , self ).__init__ (* args , ** kwargs )
575
575
(w , h ) = kwargs .get ('width' ), kwargs .get ('height' )
576
576
if w :
@@ -580,17 +580,46 @@ def __init__(self, *args, **kwargs):
580
580
if not h :
581
581
h = 900000
582
582
self .config .area .size [1 ] = h
583
+ if not kwargs .get ('connection' ):
584
+ self .connection = self ._make_default_connection (transports )
583
585
584
- def _default_connection (self ):
586
+ def _make_default_connection (self , transports ):
585
587
""" If no connection is set when the device is created,
586
588
create one using the first "connection" type the driver supports.
587
589
"""
588
- if not self .transports :
590
+
591
+ if not transports or not self .declaration :
592
+ return TestTransport (declaration = extensions .DeviceTransport ())
593
+ preferred_decl = None
594
+ # prioritize available connections based on the order in device manifest
595
+ for supported_transform in self .declaration .connections :
596
+ if preferred_decl :
597
+ break
598
+ for transport in transports :
599
+ if transport .id == supported_transform or transport .category == supported_transform :
600
+ preferred_decl = transport
601
+ break
602
+ if not preferred_decl :
603
+ for transport in transports :
604
+ if self .supports_transport (transport ):
605
+ preferred_decl = transport
606
+ break
607
+ if not preferred_decl :
589
608
return TestTransport ()
590
- declaration = self . transports [ 0 ]
609
+
591
610
driver = self .declaration
592
611
protocol = self ._default_protocol ()
593
- return declaration .factory (driver , declaration , protocol )
612
+ return preferred_decl .factory (driver , preferred_decl , protocol )
613
+
614
+ def supports_transport (self , transport_manifest ):
615
+ if not self .declaration or not self .declaration .connections :
616
+ return True
617
+ conections = self .declaration .connections
618
+ if transport_manifest .id in conections or \
619
+ transport_manifest .id == "disk" or \
620
+ (transport_manifest .category and transport_manifest .category in conections ):
621
+ return True
622
+ return False
594
623
595
624
def _default_protocol (self ):
596
625
""" Create the protocol for this device. """
@@ -1273,17 +1302,13 @@ def get_device_from_driver(self, driver, config=None):
1273
1302
and processing the jobs.
1274
1303
1275
1304
"""
1276
- # Set the protocols based on the declaration
1277
- transports = [t for t in self .transports
1278
- if not driver .connections or t .id == 'disk' or
1279
- t .id in driver .connections ]
1280
1305
1281
1306
# Set the protocols based on the declaration
1282
1307
protocols = [p for p in self .protocols
1283
1308
if not driver .protocols or p .id in driver .protocols ]
1284
1309
1285
1310
# Generate the device
1286
- return driver .factory (driver , transports , protocols , config )
1311
+ return driver .factory (driver , self . transports , protocols , config )
1287
1312
1288
1313
# -------------------------------------------------------------------------
1289
1314
# Device Extensions API
0 commit comments