Exemplo n.º 1
0
    def _get_function_type(self, app_name, function_name, function_type):
        """Gets the function of the for a given app and action name

        Args:
            app_name (str): Name of the app
            function_name (str): Name of the action
            function_type (WalkoffTag): Type of function, 'actions', 'conditions', or 'transforms'

        Returns:
            (func): The function

        Raises:
            UnknownApp: If the app is not found in the cache
            UnknownAppAction: if the function_type is 'actions' and the given action name isn't found
            UnknownCondition: if the function_type is 'conditions' and the given condition name isn't found
            UnknownTransform: if the function_type is 'transforms' and the given transform name isn't found
        """
        try:
            app_cache = self._cache[app_name]
            if not app_cache.functions:
                _logger.warning('App {0} has no actions.'.format(app_name))
                raise self.exception_lookup[function_type](app_name,
                                                           function_name)
        except KeyError:
            _logger.error('Cannot locate app {} in cache!'.format(app_name))
            raise UnknownApp(app_name)
        try:
            return app_cache.get_run(function_name, function_type)
        except KeyError:
            _logger.error('App {0} has no {1} {2}'.format(
                app_name, function_type.name, function_name))
            raise self.exception_lookup[function_type](app_name, function_name)
Exemplo n.º 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)
Exemplo n.º 3
0
    def get_app(self, app_name):
        """Gets the app class for a given app.

        Args:
            app_name (str): Name of the app to get

        Returns:
            cls: The app's class

        Raises:
            UnknownApp: If the app is not found in the cache or the app has only global actions
        """
        try:
            app_cache = self._cache[app_name]
        except KeyError:
            _logger.error('Cannot locate app {} in cache!'.format(app_name))
            raise UnknownApp(app_name)
        else:
            if app_cache.main is not None:
                return app_cache.main
            else:
                _logger.warning('App {} has no class.'.format(app_name))
                raise UnknownApp(app_name)
Exemplo n.º 4
0
    def _get_function_type_names(self, app_name, function_type):
        """Gets all the names for a given function type ('action', 'condition', 'transform') for an app

        Args:
            app_name (str): The name of the app
            function_type (WalkoffTag): tag to search for

        Returns:
            list[str]: List of all the names of the functions of the given type

        Raises:
            UnknownApp: If the app is not found in the cache
        """
        try:
            return self._cache[app_name].get_tagged_functions(function_type)
        except KeyError:
            _logger.error('Cannot locate app {} in cache!'.format(app_name))
            raise UnknownApp(app_name)
Exemplo n.º 5
0
    def is_app_action_bound(self, app_name, action_name):
        """Determines if the action is bound (meaning it's inside a class) or not

        Args:
            app_name (str): Name of the app
            action_name(str): Name of the action

        Returns:
            bool: Is the action bound?

        Raises:
            UnknownApp: If the app is not found in the cache
            UnknownAppAction: If the app does not have the specified action
        """
        try:
            app_cache = self._cache[app_name]
            if not app_cache.functions:
                _logger.warning('App {} has no actions'.format(app_name))
                raise UnknownAppAction(app_name, action_name)
        except KeyError:
            _logger.error('Cannot locate app {} in cache!'.format(app_name))
            raise UnknownApp(app_name)
        else:
            return app_cache.is_bound(action_name)