1515import itertools
1616from collections import namedtuple , defaultdict , OrderedDict
1717import threading
18- from concurrent .futures import Future , ThreadPoolExecutor
19-
18+ from concurrent .futures import ThreadPoolExecutor
19+ import queue
2020import typing
2121import hwPortUtils
2222import braille
23- import winKernel
2423import winUser
2524from logHandler import log
2625import config
@@ -239,14 +238,18 @@ class Detector(object):
239238 This should only be used by the L{braille} module.
240239 """
241240
242- def __init__ (self , usb = True , bluetooth = True , limitToDevices = None ):
241+ def __init__ (
242+ self ,
243+ usb : bool = True ,
244+ bluetooth : bool = True ,
245+ limitToDevices : typing .Optional [typing .List [str ]] = None
246+ ):
243247 """Constructor.
244248 The keyword arguments initialize the detector in a particular state.
245- On an initialized instance, these initial arguments can be overridden by calling L{_queueBgScan} or L{rescan}.
249+ On an initialized instance, these initial arguments can be overridden by calling
250+ L{_queueBgScan} or L{rescan}.
246251 @param usb: Whether this instance should detect USB devices initially.
247- @type usb: bool
248252 @param bluetooth: Whether this instance should detect Bluetooth devices initially.
249- @type bluetooth: bool
250253 @param limitToDevices: Drivers to which detection should be limited initially.
251254 C{None} if no driver filtering should occur.
252255 """
@@ -263,17 +266,20 @@ def __init__(self, usb=True, bluetooth=True, limitToDevices=None):
263266 self ._queueBgScan (usb = usb , bluetooth = bluetooth , limitToDevices = limitToDevices )
264267
265268 @property
266- def _scanQueued (self ):
269+ def _scanQueued (self ) -> bool :
267270 return not self ._executor ._work_queue .empty ()
268271
269- def _queueBgScan (self , usb = False , bluetooth = False , limitToDevices = None ):
272+ def _queueBgScan (
273+ self ,
274+ usb : bool = False ,
275+ bluetooth : bool = False ,
276+ limitToDevices : typing .Optional [typing .List [str ]] = None
277+ ):
270278 """Queues a scan for devices.
271279 If a scan is already in progress, a new scan will be queued after the current scan.
272280 To explicitely cancel a scan in progress, use L{rescan}.
273281 @param usb: Whether USB devices should be detected for this and subsequent scans.
274- @type usb: bool
275282 @param bluetooth: Whether Bluetooth devices should be detected for this and subsequent scans.
276- @type bluetooth: bool
277283 @param limitToDevices: Drivers to which detection should be limited for this and subsequent scans.
278284 C{None} if no driver filtering should occur.
279285 """
@@ -287,11 +293,29 @@ def _stopBgScan(self):
287293 """Stops the current scan as soon as possible and prevents a queued scan to start."""
288294 self ._stopEvent .set ()
289295 # Cancel queued scans
290- while self ._scanQueued :
291- workItem = self ._executor ._work_queue .get_nowait ()
292- workItem .future .cancel ()
293-
294- def _bgScan (self , detectUsb , detectBluetooth , limitToDevices ):
296+ try :
297+ while self ._scanQueued :
298+ workItem = self ._executor ._work_queue .get_nowait ()
299+ workItem .future .cancel ()
300+ except queue .Empty :
301+ pass
302+
303+ # C901 '_bgScan' is too complex
304+ # Note: when working on _bgScan, look for opportunities to simplify
305+ # and move logic out into smaller helper functions.
306+ def _bgScan ( # noqa: C901
307+ self ,
308+ detectUsb : bool ,
309+ detectBluetooth : bool ,
310+ limitToDevices : typing .Optional [typing .List [str ]]
311+ ):
312+ """Performs the actual background scan.
313+ this function should be run on a background thread.
314+ @param usb: Whether USB devices should be detected for this particular scan.
315+ @param bluetooth: Whether Bluetooth devices should be detected for this particular scan.
316+ @param limitToDevices: Drivers to which detection should be limited for this scan.
317+ C{None} if no driver filtering should occur.
318+ """
295319 # Clear the stop event before a scan is started.
296320 # Since a scan can take some time to complete, another thread can set the stop event to cancel it.
297321 self ._stopEvent .clear ()
@@ -301,7 +325,7 @@ def _bgScan(self, detectUsb, detectBluetooth, limitToDevices):
301325 for driver , match in getDriversForConnectedUsbDevices ():
302326 if self ._stopEvent .isSet ():
303327 return
304- if (self . _limitToDevices and driver not in self . _limitToDevices ):
328+ if (limitToDevices and driver not in limitToDevices ):
305329 continue
306330 if braille .handler .setDisplayByName (driver , detected = match ):
307331 return
@@ -319,7 +343,7 @@ def _bgScan(self, detectUsb, detectBluetooth, limitToDevices):
319343 for driver , match in btDevs :
320344 if self ._stopEvent .isSet ():
321345 return
322- if (self ._limitToDevices and driver not in self . _limitToDevices ):
346+ if (self ._limitToDevices and driver not in limitToDevices ):
323347 continue
324348 if btDevsCache is not btDevs :
325349 btDevsCache .append ((driver , match ))
0 commit comments