def test_get_plugin_config_success(self):
        os.environ["RADIO_BRIDGE_CONFIG_PATH"] = CONFIG_PATH_1

        config = get_plugin_config("current_time")
        self.assertEqual(config["dtmf_sequence"], "54")

        config = get_plugin_config("invalid")
        self.assertEqual(config, {})
Пример #2
0
def _load_and_register_plugins() -> None:
    global INITIALIZED, REGISTERED_PLUGINS

    if INITIALIZED:
        return

    loader = pluginlib.PluginLoader(
        modules=["radio_bridge.plugins", "radio_bridge.plugins.admin"])
    plugins = loader.plugins

    for plugin_name, plugin_class in itertools.chain(
            plugins["DTMFPlugin"].items(),
            plugins["DTMFWithDataPlugin"].items(),
            plugins["AdminDTMFPlugin"].items(),
            plugins["AdminDTMFWithDataPlugin"].items(),
    ):
        if "ForTest" in plugin_name or "Mock" in plugin_name:
            continue

        LOG.debug("Found plugin: %s" % (plugin_name))

        # Initialize and validate plugin config
        plugin_config = get_plugin_config(plugin_class.ID)
        plugin_instance = plugin_class()
        plugin_instance.initialize(config=plugin_config)

        REGISTERED_PLUGINS[plugin_name] = plugin_instance

        dtmf_sequence = _validate_dtmf_sequence(plugin_class=plugin_class)

        DTMF_SEQUENCE_TO_PLUGIN_CLASS_INSTANCE_MAP[
            dtmf_sequence] = plugin_instance
        LOG.debug("Registered plugin %s with DTMF sequence #%s" %
                  (plugin_name, dtmf_sequence))

    for plugin_name, plugin_class in plugins["NonDTMFPlugin"].items():
        if "ForTest" in plugin_name or "Mock" in plugin_name:
            continue

        LOG.debug("Found plugin: %s" % (plugin_name))

        # Initialize and validate plugin config
        plugin_config = get_plugin_config(plugin_class.ID)
        plugin_instance = plugin_class()
        plugin_instance.initialize(config=plugin_config)

        REGISTERED_PLUGINS[plugin_name] = plugin_instance
        LOG.debug("Registered plugin %s" % (plugin_name))

    INITIALIZED = True
Пример #3
0
    def test_parse_and_validate_config_success(self):
        _load_and_parse_config(config_path=CONFIG_PATH)

        plugin_config = get_plugin_config(CronSayPlugin.ID)
        self.assertEqual(len(plugin_config), 5)

        plugin = CronSayPlugin()

        job_id_to_config_map = plugin._parse_and_validate_config(config=plugin_config)
        self.assertEqual(len(job_id_to_config_map), 5)

        # Text say job
        job_id = "say_current_time_every_120_seconds"
        job = job_id_to_config_map[job_id]

        self.assertEqual(job.job_id, job_id)
        self.assertEqual(job.type, "text")
        self.assertEqual(job.value, "Current time is {time_utc} UTC.")
        self.assertEqual(job.trigger_instance.interval, datetime.timedelta(seconds=120))

        # Text to morse say job
        job_id = "say_text_as_morse_every_200_seconds"
        job = job_id_to_config_map[job_id]

        self.assertEqual(job.job_id, job_id)
        self.assertEqual(job.type, "text_to_morse")
        self.assertEqual(job.value, "sos")
        self.assertEqual(job.trigger_instance.interval, datetime.timedelta(seconds=200))

        # Morse say job
        job_id = "say_morse_code_every_400_seconds"
        job = job_id_to_config_map[job_id]

        self.assertEqual(job.job_id, job_id)
        self.assertEqual(job.type, "morse")
        self.assertEqual(job.value, "... --- ...")
        self.assertEqual(job.trigger_instance.interval, datetime.timedelta(seconds=400))

        # Play file job (interval trigger)
        job_id = "play_callsign_every_500_seconds"
        job = job_id_to_config_map[job_id]

        self.assertEqual(job.job_id, job_id)
        self.assertEqual(job.type, "file")
        self.assertEqual(job.value, "tests/fixtures/audio/plugin_current_time.wav")
        self.assertEqual(job.trigger_instance.interval, datetime.timedelta(seconds=500))

        # Play file job (cron trigger)
        job_id = "play_callsign_every_5_minutes"
        job = job_id_to_config_map[job_id]

        self.assertEqual(job.job_id, job_id)
        self.assertEqual(job.type, "file")
        self.assertEqual(job.value, "tests/fixtures/audio/plugin_current_time.wav")
        self.assertEqual(
            str(job.trigger_instance),
            "cron[month='*', day='*', day_of_week='*', hour='*', minute='*/5']",
        )
Пример #4
0
    def _parse_job_specs(self) -> Dict[str, str]:
        plugin_config = get_plugin_config(self.ID)

        result = {}
        for job_id, job_specs in plugin_config.items():
            # -1 last item is always the text to say
            result[job_id] = job_specs.split(JOB_SPEC_DELIMITER)[-1]

        return result
Пример #5
0
    def test_run_success_file_job(self):
        _load_and_parse_config(config_path=CONFIG_PATH)
        plugin_config = get_plugin_config(CronSayPlugin.ID)

        plugin = CronSayPluginForTest()
        plugin.initialize(config=plugin_config)
        plugin._audio_player = mock.Mock()

        self.assertEqual(plugin._audio_player.play_file.call_count, 0)

        job_id = "play_callsign_every_5_minutes"
        plugin.run(job_id=job_id)

        plugin._audio_player.play_file.assert_called_with(
            file_path="tests/fixtures/audio/plugin_current_time.wav", delete_after_play=False
        )
        self.assertEqual(plugin._audio_player.play_file.call_count, 1)
Пример #6
0
    def test_run_success_morse_job(self, mock_morse):
        mock_morse_instance = mock.Mock()
        mock_morse.return_value = mock_morse_instance

        _load_and_parse_config(config_path=CONFIG_PATH)
        plugin_config = get_plugin_config(CronSayPlugin.ID)

        plugin = CronSayPluginForTest()
        plugin.initialize(config=plugin_config)

        self.assertEqual(mock_morse_instance.transmit.call_count, 0)

        job_id = "say_morse_code_every_400_seconds"
        plugin.run(job_id=job_id)

        mock_morse.assert_called_with(morse="... --- ...")
        self.assertEqual(mock_morse_instance.transmit.call_count, 1)
Пример #7
0
    def test_run_success_text_job(self, mock_datetime):
        mock_datetime.datetime.utcnow.return_value = datetime.datetime(2020, 10, 26, 14, 16)

        _load_and_parse_config(config_path=CONFIG_PATH)
        plugin_config = get_plugin_config(CronSayPlugin.ID)

        plugin = CronSayPluginForTest()
        plugin.initialize(config=plugin_config)

        self.assertEqual(len(plugin.mock_said_text), 0)

        job_id = "say_current_time_every_120_seconds"
        plugin.run(job_id=job_id)

        expected_text = "Current time is 14:16 UTC."

        self.assertEqual(len(plugin.mock_said_text), 1)
        self.assertEqual(plugin.mock_said_text[0], expected_text)
Пример #8
0
    def get_scheduler_jobs(self) -> List[Tuple[str, Type[BaseTrigger]]]:
        """
        Return a list of jobs to add to the scheduler based on the specifications defined in the
        config.
        """
        # TODO: Specify minimum job run interval (e.g. every 5 minutes) to avoid issues and spam,
        # etc.
        jobs = []

        plugin_config = get_plugin_config(self.ID)

        for job_id, job_specs in plugin_config.items():
            split = job_specs.split(JOB_SPEC_DELIMITER)
            job_trigger = split[0]
            job_kwargs_str = split[1]
            job_trigger_instance = self._get_job_trigger_for_job_spec(
                job_id, job_trigger, job_kwargs_str)

            job = (job_id, job_trigger_instance)
            jobs.append(job)

        return jobs