def test_config_value_float(self):
        config = RawConfigParser()
        config["section"] = {"key": 2.0}

        val = get_config_value(config, "section", "key", dtype=float)
        self.assertEqual(type(val), float)
        self.assertEqual(val, 2.0)
    def test_config_value_bool(self):
        config = RawConfigParser()
        config["section"] = {"key": "True"}

        val = get_config_value(config, "section", "key", dtype=bool)
        self.assertEqual(type(val), bool)
        self.assertEqual(val, True)
    def test_get_config_value_int(self):
        config = RawConfigParser()
        config["section"] = {"key": 3}

        val = get_config_value(config, "section", "key", dtype=int)
        self.assertEqual(type(val), int)
        self.assertEqual(val, 3)
    def unpack_config(self, config, protected_values=None):
        """
        Unpacks config data, sets relevant entries to values instance attribute
        :type config: configparser.RawConfigParser
        :param config: config data
        """

        protected_values = [] if protected_values is None else protected_values
        for section in config.sections():
            for name in config[section].keys():

                if name not in protected_values:
                    value = get_config_value(config, section, name)
                    self.set_config_value(name, value)
Exemple #5
0
def unpack_scheduler_config(scheduler_config):
    """
    Returns information from scheduler configuration information

    :type scheduler_config: configparser.RawConfigParser
    :param scheduler_config: scheduler configuration

    :rtype: dict
    :return: scheduler configuration information, with entries (defaults occur if entry omitted from config file):

    * seconds (int) - Scheduled job repeat interval in seconds, default None (if not None minutes and hours are None)
    * minutes (int) - Scheduled job repeat interval in minutes, default None (if not None seconds and hours are None)
    * hours (int) - Scheduled job repeat interval in hour, default None (if not None seconds and minutes are None)
    * start_time (datetime.datetime) - Scheduled time to start running tasks, default None (means start now)
    * parallel (bool) - Switch to run scheduled jobs on different threads, default False
    * jobs_list (str) - Path of jobs list file, to run on schedule

    """

    scheduler_config_dict = dict()

    sections = [sch for sch in scheduler_config.sections()]
    sections.remove("Log")

    for sch in sections:

        scheduler_config_dict[sch] = {}

        # Schedule Configuration
        scheduler_config_dict[sch]["seconds"] = get_config_value(
            scheduler_config, sch, "seconds", dtype=int)
        scheduler_config_dict[sch]["minutes"] = get_config_value(
            scheduler_config, sch, "minutes", dtype=int)
        scheduler_config_dict[sch]["hours"] = get_config_value(
            scheduler_config, sch, "hours", dtype=int)
        scheduler_config_dict[sch]["start_time"] = get_config_value(
            scheduler_config, sch, "start_time", dtype=str)
        scheduler_config_dict[sch]["parallel"] = get_config_value(
            scheduler_config, sch, "seconds", dtype=bool)

        # Use custom jobs list provided, else use default
        scheduler_config_dict[sch]["jobs_list"] = get_config_value(
            scheduler_config, sch, "jobs_list", dtype=str)
        if scheduler_config_dict[sch]["jobs_list"] is None:
            scheduler_config_dict[sch]["jobs_list"] = JOBS_FILE_PATH

        # todo - sort out start time format

        # Checks
        # Check only have hours, minutes or seconds
        intervals = [
            scheduler_config_dict[sch]["seconds"],
            scheduler_config_dict[sch]["minutes"],
            scheduler_config_dict[sch]["hours"]
        ]
        if intervals.count(None) != 2:
            raise ValueError(
                "job repeat interval must be defined as 1 of seconds, minutes or hours for "
                + sch)

    return scheduler_config_dict
    def test_get_config_value_dtype_bool(self):
        config = RawConfigParser()
        config["section"] = {"key": "False"}

        val = get_config_value(config, "section", "key")
        self.assertEqual(val, False)
    def test_get_config_value_dtype_None(self):
        config = RawConfigParser()
        config["section"] = {"key": "val"}

        val = get_config_value(config, "section", "key")
        self.assertEqual(val, "val")
    def test_get_config_value_None_bool(self):
        config = RawConfigParser()
        config["section"] = {"key": None}

        val = get_config_value(config, "section", "key", dtype=bool)
        self.assertEqual(val, False)
    def test_get_config_value_None(self):
        config = RawConfigParser()
        config["section"] = {"key": None}

        val = get_config_value(config, "section", "key")
        self.assertIsNone(val)
    def test_get_config_value_empty_string(self):
        config = RawConfigParser()
        config["section"] = {"key": ""}

        val = get_config_value(config, "section", "key")
        self.assertIsNone(val)
Exemple #11
0
def configure_logging(fname=None,
                      verbose=None,
                      quiet=None,
                      config=None,
                      name=None):
    """
    Configure logger

    :type fname: str
    :param fname:  path to write log to, if None output log as stdout

    :type verbose: bool
    :param verbose: Option for verbose log output (DEBUG level)

    :type quiet: bool
    :param quiet: Option for quiet log output (WARNING level)

    :type config: configparser.RawConfigParser
    :param config: Config file with logging configuration information. This finds fname, verbose and quiet if not
    specifed as arguments

    :type name: str
    :param name: (optional) set name of logger to get

    :return: logger
    :rtype: logging.logger
    """
    global loggers

    if name is not None:
        if loggers.get(name):
            return loggers.get(name)

    if config is not None:
        fname = get_config_value(config, "Log", "log_path", dtype=str)
        verbose = get_config_value(config, "Log", "verbose", dtype=bool)
        quiet = get_config_value(config, "Log", "quiet", dtype=bool)

    # Configure logger
    if name is None:
        name = __name__

    logger = logging.getLogger(name)

    # Define verboseness levels
    if verbose:
        logger.setLevel(logging.DEBUG)
    elif quiet:
        logger.setLevel(logging.WARNING)
    else:
        logger.setLevel(logging.INFO)

    # Define logging to file if fname provided
    if fname is not None:
        file_formatter = logging.Formatter(
            '%(asctime)s : %(levelname)s : %(message)s')
        file_handler = logging.FileHandler(fname)
        file_handler.setFormatter(file_formatter)
        logger.addHandler(file_handler)

    # Define logging to stdout if no fname provided
    else:
        stream_formatter = logging.Formatter('%(message)s')
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setFormatter(stream_formatter)
        logger.addHandler(stream_handler)

    loggers[name] = logger

    return logger