Ejemplo n.º 1
0
    def verify_required_params(required_params, actual_params):
        error_messages = []
        for required_key, key_info in required_params.iteritems():
            expected_type = key_info[0]
            expected_value = key_info[1]
            optional = len(key_info) == 3 and key_info[2] is False

            if required_key not in actual_params:
                error_messages.append('Missing required param "{0}"'.format(required_key))
                continue

            actual_value = actual_params[required_key]
            if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
                error_messages.append('Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(required_key, type(actual_value), expected_type))
                continue

            if expected_value is None or (optional is True and actual_value in ('', None)):
                continue

            if expected_type == list:
                if type(expected_value) == Toolbox.compiled_regex_type:  # List of strings which need to match regex
                    for item in actual_value:
                        if not re.match(expected_value, item):
                            error_messages.append('Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(required_key, item, expected_value.pattern))
            else:
                if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
                    error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
                elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(expected_value, actual_value):
                    error_messages.append('Required param "{0}" with value "{1}" does not match regex "{2}"'.format(required_key, actual_value, expected_value.pattern))
        if error_messages:
            raise RuntimeError('\n' + '\n'.join(error_messages))
Ejemplo n.º 2
0
    def verify_required_params(required_params, actual_params, exact_match=False):
        """
        Verify whether the actual parameters match the required parameters
        :param required_params: Required parameters which actual parameters have to meet
        :param actual_params: Actual parameters to check for validity
        :param exact_match: Keys of both dictionaries must be identical
        :return: None
        """
        error_messages = []
        if not isinstance(required_params, dict) or not isinstance(actual_params, dict):
            raise RuntimeError('Required and actual parameters must be of type dictionary')

        if exact_match is True:
            for key in set(actual_params.keys()).difference(required_params.keys()):
                error_messages.append('Missing key "{0}" in required_params'.format(key))

        for required_key, key_info in required_params.iteritems():
            expected_type = key_info[0]
            expected_value = key_info[1]
            optional = len(key_info) == 3 and key_info[2] is False

            if optional is True and (required_key not in actual_params or actual_params[required_key] in ('', None)):
                continue

            if required_key not in actual_params:
                error_messages.append('Missing required param "{0}" in actual parameters'.format(required_key))
                continue

            actual_value = actual_params[required_key]
            if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
                error_messages.append('Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(required_key, type(actual_value), expected_type))
                continue

            if expected_value is None:
                continue

            if expected_type == list:
                if type(expected_value) == Toolbox.compiled_regex_type:  # List of strings which need to match regex
                    for item in actual_value:
                        if not re.match(expected_value, item):
                            error_messages.append('Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(required_key, item, expected_value.pattern))
            elif expected_type == dict:
                Toolbox.verify_required_params(expected_value, actual_params[required_key])
            elif expected_type == int:
                if isinstance(expected_value, list) and actual_value not in expected_value:
                    error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
                if isinstance(expected_value, dict):
                    minimum = expected_value.get('min', sys.maxint * -1)
                    maximum = expected_value.get('max', sys.maxint)
                    if not minimum <= actual_value <= maximum:
                        error_messages.append('Required param "{0}" with value "{1}" should be in range: {2} - {3}'.format(required_key, actual_value, minimum, maximum))
            else:
                if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
                    error_messages.append('Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(required_key, actual_value, expected_value))
                elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(expected_value, actual_value):
                    error_messages.append('Required param "{0}" with value "{1}" does not match regex "{2}"'.format(required_key, actual_value, expected_value.pattern))
        if error_messages:
            raise RuntimeError('\n' + '\n'.join(error_messages))
Ejemplo n.º 3
0
    def verify_required_params(required_params, actual_params):
        error_messages = []
        for required_key, key_info in required_params.iteritems():
            expected_type = key_info[0]
            expected_value = key_info[1]
            optional = len(key_info) == 3 and key_info[2] is False

            if required_key not in actual_params:
                error_messages.append(
                    'Missing required param "{0}"'.format(required_key))
                continue

            actual_value = actual_params[required_key]
            if HelperToolbox.check_type(actual_value,
                                        expected_type)[0] is False:
                error_messages.append(
                    'Required param "{0}" is of type "{1}" but we expected type "{2}"'
                    .format(required_key, type(actual_value), expected_type))
                continue

            if expected_value is None or (optional is True
                                          and actual_value in ('', None)):
                continue

            if expected_type == list:
                if type(
                        expected_value
                ) == Toolbox.compiled_regex_type:  # List of strings which need to match regex
                    for item in actual_value:
                        if not re.match(expected_value, item):
                            error_messages.append(
                                'Required param "{0}" has an item "{1}" which does not match regex "{2}"'
                                .format(required_key, item,
                                        expected_value.pattern))
            else:
                if HelperToolbox.check_type(
                        expected_value, list
                )[0] is True and actual_value not in expected_value:
                    error_messages.append(
                        'Required param "{0}" with value "{1}" should be 1 of the following: {2}'
                        .format(required_key, actual_value, expected_value))
                elif HelperToolbox.check_type(
                        expected_value, Toolbox.compiled_regex_type
                )[0] is True and not re.match(expected_value, actual_value):
                    error_messages.append(
                        'Required param "{0}" with value "{1}" does not match regex "{2}"'
                        .format(required_key, actual_value,
                                expected_value.pattern))
        if error_messages:
            raise RuntimeError('\n' + '\n'.join(error_messages))
Ejemplo n.º 4
0
 def _backend_property(self, function, dynamic):
     """
     Handles the internal caching of dynamic properties
     """
     caller_name = dynamic.name
     cache_key = '{0}_{1}'.format(self._key, caller_name)
     mutex = volatile_mutex(cache_key)
     try:
         cached_data = self._volatile.get(cache_key)
         if cached_data is None:
             if dynamic.locked:
                 mutex.acquire()
                 cached_data = self._volatile.get(cache_key)
             if cached_data is None:
                 function_info = inspect.getargspec(function)
                 if 'dynamic' in function_info.args:
                     cached_data = function(dynamic=dynamic)  # Load data from backend
                 else:
                     cached_data = function()
                 if cached_data is not None:
                     correct, allowed_types, given_type = Toolbox.check_type(cached_data, dynamic.return_type)
                     if not correct:
                         raise TypeError('Dynamic property {0} allows types {1}. {2} given'.format(
                             caller_name, str(allowed_types), given_type
                         ))
                 if dynamic.timeout > 0:
                     self._volatile.set(cache_key, cached_data, dynamic.timeout)
         return cached_data
     finally:
         mutex.release()
Ejemplo n.º 5
0
 def _backend_property(self, function, dynamic):
     """
     Handles the internal caching of dynamic properties
     """
     caller_name = dynamic.name
     cache_key = '{0}_{1}'.format(self._key, caller_name)
     mutex = VolatileMutex(cache_key)
     try:
         cached_data = self._volatile.get(cache_key)
         if cached_data is None:
             if dynamic.locked:
                 mutex.acquire()
                 cached_data = self._volatile.get(cache_key)
             if cached_data is None:
                 function_info = inspect.getargspec(function)
                 if 'dynamic' in function_info.args:
                     cached_data = function(dynamic=dynamic)  # Load data from backend
                 else:
                     cached_data = function()
                 if cached_data is not None:
                     correct, allowed_types, given_type = Toolbox.check_type(cached_data, dynamic.return_type)
                     if not correct:
                         raise TypeError('Dynamic property {0} allows types {1}. {2} given'.format(
                             caller_name, str(allowed_types), given_type
                         ))
                 if dynamic.timeout > 0:
                     self._volatile.set(cache_key, cached_data, dynamic.timeout)
         return cached_data
     finally:
         mutex.release()
Ejemplo n.º 6
0
 def _set_property(self, prop, value):
     """
     Setter for a simple property that will validate the type
     """
     self.dirty = True
     if value is None:
         self._data[prop.name] = value
     else:
         correct, allowed_types, given_type = Toolbox.check_type(value, prop.property_type)
         if correct:
             self._data[prop.name] = value
         else:
             raise TypeError('Property {0} allows types {1}. {2} given'.format(
                 prop.name, str(allowed_types), given_type
             ))
Ejemplo n.º 7
0
 def _set_property(self, prop, value):
     """
     Setter for a simple property that will validate the type
     """
     self.dirty = True
     if value is None:
         self._data[prop.name] = value
     else:
         correct, allowed_types, given_type = Toolbox.check_type(value, prop.property_type)
         if correct:
             self._data[prop.name] = value
         else:
             raise TypeError('Property {0} allows types {1}. {2} given'.format(
                 prop.name, str(allowed_types), given_type
             ))
Ejemplo n.º 8
0
    def verify_required_params(required_params, actual_params):
        """
        Verify whether the actual parameters match the required parameters
        :param required_params: Required parameters which actual parameters have to meet
        :param actual_params: Actual parameters to check for validity
        :return: None
        """
        error_messages = []
        for required_key, key_info in required_params.iteritems():
            expected_type = key_info[0]
            expected_value = key_info[1]
            optional = len(key_info) == 3 and key_info[2] is False

            if optional is True and (required_key not in actual_params
                                     or actual_params[required_key]
                                     in ('', None)):
                continue

            if required_key not in actual_params:
                error_messages.append(
                    'Missing required param "{0}"'.format(required_key))
                continue

            actual_value = actual_params[required_key]
            if HelperToolbox.check_type(actual_value,
                                        expected_type)[0] is False:
                error_messages.append(
                    'Required param "{0}" is of type "{1}" but we expected type "{2}"'
                    .format(required_key, type(actual_value), expected_type))
                continue

            if expected_value is None:
                continue

            if expected_type == list:
                if type(
                        expected_value
                ) == Toolbox.compiled_regex_type:  # List of strings which need to match regex
                    for item in actual_value:
                        if not re.match(expected_value, item):
                            error_messages.append(
                                'Required param "{0}" has an item "{1}" which does not match regex "{2}"'
                                .format(required_key, item,
                                        expected_value.pattern))
            elif expected_type == dict:
                Toolbox.verify_required_params(expected_value,
                                               actual_params[required_key])
            elif expected_type == int:
                if isinstance(expected_value,
                              list) and actual_value not in expected_value:
                    error_messages.append(
                        'Required param "{0}" with value "{1}" should be 1 of the following: {2}'
                        .format(required_key, actual_value, expected_value))
                if isinstance(expected_value, dict):
                    minimum = expected_value.get('min', sys.maxint * -1)
                    maximum = expected_value.get('max', sys.maxint)
                    if not minimum <= actual_value <= maximum:
                        error_messages.append(
                            'Required param "{0}" with value "{1}" should be in range: {2} - {3}'
                            .format(required_key, actual_value, minimum,
                                    maximum))
            else:
                if HelperToolbox.check_type(
                        expected_value, list
                )[0] is True and actual_value not in expected_value:
                    error_messages.append(
                        'Required param "{0}" with value "{1}" should be 1 of the following: {2}'
                        .format(required_key, actual_value, expected_value))
                elif HelperToolbox.check_type(
                        expected_value, Toolbox.compiled_regex_type
                )[0] is True and not re.match(expected_value, actual_value):
                    error_messages.append(
                        'Required param "{0}" with value "{1}" does not match regex "{2}"'
                        .format(required_key, actual_value,
                                expected_value.pattern))
        if error_messages:
            raise RuntimeError('\n' + '\n'.join(error_messages))
Ejemplo n.º 9
0
    def verify_required_params(required_params, actual_params):
        error_messages = []
        for required_key, key_info in required_params.iteritems():
            expected_type = key_info[0]
            expected_value = key_info[1]
            optional = len(key_info) == 3 and key_info[2] is False

            if optional is True and (required_key not in actual_params or actual_params[required_key] in ("", None)):
                continue

            if required_key not in actual_params:
                error_messages.append('Missing required param "{0}"'.format(required_key))
                continue

            actual_value = actual_params[required_key]
            if HelperToolbox.check_type(actual_value, expected_type)[0] is False:
                error_messages.append(
                    'Required param "{0}" is of type "{1}" but we expected type "{2}"'.format(
                        required_key, type(actual_value), expected_type
                    )
                )
                continue

            if expected_value is None:
                continue

            if expected_type == list:
                if type(expected_value) == Toolbox.compiled_regex_type:  # List of strings which need to match regex
                    for item in actual_value:
                        if not re.match(expected_value, item):
                            error_messages.append(
                                'Required param "{0}" has an item "{1}" which does not match regex "{2}"'.format(
                                    required_key, item, expected_value.pattern
                                )
                            )
            elif expected_type == dict:
                Toolbox.verify_required_params(expected_value, actual_params[required_key])
            elif expected_type == int:
                if isinstance(expected_value, list) and actual_value not in expected_value:
                    error_messages.append(
                        'Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(
                            required_key, actual_value, expected_value
                        )
                    )
                if isinstance(expected_value, dict):
                    minimum = expected_value.get("min", sys.maxint * -1)
                    maximum = expected_value.get("max", sys.maxint)
                    if not minimum <= actual_value <= maximum:
                        error_messages.append(
                            'Required param "{0}" with value "{1}" should be in range: {2} - {3}'.format(
                                required_key, actual_value, minimum, maximum
                            )
                        )
            else:
                if HelperToolbox.check_type(expected_value, list)[0] is True and actual_value not in expected_value:
                    error_messages.append(
                        'Required param "{0}" with value "{1}" should be 1 of the following: {2}'.format(
                            required_key, actual_value, expected_value
                        )
                    )
                elif HelperToolbox.check_type(expected_value, Toolbox.compiled_regex_type)[0] is True and not re.match(
                    expected_value, actual_value
                ):
                    error_messages.append(
                        'Required param "{0}" with value "{1}" does not match regex "{2}"'.format(
                            required_key, actual_value, expected_value.pattern
                        )
                    )
        if error_messages:
            raise RuntimeError("\n" + "\n".join(error_messages))