示例#1
0
class QuickFluidRule(FluidRule):
    """
    A shortcut to assign an action_ to a spec.
    
    Example::
    
        rule = QuickFluidRule("press home key", Key("home"))
        
    """
    _next_unique_id = 1

    def __init__(self, spec, action, args={}, **kwargs):
        """
        
        :param string spec: The spec for this command, from which `intros
            <intros>` will be determined.
        :param action: The action to be executed when this
            command is said.
        :type action: a dragonfly action_
        :param dict args: Provides a way to add to or modify the extras
            dictionary. The args dictionary has keys of name strings, items of
            function callbacks. The callbacks are supplied a single parameter
            of a dictionary of extras, and their return value is assigned to
            the extra named by the key. When the ``action`` is executed, it
            will then have these final values available to it.
        :param \*\*kwargs: Passed to `FluidRule`, except ``"name"`` and ``"spec"``
            ignored.
        
        
        """
        if isinstance(action, ActionBase) or not six.callable(action):
            self.action = action
            self._is_call = False
        else:
            self.action = Function(action)
            self._is_call = True

        self.args = args
        kwargs["spec"] = spec
        kwargs["name"] = self._autogenerate_name(spec)
        FluidRule.__init__(self, **kwargs)

    def _autogenerate_name(self, spec):
        id_string = str(QuickFluidRule._next_unique_id)
        QuickFluidRule._next_unique_id += 1
        return "quickFluidRule_" + spec + "_id" + id_string

    def _process_recognition(self, node, extras):
        if self._is_call:
            format_candidates = [(name, extra)
                                 for name, extra in extras.items()
                                 if name not in self.args.keys()]
            for name, extra in format_candidates:
                if isinstance(extra, DictationContainerBase):
                    extras[name] = extra.format()
        for name, value_callback in self.args.items():
            extras[name] = value_callback(extras)
        self.action.execute(extras)
示例#2
0
class QuickFluidRule(FluidRule):
    """
    A shortcut to assign an action_ to a spec.
    
    Example::
    
        rule = QuickFluidRule("press home key", Key("home"))
        
    """
    _next_unique_id = 1

    def __init__(self, spec, action, args={}, **kwargs):
        """
        
        :param string spec: The spec for this command, from which `intros
            <intros>` will be determined.
        :param action: The action to be executed when this
            command is said.
        :type action: a dragonfly action_
        :param dict args: Provides a way to add to or modify the extras
            dictionary. The args dictionary has keys of name strings, items of
            function callbacks. The callbacks are supplied a single parameter
            of a dictionary of extras, and their return value is assigned to
            the extra named by the key. When the ``action`` is executed, it
            will then have these final values available to it.
        :param \*\*kwargs: Passed to `FluidRule`, except ``"name"`` and ``"spec"``
            ignored.
        
        
        """
        if isinstance(action, ActionBase) or not six.callable(action):
            self.action = action
            self._is_call = False
        else:
            self.action = Function(action)
            self._is_call = True

        self.args = args
        kwargs["spec"] = spec
        kwargs["name"] = self._autogenerate_name(spec)
        FluidRule.__init__(self, **kwargs)
    
    def _autogenerate_name(self, spec):
        id_string = str(QuickFluidRule._next_unique_id)
        QuickFluidRule._next_unique_id += 1
        return "quickFluidRule_" + spec + "_id" + id_string
            
    def _process_recognition(self, node, extras):
        if self._is_call:
            format_candidates = [(name, extra) for name, extra in extras.items() if name not in self.args.keys()]
            for name, extra in format_candidates:
                if isinstance(extra, DictationContainerBase):
                    extras[name] = extra.format()
        for name, value_callback in self.args.items():
            extras[name] = value_callback(extras)
        self.action.execute(extras)