def load_data(self, filename): """ Loads the pairing data of the controller from a file. :param filename: the file name of the pairing data :raises ConfigLoadingError: if the config could not be loaded. The reason is given in the message. :raises TransportNotSupportedError: if the dependencies for the selected transport are not installed """ try: with open(filename, 'r') as input_fp: data = json.load(input_fp) for pairing_id in data: if 'Connection' not in data[pairing_id]: # This is a pre BLE entry in the file with the pairing data, hence it is for an IP based # accessory. So we set the connection type (in case save data is used everything will be fine) # and also issue a warning data[pairing_id]['Connection'] = 'IP' self.logger.warning( 'Loaded pairing for %s with missing connection type. Assume this is IP based.', pairing_id) if data[pairing_id]['Connection'] == 'IP': if not IP_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('IP') self.pairings[pairing_id] = IpPairing(data[pairing_id]) elif data[pairing_id]['Connection'] == 'BLE': if not BLE_TRANSPORT_SUPPORTED: raise TransportNotSupportedError('BLE') self.pairings[pairing_id] = BlePairing( data[pairing_id], self.ble_adapter) elif data[pairing_id][ 'Connection'] == 'ADDITIONAL_PAIRING': self.pairings[pairing_id] = AdditionalPairing( data[pairing_id]) else: # ignore anything else, issue warning self.logger.warning( 'could not load pairing %s of type "%s"', pairing_id, data[pairing_id]['Connection']) except PermissionError: raise ConfigLoadingError( 'Could not open "{f}" due to missing permissions'.format( f=filename)) except JSONDecodeError: raise ConfigLoadingError( 'Cannot parse "{f}" as JSON file'.format(f=filename)) except FileNotFoundError: raise ConfigLoadingError( 'Could not open "{f}" because it does not exist'.format( f=filename))
def load_data(self, filename): """ Loads the pairing data of the controller from a file. :param filename: the file name of the pairing data :raises ConfigLoadingError: if the config could not be loaded. The reason is given in the message. """ try: with open(filename, 'r') as input_fp: data = json.load(input_fp) for pairing_id in data: self.pairings[pairing_id] = Pairing(data[pairing_id]) except PermissionError as e: raise ConfigLoadingError('Could not open "{f}" due to missing permissions'.format(f=filename)) except JSONDecodeError as e: raise ConfigLoadingError('Cannot parse "{f}" as JSON file'.format(f=filename)) except FileNotFoundError as e: raise ConfigLoadingError('Could not open "{f}" because it does not exist'.format(f=filename))