コード例 #1
0
    def test_should_return_default_value_when_option_not_in_section(self):
        mock_parser = Mock(YadtConfigParser)
        mock_wrapped_parser = Mock()
        mock_wrapped_parser.has_section.return_value = True
        mock_wrapped_parser.has_option.return_value = False
        mock_parser._parser = mock_wrapped_parser

        actual_option = YadtConfigParser.get_option(mock_parser, 'section',
                                                    'option', 'default_value')

        self.assertEqual('default_value', actual_option)
        self.assertEqual(call('section', 'option'),
                         mock_wrapped_parser.has_option.call_args)
コード例 #2
0
class ReceiverConfigLoader(object):
    """
        uses a YadtConfigParser which offers convenience methods.
    """
    def __init__(self):
        """
            Creates instance of YadtConfigParser which will be used to parse
            the configuration file.
        """
        self._parser = YadtConfigParser()

    def get_app_status_port(self):
        """
            @return: the app status port from the configuration file as int,
                     otherwise DEFAULT_APP_STATUS_PORT.
        """
        return self._parser.get_option_as_int(SECTION_RECEIVER,
                                              'app_status_port',
                                              DEFAULT_APP_STATUS_PORT)

    def get_broadcaster_host(self):
        """
            @return: the broadcaster host from the configuration file,
                     otherwise DEFAULT_BROADCASTER_HOST.
        """
        return self._parser.get_option(SECTION_BROADCASTER, 'host',
                                       DEFAULT_BROADCASTER_HOST)

    def get_broadcaster_port(self):
        """
            @return: the broadcaster port from the configuration file as int,
                     otherwise DEFAULT_BROADCASTER_PORT.
        """
        return self._parser.get_option_as_int(SECTION_BROADCASTER, 'port',
                                              DEFAULT_BROADCASTER_PORT)

    def get_hostname(self):
        """
            @return: if a hostname is given in the configuration file it will
                     return it, otherwise it will return the host name as
                     given by socket.gethostname
        """
        return self._parser.get_option(SECTION_RECEIVER, 'hostname',
                                       socket.gethostname())

    def get_log_filename(self):
        """
            @return: the log filename from the configuration file if given,
                     otherwise DEFAULT_LOG_FILENAME
        """
        return self._parser.get_option(SECTION_RECEIVER, 'log_filename',
                                       DEFAULT_LOG_FILENAME)

    def get_python_command(self):
        """
            @return: the python command from the configuration file if given,
                     otherwise DEFAULT_PYTHON_COMMAND
        """
        return self._parser.get_option(SECTION_RECEIVER, 'python_command',
                                       DEFAULT_PYTHON_COMMAND)

    def get_script_to_execute(self):
        """
            @return: if the configuration file provides a script to execute,
                     otherwise DEFAULT_SCRIPT_TO_EXECUTE.
        """
        return self._parser.get_option(SECTION_RECEIVER, 'script_to_execute',
                                       DEFAULT_SCRIPT_TO_EXECUTE)

    def get_targets(self):
        """
            @return: the set of targets given in the configuration file,
                     otherwise DEFAULT_TARGETS.
        """
        return self._parser.get_option_as_set(SECTION_RECEIVER, 'targets',
                                              DEFAULT_TARGETS)

    def get_targets_directory(self):
        """
            @return: the targets directory from the configuration file,
                     otherwise DEFAULT_TARGETS_DIRECTORY.
        """
        return self._parser.get_option(SECTION_RECEIVER, 'targets_directory',
                                       DEFAULT_TARGETS_DIRECTORY)

    def read_configuration_file(self, filename):
        """
            Reads the given configuration file. Uses the YadtConfigParser.

            @return: configuration dictionary
        """
        return self._parser.read_configuration_file(filename)

    def get_metrics_directory(self):
        """
            @return: the metrics_directory from the receiver configuration and
            None otherwise
        """
        return self._parser.get_option(SECTION_RECEIVER, 'metrics_directory',
                                       None)

    def get_metrics_file(self):
        """
            @return: the derived metrics_file or None
        """
        metrics_directory = self.get_metrics_directory()
        if metrics_directory is not None:
            return os.path.join(metrics_directory, 'yrc.metrics')