Пример #1
0
    def get(self,
            field,
            required_field=False,
            max_value=None,
            min_value=None,
            convert_to=None,
            default=None,
            report_conflicting_environment_value=False):
        """Returns the value for the requested field.

        If the value is not set via config file, also look for it in the environment.

        This method will optionally apply some validation rules as indicated by the optional arguments.  If any
        of these validation operations fail, then a BadMonitorConfiguration exception is raised.  Monitor developers are
        encouraged to catch this exception at their layer.

        @param field: The name of the field.
        @param required_field: If True, then will raise a BadMonitorConfiguration exception if the field is not
            present.
        @param convert_to: If not None, then will convert the value for the field to the specified type. Only int,
            bool, float, long, str, and unicode are supported. If the type conversion cannot be done, a
            BadMonitorConfiguration exception is raised. The only conversions allowed are those mapped out in
            ALLOWED_CONVERSIONS. Trivial conversions are allowed from int, long to
            float, but not the other way around. Additionally, any primitive type can be converted to str, unicode.
            str, unicode can be converted to complex types such as ArrayOfStrings, JsonArray, JsonObject as long as
            they can be correctly parsed.
        @param default: The value to return if the field is not present in the configuration. This is ignored if
            'required_field' is True.
        @param max_value: If not None, the maximum allowed value for field. Raises a BadMonitorConfiguration if the
            value is greater.
        @param min_value: If not None, the minimum allowed value for field. Raises a BadMonitorConfiguration if the
            value is less than.
        @param report_conflicting_environment_value: If True, disallows overriding via environment variable.

        @return: The value
        @raise BadMonitorConfiguration: If any of the conversion or required rules are violated.
        """
        try:
            result = self.__map.get(field)
            if result is not None and convert_to is not None and type(
                    result) != convert_to:
                result = convert_config_param(field, result, convert_to)

            # Param not found in config file, so check environment
            if result is None or report_conflicting_environment_value:
                envar_name = self._environment_aware_map.get(field)
                logger = None
                if report_conflicting_environment_value:
                    logger = log
                envar_val = get_config_from_env(
                    field,
                    envar_name,
                    convert_to=convert_to,
                    logger=logger,
                    param_val=result,
                    monitor_name=self.__monitor_module)
                if result is None:
                    result = envar_val

            # Required field not found in environment nor in config
            if result is None:
                if required_field and field not in self.__map:
                    raise BadMonitorConfiguration(
                        'Missing required field "%s"' % field, field)
                result = self.__map.get(field, default)

            if result is None:
                return result

            # Perform conversion again in case both config-file and environment values were absent and the default
            # value requires conversion.
            if convert_to is not None and not issubclass(
                    convert_to, type(result)):
                result = convert_config_param(field, result, convert_to)

            if max_value is not None and result > max_value:
                raise BadMonitorConfiguration(
                    'Value of %s in field "%s" is invalid; maximum is %s' %
                    (str(result), field, str(max_value)), field)

            if min_value is not None and result < min_value:
                raise BadMonitorConfiguration(
                    'Value of %s in field "%s" is invalid; minimum is %s' %
                    (str(result), field, str(min_value)), field)

            return result
        except BadConfiguration, e:
            raise BadMonitorConfiguration(message=e.message, field=e.field)
Пример #2
0
    def get(self, field, required_field=False, max_value=None, min_value=None,
            convert_to=None, default=None, report_conflicting_environment_value=False):
        """Returns the value for the requested field.

        If the value is not set via config file, also look for it in the environment.

        This method will optionally apply some validation rules as indicated by the optional arguments.  If any
        of these validation operations fail, then a BadMonitorConfiguration exception is raised.  Monitor developers are
        encouraged to catch this exception at their layer.

        @param field: The name of the field.
        @param required_field: If True, then will raise a BadMonitorConfiguration exception if the field is not
            present.
        @param convert_to: If not None, then will convert the value for the field to the specified type. Only int,
            bool, float, long, str, and unicode are supported. If the type conversion cannot be done, a
            BadMonitorConfiguration exception is raised. The only conversions allowed are those mapped out in
            ALLOWED_CONVERSIONS. Trivial conversions are allowed from int, long to
            float, but not the other way around. Additionally, any primitive type can be converted to str, unicode.
            str, unicode can be converted to complex types such as ArrayOfStrings, JsonArray, JsonObject as long as
            they can be correctly parsed.
        @param default: The value to return if the field is not present in the configuration. This is ignored if
            'required_field' is True.
        @param max_value: If not None, the maximum allowed value for field. Raises a BadMonitorConfiguration if the
            value is greater.
        @param min_value: If not None, the minimum allowed value for field. Raises a BadMonitorConfiguration if the
            value is less than.
        @param report_conflicting_environment_value: If True, disallows overriding via environment variable.

        @return: The value
        @raise BadMonitorConfiguration: If any of the conversion or required rules are violated.
        """
        try:
            result = self.__map.get(field)
            if result is not None and convert_to is not None and type(result) != convert_to:
                result = convert_config_param(field, result, convert_to)

            # Param not found in config file, so check environment
            if result is None or report_conflicting_environment_value:
                envar_name = self._environment_aware_map.get(field)
                logger = None
                if report_conflicting_environment_value:
                    logger = log
                envar_val = get_config_from_env(field, envar_name, convert_to=convert_to,
                                                logger=logger, param_val=result, monitor_name=self.__monitor_module)
                if result is None:
                    result = envar_val

            # Required field not found in environment nor in config
            if result is None:
                if required_field and field not in self.__map:
                    raise BadMonitorConfiguration('Missing required field "%s"' % field, field)
                result = self.__map.get(field, default)

            if result is None:
                return result

            # Perform conversion again in case both config-file and environment values were absent and the default
            # value requires conversion.
            if convert_to is not None and type(result) != convert_to:
                result = convert_config_param(field, result, convert_to)

            if max_value is not None and result > max_value:
                raise BadMonitorConfiguration('Value of %s in field "%s" is invalid; maximum is %s' % (
                                              str(result), field, str(max_value)), field)

            if min_value is not None and result < min_value:
                raise BadMonitorConfiguration('Value of %s in field "%s" is invalid; minimum is %s' % (
                                              str(result), field, str(min_value)), field)

            return result
        except BadConfiguration, e:
            raise BadMonitorConfiguration(message=e.message, field=e.field)