Esempio n. 1
0
 def _validate_attributes(self, element, **kwargs):
     validated_attributes = OrderedDict()
     attributes = element.attributes
     if attributes:
         expected_attributes = set([
             validator.tag for validator in self.attributes
             if validator.tag[-1] != '*'
         ])
         extra_attribute_keys = set(attributes.keys()) - expected_attributes
         validators = OrderedDict()
         for validator in self.attributes:
             if validator.tag in attributes.keys():
                 validators[validator.tag] = validator
         if extra_attribute_keys:
             allow_extra_attributes = any(validator.tag[-1] == '*'
                                          for validator in self.attributes)
             if allow_extra_attributes:
                 for extra_attribute_name in extra_attribute_keys:
                     validated_attributes[
                         extra_attribute_name] = attributes[
                             extra_attribute_name]
             else:
                 utils.error(
                     logger, 'Found unexpected attributes: "%s" in "%s".' %
                     (', '.join(extra_attribute_keys), element.path))
         for tag in validators.keys():
             if tag in expected_attributes:
                 if validators[tag].validator is None:
                     validated_attributes[tag] = attributes[tag]
                 else:
                     kwargs.update(path=element.path + '@%s' % tag)
                     validated_attributes[tag] = self._validate(
                         validators[tag].validator, attributes[tag],
                         '%s.@%s' % (element.tag, tag), **kwargs)
         return validated_attributes
Esempio n. 2
0
 def match_sequence(self, value_tags, parent_path):
     result_sequence = []
     covered_tags_set = set([])
     failed = False
     for field in self.sequence:
         if isinstance(field, ElementSchema):
             if field.tag in value_tags:
                 result_sequence.append(field)
             elif field.minOccurs > 0:
                 msg = "Missing required key: %s" % field.tag
                 utils.error(logger, msg)
                 failed = True
             covered_tags_set.add(field.tag)
         elif isinstance(field, Choice):
             choice_keys_sey = set(value_tags) & field.all_keys_set
             cs = field.match_choice_keys(choice_keys_sey)
             covered_tags_set = covered_tags_set | field.all_keys_set
             if cs:
                 result_sequence.extend(cs)
     extra_tags = set(value_tags) - covered_tags_set
     if extra_tags:
         msg = "Could not match tag(s): %s" % ', '.join(extra_tags)
         utils.error(logger, msg)
     elif not failed:
         self.check_key_order(value_tags, result_sequence, parent_path)
     return result_sequence
Esempio n. 3
0
 def _validate_attributes(self, element, **kwargs):
     validated_attributes = OrderedDict()
     attributes = element.attributes
     if attributes:
         expected_attributes = set([validator.tag
                                    for validator in self.attributes
                                    if validator.tag[-1] != '*'])
         extra_attribute_keys = set(attributes.keys()) - expected_attributes
         validators = OrderedDict()
         for validator in self.attributes:
             if validator.tag in attributes.keys():
                 validators[validator.tag] = validator
         if extra_attribute_keys:
             allow_extra_attributes = any(validator.tag[-1] == '*'
                                          for validator in self.attributes)
             if allow_extra_attributes:
                 for extra_attribute_name in extra_attribute_keys:
                     validated_attributes[extra_attribute_name] = attributes[extra_attribute_name]
             else:
                 utils.error(logger, 'Found unexpected attributes: "%s" in "%s".' % (
                     ', '.join(extra_attribute_keys), element.path))
         for tag in validators.keys():
             if tag in expected_attributes:
                 if validators[tag].validator is None:
                     validated_attributes[tag] = attributes[tag]
                 else:
                     kwargs.update(path=element.path + '@%s' % tag)
                     validated_attributes[tag] = self._validate(
                         validators[tag].validator,
                         attributes[tag], '%s.@%s' % (element.tag, tag),
                         **kwargs)
         return validated_attributes
Esempio n. 4
0
 def match_sequence(self, value_tags, parent_path):
     result_sequence = []
     covered_tags_set = set([])
     failed = False
     for field in self.sequence:
         if isinstance(field, ElementSchema):
             if field.tag in value_tags:
                 result_sequence.append(field)
             elif field.minOccurs > 0:
                 msg = "Missing required key: %s" % field.tag
                 utils.error(logger, msg)
                 failed = True
             covered_tags_set.add(field.tag)
         elif isinstance(field, Choice):
             choice_keys_sey = set(value_tags) & field.all_keys_set
             cs = field.match_choice_keys(choice_keys_sey)
             covered_tags_set = covered_tags_set | field.all_keys_set
             if cs:
                 result_sequence.extend(cs)
     extra_tags = set(value_tags) - covered_tags_set
     if extra_tags:
         msg = "Could not match tag(s): %s" % ', '.join(extra_tags)
         utils.error(logger, msg)
     elif not failed:
         self.check_key_order(value_tags, result_sequence, parent_path)
     return result_sequence
Esempio n. 5
0
 def match_choice_keys(self, value_key_set):
     no_match_msg = "Could not match keys: %s with: choices: %s" % (
         ', '.join(value_key_set), self.choice_keys_str())
     if value_key_set == set([]) and not self.required:
         return []
     max_key_sets = [self.required_keys_sets[i] | self.optional_keys_sets[i]
                     for i in range(len(self.options))]
     min_key_matches = [value_key_set >= min_keys
                        for min_keys in self.required_keys_sets]
     max_key_matches = [value_key_set <= max_keys
                        for max_keys in max_key_sets]
     if not any(min_key_matches):
         utils.error(logger, no_match_msg)
     if not any(max_key_matches):
         utils.error(logger, no_match_msg)
     if any(min_key_matches) and any(max_key_matches):
         matches = [i for i in range(len(self.options))
                    if min_key_matches[i] and max_key_matches[i]]
         if isinstance(self.options[matches[0]], ElementSchema):
             matched_fields = [self.options[matches[0]]]
         else:
             matched_fields = [field
                               for field in self.options[matches[0]]
                               if field.tag in value_key_set]
         msg = "Matched keys: %s with option: %d" % \
               (', '.join(value_key_set), matches[0])
         utils.debug(logger, msg)
         return matched_fields
     return [self._flat_options[tag] for tag in value_key_set
             if tag in self._flat_options]
Esempio n. 6
0
 def _validate(self, validator, value, value_type, **kwargs):
     if validator is None:
         return value
     try:
         result = validator.to_python(value, **kwargs)
     except ValidationException as e:
         path = kwargs['path']
         msg = 'Error validating %s in "%s": %s got: %r' % (
             value_type, path, e._msg, value)
         utils.error(logger, msg)
     else:
         logger.debug('Successfully validated "%s", got: %r' %
                      (value_type, result))
         return result
Esempio n. 7
0
 def _validate(self, validator, value, value_type, **kwargs):
     if validator is None:
         return value
     try:
         result = validator.to_python(value, **kwargs)
     except ValidationException as e:
         path = kwargs['path']
         msg = 'Error validating %s in "%s": %s got: %r' % (
             value_type, path, e._msg, value)
         utils.error(logger, msg)
     else:
         logger.debug('Successfully validated "%s", got: %r'
                      % (value_type, result))
         return result
Esempio n. 8
0
 def match_choice_keys(self, value_key_set):
     no_match_msg = "Could not match keys: %s with: choices: %s" % (
         ', '.join(value_key_set), self.choice_keys_str())
     if value_key_set == set([]) and not self.required:
         return []
     max_key_sets = [
         self.required_keys_sets[i] | self.optional_keys_sets[i]
         for i in range(len(self.options))
     ]
     min_key_matches = [
         value_key_set >= min_keys for min_keys in self.required_keys_sets
     ]
     max_key_matches = [
         value_key_set <= max_keys for max_keys in max_key_sets
     ]
     if not any(min_key_matches):
         utils.error(logger, no_match_msg)
     if not any(max_key_matches):
         utils.error(logger, no_match_msg)
     if any(min_key_matches) and any(max_key_matches):
         matches = [
             i for i in range(len(self.options))
             if min_key_matches[i] and max_key_matches[i]
         ]
         if isinstance(self.options[matches[0]], ElementSchema):
             matched_fields = [self.options[matches[0]]]
         else:
             matched_fields = [
                 field for field in self.options[matches[0]]
                 if field.tag in value_key_set
             ]
         msg = "Matched keys: %s with option: %d" % \
               (', '.join(value_key_set), matches[0])
         utils.debug(logger, msg)
         return matched_fields
     return [
         self._flat_options[tag] for tag in value_key_set
         if tag in self._flat_options
     ]
Esempio n. 9
0
def test_message_count_info_errors_abort():
    utils.reset_message_counters()
    utils.error(logger, 'test_message_count_info_errors_abort')
    utils.message_count_info(logger,
                             'test_message_count_info_errors_abort',
                             abort_on_errors=True)
Esempio n. 10
0
def test_error_pass():
    utils.reset_message_counters()
    utils.error(logger, 'test_error_state_None')
    nose.tools.eq_(utils.error_count, 1)