예제 #1
0
    def get_configuration(
        self, conf_path, logger, conf=None, override=True
    ):
        """
        Parse a configuration_files with input conf and returns
        parameters and errors by param name.

        :param str conf_path: conf file to parse and from get parameters
        :param Configuration conf: conf to fill with conf_path values and
            conf param names.
        :param Logger logger: logger to use in order to trace information/error
        :param bool override: if True (by default), override self configuration
        """

        conf_resource = None

        result = None

        # ensure conf_path exists and is not empty.
        if self.exists(conf_path):
            try:
                # first, read conf file
                conf_resource = self._get_conf_resource(
                    conf_path=conf_path,
                    logger=logger
                )

            except Exception as e:
                # if an error occured, log it
                logger.error(
                    'Impossible to parse conf_path {0} with {1}: {2}'.format(
                        conf_path,
                        type(self),
                        e
                    )
                )

            else:  # else process conf file
                if conf_resource is None:
                    return result

                result = Configuration() if conf is None else conf

                categories = self._get_categories(
                    conf_resource=conf_resource,
                    logger=logger
                )

                for category_name in categories:
                    # do something only for referenced categories
                    if category_name in result:
                        category = result.setdefault(
                            category_name,
                            Category(category_name)
                        )

                        if isinstance(category, Category):
                            parameters = self._get_parameters(
                                conf_resource=conf_resource,
                                category=category,
                                logger=logger
                            )

                            for name in parameters:
                                # if param name exists in conf
                                if name in category:
                                    # copy parameter
                                    param = category[name].copy()

                                # else create not local parameter
                                else:
                                    param = Parameter(name, local=False)

                                param = category.setdefault(name, param)

                                value = self._get_value(
                                    conf_resource=conf_resource,
                                    category=category,
                                    param=param,
                                    logger=logger
                                )

                                if value not in (None, ''):
                                    if override or param.value in (None, ''):
                                        param.value = value

                        elif isinstance(category, ParamList):
                            paramlist = category
                            category = Category(category_name)
                            result.categories[category_name] = category

                            parameters = self._get_parameters(
                                conf_resource=conf_resource,
                                category=category,
                                logger=logger
                            )

                            for name in parameters:
                                param = Parameter(
                                    name,
                                    local=False,
                                    parser=paramlist.parser,
                                    asitem=category
                                )

                                param = category.setdefault(name, param)

                                value = self._get_value(
                                    conf_resource=conf_resource,
                                    category=category,
                                    param=param,
                                    logger=logger
                                )

                                if value not in (None, ''):
                                    if override or param.value in (None, ''):
                                        param.value = value

        return result
예제 #2
0
파일: manager.py 프로젝트: Anhmike/canopsis
CONF_PATH = 'check/check.conf'


class InvalidState(Exception):
    def __init__(self, state, states):
        self.state = state
        self.states = states

    def __str__(self):
        return 'Invalid state: got value {}, expected one of {}'.format(
            self.state,
            self.states
        )


@add_category(CATEGORY, content=Parameter('types', parser=Parameter.array()))
@conf_paths(CONF_PATH)
class CheckManager(MiddlewareRegistry):
    """
    Manage entity checking state.

    A state is bound to an entity. Therefore, an entity id is a document state
        id.
    """

    CHECK_STORAGE = 'check_storage'  #: storage name

    ID = '_id'  # Storage.DATA_ID  #: state id field name

    STATE = Check.STATE  #: state field name
    LAST_STATE = 'last'  #: last state field name if criticity != HARD