Exemple #1
0
    def register_events(self,
                        func,
                        events,
                        sender_uids=None,
                        names=None,
                        weak=True):
        """Registers an event for a given sender UID or name

        Args:
            func (func): Callback to register
            events (iterable(WalkoffEvent)): Events to register the callback to
            sender_uids (str|iterable(str), optional): UIDs to register the callback to. Defaults to None
            names (str|iterable(str), optional): The names to register the callback to. Defaults to None
            weak (bool, optional): Should the callback be registered with a weak reference? Defaults to true
        """
        if sender_uids is None:
            sender_uids = []
        sender_uids = convert_to_iterable(sender_uids)
        if names is None:
            names = []
        names = convert_to_iterable(names)
        entry_ids = set(sender_uids) | set(names)
        if not entry_ids:
            raise ValueError('Either sender_uid or name must specified')
        for entry_id in entry_ids:
            self.__register_entry(entry_id, events, func, weak)
Exemple #2
0
    def validate_app_actions(app, actions):
        """Validates that an app's actions are valid, meaning that they exist in a defined app API

        Args:
            app (str): The app to validate
            actions (str): The action to validate

        Returns:
            set(str): The actions. Expanded to all known actions if `action` was 'all'

         Raises:
            UnknownApp: If the app specified is not found in all known app APIs or the app has no actions
            UnknownAppAction: If the action is not found in the give app's actions
        """
        try:
            available_actions = set(walkoff.config.app_apis[app]['actions'].keys())
            if actions == 'all':
                return available_actions
            actions = set(convert_to_iterable(actions))
            if actions - available_actions:
                message = 'Unknown actions for app {0}: {1}'.format(app, list(set(actions) - set(available_actions)))
                _logger.error(message)
                raise UnknownAppAction(app, actions)
            return actions
        except KeyError:
            message = 'Unknown app {} or app has no actions'.format(app)
            _logger.exception(message)
            raise UnknownApp(app)
Exemple #3
0
    def validate_app_actions(app, actions):
        """Validates that an app's actions are valid, meaning that they exist in a defined app API

        Args:
            app (str): The app to validate
            actions (str): The action to validate

        Returns:
            set(str): The actions. Expanded to all known actions if `action` was 'all'

         Raises:
            UnknownApp: If the app specified is not found in all known app APIs or the app has no actions
            UnknownAppAction: If the action is not found in the give app's actions
        """
        try:
            available_actions = set(
                walkoff.config.config.app_apis[app]['actions'].keys())
            if actions == 'all':
                return available_actions
            actions = set(convert_to_iterable(actions))
            if actions - available_actions:
                message = 'Unknown actions for app {0}: {1}'.format(
                    app, list(set(actions) - set(available_actions)))
                _logger.error(message)
                raise UnknownAppAction(app, actions)
            return actions
        except KeyError:
            message = 'Unknown app {} or app has no actions'.format(app)
            _logger.exception(message)
            raise UnknownApp(app)
Exemple #4
0
    def register_events(self, func, events, sender_ids=None, names=None, weak=True):
        """Registers an event for a given sender ID or name

        Args:
            func (func): Callback to register
            events (iterable(WalkoffEvent)): Events to register the callback to
            sender_ids (str|iterable(str), optional): IDs to register the callback to. Defaults to None
            names (str|iterable(str), optional): The names to register the callback to. Defaults to None
            weak (bool, optional): Should the callback be registered with a weak reference? Defaults to true
        """
        if sender_ids is None:
            sender_ids = []
        sender_ids = convert_to_iterable(sender_ids)
        if names is None:
            names = []
        names = convert_to_iterable(names)
        entry_ids = set(sender_ids) | set(names)
        if not entry_ids:
            entry_ids = ['all']
        for entry_id in entry_ids:
            self.__register_entry(entry_id, events, func, weak)
Exemple #5
0
    def register_event(self, event, device_ids, func, weak=True):
        """Registers an event and device_ids to a function

        Args:
            event (WalkoffEvent): The event to register
            device_ids: (str|iterable(int)): The device IDs to register to. Specifying 'all' will always trigger
                regardless of device ID passed to dispatch()
            func (func): Function to register
            weak: (bool, optional): Should the reference to the function be weak? Defaults to True
        """
        if event not in self._event_router:
            self._event_router[event] = {}
        if device_ids == 'all':
            self._register_event_for_device_id(event, 'all', func, weak)
        else:
            device_ids = convert_to_iterable(device_ids)
            for device_id in device_ids:
                self._register_event_for_device_id(event, device_id, func, weak)
Exemple #6
0
    def register_event(self, event, device_ids, func, weak=True):
        """Registers an event and device_ids to a function

        Args:
            event (WalkoffEvent): The event to register
            device_ids: (str|iterable(int)): The device IDs to register to. Specifying 'all' will always trigger
                regardless of device ID passed to dispatch()
            func (func): Function to register
            weak: (bool, optional): Should the reference to the function be weak? Defaults to True
        """
        if event not in self._event_router:
            self._event_router[event] = {}
        if device_ids == 'all':
            self._register_event_for_device_id(event, 'all', func, weak)
        else:
            device_ids = convert_to_iterable(device_ids)
            for device_id in device_ids:
                self._register_event_for_device_id(event, device_id, func,
                                                   weak)
Exemple #7
0
 def test_convert_to_iterable_with_string(self):
     self.assertListEqual(convert_to_iterable('hello'), ['hello'])
Exemple #8
0
 def test_convert_to_iterable(self):
     self.assertListEqual(convert_to_iterable(1), [1])
Exemple #9
0
 def test_convert_to_iterable_already_iterable(self):
     self.assertListEqual(convert_to_iterable([1, 2, 3, 4]), [1, 2, 3, 4])
 def test_convert_to_iterable_with_string(self):
     self.assertListEqual(convert_to_iterable('hello'), ['hello'])
 def test_convert_to_iterable(self):
     self.assertListEqual(convert_to_iterable(1), [1])
 def test_convert_to_iterable_already_iterable(self):
     self.assertListEqual(convert_to_iterable([1, 2, 3, 4]), [1, 2, 3, 4])