def validate_key(key, dictionary, expected_type, source):
    """Validates if a key exists and its value is the correct type.
    Args:
        key: The key in dictionary.
        dictionary: The dictionary that should contain key.
        expected_type: the type that key's value should have.
        source: The name of the object being checked. Used for error messages.

    Returns:
        The value of dictionary[key] if no error was raised.

    Raises:
        RelayConfigError if the key does not exist, or is not of expected_type.
    """
    if key not in dictionary:
        raise RelayConfigError(MISSING_KEY_ERR_MSG % (key, source, dictionary))
    if expected_type == str:
        if not isinstance(dictionary[key], string_types):
            raise RelayConfigError(
                TYPE_MISMATCH_ERR_MSG %
                (key, dictionary[key], expected_type, dictionary))
    elif not isinstance(dictionary[key], expected_type):
        raise RelayConfigError(
            TYPE_MISMATCH_ERR_MSG %
            (key, dictionary[key], expected_type, dictionary))
    return dictionary[key]
Пример #2
0
    def __init__(self, config):
        self.relays = dict()
        self.boards = dict()
        self.devices = dict()

        validate_key('boards', config, list, 'relay config file')

        for elem in config['boards']:
            board = self.create_relay_board(elem)
            if board.name in self.boards:
                raise RelayConfigError(
                    self.DUPLICATE_ID_ERR_MSG.format('name', elem['name'],
                                                     elem))
            self.boards[board.name] = board

        # Note: 'boards' is a necessary value, 'devices' is not.
        if 'devices' in config:
            for elem in config['devices']:
                relay_device = self.create_relay_device(elem)
                if relay_device.name in self.devices:
                    raise RelayConfigError(
                        self.DUPLICATE_ID_ERR_MSG.format(
                            'name', elem['name'], elem))
                self.devices[relay_device.name] = relay_device
        else:
            device_config = dict()
            device_config['name'] = 'GenericRelayDevice'
            device_config['relays'] = dict()
            for relay_id in self.relays:
                device_config['relays'][relay_id] = relay_id
            self.devices['device'] = self.create_relay_device(device_config)
Пример #3
0
 def __getitem__(self, key):
     try:
         return self._store[key]
     except KeyError:
         raise RelayConfigError(
             self.ERROR_MESSAGE % (key, type(
                 self.relay_device), self.relay_device.name, self._store))
Пример #4
0
    def create_relay_device(self, config):
        """Builds a RelayDevice from the given config.

        When given no 'type' key in the config, the function will default to
        returning a GenericRelayDevice with the relays found in the 'relays'
        array.

        Args:
            config: An object containing 'name', 'relays', and (optionally)
            type.

        Returns:
            A RelayDevice with the given type found in the config. If no type is
            found, it will default to GenericRelayDevice.

        Raises:
            RelayConfigError if the type given does not match any from the
            _device_constructors dictionary.

        """
        if 'type' in config:
            if config['type'] not in RelayRig._device_constructors:
                raise RelayConfigError(
                    'Device with type {} not found. Has it been added '
                    'to the _device_constructors dict?'.format(config['type']))
            else:
                device = self._device_constructors[config['type']](config,
                                                                   self)

        else:
            device = GenericRelayDevice(config, self)

        return device
Пример #5
0
    def ensure_config_contains_relay(self, relay_name):
        """
        Throws an error if the relay does not exist.

        Args:
            relay_name:relay_name to be checked.
        """
        if relay_name not in self.relays:
            raise RelayConfigError(MISSING_RELAY_MSG % (self.name, relay_name))
    def __init__(self, config):
        """Creates a RelayBoard instance. Handles naming and relay creation.

        Args:
            config: A configuration dictionary, usually pulled from an element
            under in "boards" list in the relay rig config file.
        """
        self.name = validate_key('name', config, str, 'config')
        if '/' in self.name:
            raise RelayConfigError('RelayBoard name cannot contain a "/".')
        self.relays = dict()
        for pos in self.get_relay_position_list():
            self.relays[pos] = Relay(self, pos)
Пример #7
0
    def create_relay_board(self, config):
        """Builds a RelayBoard from the given config.

        Args:
            config: An object containing 'type', 'name', 'relays', and
            (optionally) 'properties'. See the example json file.

        Returns:
            A RelayBoard with the given type found in the config.

        Raises:
            RelayConfigError if config['type'] doesn't exist or is not a string.

        """
        validate_key('type', config, str, '"boards" element')
        try:
            ret = self._board_constructors[config['type']](config)
        except LookupError:
            raise RelayConfigError(
                'RelayBoard with type {} not found. Has it been added '
                'to the _board_constructors dict?'.format(config['type']))
        for _, relay in ret.relays.items():
            self.relays[relay.relay_id] = relay
        return ret
    def __init__(self, config, relay_rig):
        """Creates a RelayDevice.

        Args:
            config: The dictionary found in the config file for this device.
            You can add your own params to the config file if needed, and they
            will be found in this dictionary.
            relay_rig: The RelayRig the device is attached to. This won't be
            useful for classes that inherit from RelayDevice, so just pass it
            down to this __init__.
        """
        self.rig = relay_rig
        self.relays = dict()

        validate_key('name', config, str, '"devices" element')
        self.name = config['name']

        relays = validate_key('relays', config, dict, '"devices" list element')
        if len(relays) < 1:
            raise RelayConfigError(
                'Key "relays" must have at least 1 element.')

        for name, relay_id in relays.items():
            self.relays[name] = relay_rig.relays[relay_id]
Пример #9
0
 def _ensure_config_contains_relay(self, relay_name):
     """Throws an error if the relay does not exist."""
     if relay_name not in self.relays:
         raise RelayConfigError(
             MISSING_RELAY_MSG %
             (self.__class__.__name__, self.name, relay_name))