예제 #1
0
    def audio_analyser_callback(self, order):
        """
        Callback of the OrderListener. Called after the processing of the audio file
        This method will
        - call the Order Analyser to analyse the  order and launch corresponding synapse as usual.
        - get a list of launched synapse.
        - give the list to the main process via self.launched_synapses
        - notify that the processing is over via order_analyser_return
        :param order: string order to analyse
        :return:
        """
        logger.debug("order to process %s" % order)
        if order is not None:  # maybe we have received a null audio from STT engine
            order_analyser = OrderAnalyser(order, brain=self.brain)
            synapses_launched = order_analyser.start()
            self.launched_synapses = synapses_launched
        else:
            if self.settings.default_synapse is not None:
                SynapseLauncher.start_synapse(
                    name=self.settings.default_synapse, brain=self.brain)
                self.launched_synapses = self.brain.get_synapse_by_name(
                    synapse_name=self.settings.default_synapse)

        # this boolean will notify the main process that the order have been processed
        self.order_analyser_return = True
예제 #2
0
 def run_synapse_by_name(synapse_name):
     """
     This method will run the synapse
     """
     Utils.print_info("Event triggered, running synapse: %s" % synapse_name)
     # get a brain
     brain_loader = BrainLoader()
     brain = brain_loader.brain
     SynapseLauncher.start_synapse(synapse_name, brain=brain)
예제 #3
0
    def test_start_synapse(self):
        """
        Test the Synapse launcher trying to start synapse
        """
        # Init
        neuron1 = Neuron(name='neurone1', parameters={'var1': 'val1'})
        neuron2 = Neuron(name='neurone2', parameters={'var2': 'val2'})
        neuron3 = Neuron(name='neurone3', parameters={'var3': 'val3'})
        neuron4 = Neuron(name='neurone4', parameters={'var4': 'val4'})

        signal1 = Order(sentence="this is the sentence")
        signal2 = Order(sentence="this is the second sentence")
        signal3 = Order(sentence="that is part of the third sentence")

        synapse1 = Synapse(name="Synapse1",
                           neurons=[neuron1, neuron2],
                           signals=[signal1])
        synapse2 = Synapse(name="Synapse2",
                           neurons=[neuron3, neuron4],
                           signals=[signal2])
        synapse3 = Synapse(name="Synapse3",
                           neurons=[neuron2, neuron4],
                           signals=[signal3])

        all_synapse_list = [synapse1, synapse2, synapse3]

        br = Brain(synapses=all_synapse_list)

        sl = SettingLoader()
        r = Resources(neuron_folder="/var/tmp/test/resources")
        sl.settings.resources = r
        with mock.patch("kalliope.core.Utils.get_dynamic_class_instantiation"
                        ) as mock_get_class_instantiation:
            # Success
            SynapseLauncher.start_synapse("Synapse1", brain=br)

            calls = [
                mock.call(package_name="neurons",
                          module_name=neuron1.name,
                          parameters=neuron1.parameters,
                          resources_dir='/var/tmp/test/resources'),
                mock.call(package_name="neurons",
                          module_name=neuron2.name,
                          parameters=neuron2.parameters,
                          resources_dir='/var/tmp/test/resources')
            ]
            mock_get_class_instantiation.assert_has_calls(calls=calls)
            mock_get_class_instantiation.reset_mock()

            # Fail
            with self.assertRaises(SynapseNameNotFound):
                SynapseLauncher.start_synapse("Synapse4", brain=br)
예제 #4
0
 def analysing_order_thread(self):
     """
     Start the order analyser with the caught order to process
     """
     logger.debug("order in analysing_order_thread %s" % self.order_to_process)
     if self.order_to_process is not None:   # maybe we have received a null audio from STT engine
         order_analyser = OrderAnalyser(self.order_to_process, brain=self.brain)
         order_analyser.start()
     else:
         if self.settings.default_synapse is not None:
             SynapseLauncher.start_synapse(name=self.settings.default_synapse, brain=self.brain)
     # return to the state "unpausing_trigger"
     self.unpause_trigger()
예제 #5
0
    def run_synapse_by_name(self, synapse_name):
        """
        Run a synapse by its name
        test with curl:
        curl -i --user admin:secret -X POST  http://127.0.0.1:5000/synapses/start/id/say-hello-fr
        :param synapse_name:
        :return:
        """
        synapse_target = self._get_synapse_by_name(synapse_name)

        if synapse_target is None:
            data = {"synapse name not found": "%s" % synapse_name}
            return jsonify(error=data), 404

        # run the synapse
        SynapseLauncher.start_synapse(synapse_name, brain=self.brain)
        data = jsonify(synapses=synapse_target.serialize())
        return data, 201
예제 #6
0
    def show_synapses_test_menu(self):
        """
        Show a list of available synapse in the brain to run it directly
        """

        # create a tuple for the list menu
        choices = list()
        x = 0
        for el in self.brain.synapses:
            tup = (str(el.name), str(x))
            choices.append(tup)
            x += 1

        code, tag = self.d.menu("Select a synapse to run", choices=choices)

        if code == self.d.CANCEL:
            self.show_main_menu()
        if code == self.d.OK:
            logger.debug("Run synapse from GUI: %s" % tag)
            SynapseLauncher.start_synapse(tag, brain=self.brain)
            self.show_synapses_test_menu()
예제 #7
0
def main():
    """
    Entry point of Kalliope program
    """
    # create arguments
    parser = argparse.ArgumentParser(description='Kalliope')
    parser.add_argument("action", help="[start|gui]")
    parser.add_argument("--run-synapse",
                        help="Name of a synapse to load surrounded by quote")
    parser.add_argument("--brain-file", help="Full path of a brain file")
    parser.add_argument("--debug",
                        action='store_true',
                        help="Show debug output")

    # parse arguments from script parameters
    args = parser.parse_args()

    # require at least one parameter, the action
    if len(sys.argv[1:]) == 0:
        parser.print_usage()
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=args.debug)

    logger.debug("kalliope args: %s" % args)

    # by default, no brain file is set. Use the default one: brain.yml in the root path
    brain_file = None
    # check if user set a brain.yml file
    if args.brain_file:
        brain_file = args.brain_file
    # load the brain once
    brain_loader = BrainLoader.Instance(file_path=brain_file)
    brain = brain_loader.brain

    # check the user provide a valid action
    if args.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % args.action)
        parser.print_help()

    if args.action == "start":
        # user set a synapse to start
        if args.run_synapse is not None:
            SynapseLauncher.start_synapse(args.run_synapse, brain=brain)

        if args.run_synapse is None:
            # first, load events in crontab
            crontab_manager = CrontabManager(brain=brain)
            crontab_manager.load_events_in_crontab()
            Utils.print_success("Events loaded in crontab")
            # then start kalliope
            Utils.print_success("Starting Kalliope")
            Utils.print_info("Press Ctrl+C for stopping")
            # catch signal for killing on Ctrl+C pressed
            signal.signal(signal.SIGINT, signal_handler)
            # start the main controller
            MainController(brain=brain)

    if args.action == "gui":
        ShellGui(brain=brain)
예제 #8
0
def main():
    """Entry point of Kalliope program."""

    # create arguments
    parser = argparse.ArgumentParser(description='Kalliope')
    parser.add_argument("action", help="[start|gui|install|uninstall]")
    parser.add_argument("--run-synapse",
                        help="Name of a synapse to load surrounded by quote")
    parser.add_argument("--run-order", help="order surrounded by a quote")
    parser.add_argument("--brain-file", help="Full path of a brain file")
    parser.add_argument("--debug",
                        action='store_true',
                        help="Show debug output")
    parser.add_argument("--git-url", help="Git URL of the neuron to install")
    parser.add_argument("--neuron-name", help="Neuron name to uninstall")
    parser.add_argument("--stt-name", help="STT name to uninstall")
    parser.add_argument("--tts-name", help="TTS name to uninstall")
    parser.add_argument("--trigger-name", help="Trigger name to uninstall")
    parser.add_argument('-v',
                        '--version',
                        action='version',
                        version='Kalliope ' + version_str)

    # parse arguments from script parameters
    args = parser.parse_args()

    # require at least one parameter, the action
    if len(sys.argv[1:]) == 0:
        parser.print_usage()
        sys.exit(1)

    # check if we want debug
    configure_logging(debug=args.debug)

    logger.debug("kalliope args: %s" % args)

    # by default, no brain file is set.
    # Use the default one: brain.yml in the root path
    brain_file = None

    # check if user set a brain.yml file
    if args.brain_file:
        brain_file = args.brain_file

    # check the user provide a valid action
    if args.action not in ACTION_LIST:
        Utils.print_warning("%s is not a recognised action\n" % args.action)
        parser.print_help()

    # install modules
    if args.action == "install":
        if not args.git_url:
            Utils.print_danger("You must specify the git url")
        else:
            parameters = {"git_url": args.git_url}
            res_manager = ResourcesManager(**parameters)
            res_manager.install()
        return

    # uninstall modules
    if args.action == "uninstall":
        if not args.neuron_name and not args.stt_name and not args.tts_name and not args.trigger_name:
            Utils.print_danger(
                "You must specify a module name with --neuron-name or --stt-name or --tts-name "
                "or --trigger-name")
        else:
            res_manager = ResourcesManager()
            res_manager.uninstall(neuron_name=args.neuron_name,
                                  stt_name=args.stt_name,
                                  tts_name=args.tts_name,
                                  trigger_name=args.trigger_name)
        return

    # load the brain once
    brain_loader = BrainLoader(file_path=brain_file)
    brain = brain_loader.brain

    if args.action == "start":

        # user set a synapse to start
        if args.run_synapse is not None:
            SynapseLauncher.start_synapse(args.run_synapse, brain=brain)

        if args.run_order is not None:
            order_analyser = OrderAnalyser(args.run_order, brain=brain)
            order_analyser.start()

        if (args.run_synapse is None) and (args.run_order is None):
            # first, load events in event manager
            EventManager(brain.synapses)
            Utils.print_success("Events loaded")
            # then start kalliope
            Utils.print_success("Starting Kalliope")
            Utils.print_info("Press Ctrl+C for stopping")
            # catch signal for killing on Ctrl+C pressed
            signal.signal(signal.SIGINT, signal_handler)
            # start the state machine
            MainController(brain=brain)

    if args.action == "gui":
        ShellGui(brain=brain)
예제 #9
0
 def run_synapse_by_name(self, name):
     SynapseLauncher.start_synapse(name=name, brain=self.brain)