Better handling if invalid known_devices.csv encountered.

This commit is contained in:
Paulus Schoutsen 2013-10-06 16:54:46 -07:00
parent 59fa58beaf
commit 4ce550b432

View File

@ -89,8 +89,12 @@ class DeviceTracker(object):
self.lock = threading.Lock()
# Dictionary to keep track of known devices and devices we track
self.known_devices = {}
# Did we encounter a valid known devices file
self.invalid_known_devices_file = False
# Read known devices if file exists
if os.path.isfile(KNOWN_DEVICES_FILE):
with open(KNOWN_DEVICES_FILE) as inp:
@ -100,6 +104,7 @@ class DeviceTracker(object):
# so we can ensure we have unique categories.
used_categories = []
try:
for row in csv.DictReader(inp):
device = row['device']
@ -127,6 +132,9 @@ class DeviceTracker(object):
self.known_devices[device]['category'] = category
used_categories.append(category)
except KeyError:
self.invalid_known_devices_file = False
self.logger.warning("Invalid {} found. We won't update it with new found devices.".format(KNOWN_DEVICES_FILE))
if len(self.device_state_categories()) == 0:
self.logger.warning("No devices to track. Please update {}.".format(KNOWN_DEVICES_FILE))
@ -169,6 +177,8 @@ class DeviceTracker(object):
self.statemachine.set_state(STATE_CATEGORY_ALL_DEVICES, all_devices_state)
# If we come along any unknown devices we will write them to the known devices file
# but only if we did not encounter an invalid known devices file
if not self.invalid_known_devices_file:
unknown_devices = [device for device in found_devices if device not in self.known_devices]
if len(unknown_devices) > 0: