Пример #1
0
 def update_in_place(self, source, destination):
     with field_errors_check() as errors:
         for source_field, value, destination_field in self._iterate_instance(source):
             try:
                 self._destination_entity.set_value(destination, destination_field, value)
             except FieldAssignmentError as field_error:
                 errors.append(field_error)
Пример #2
0
 def iterate_instance(self, instance, per_field_map):
     with field_errors_check() as errors:
         for fieldname, value in instance.items():
             field = self[fieldname]
             try:
                 yield field, value, per_field_map.get(field)
             except FieldAssignmentError as fae:
                 errors[field] = fae
Пример #3
0
 def iterate_instance(self, instance, per_field_map):
     with field_errors_check() as errors:
         for field, destination_field in per_field_map.items():
             try:
                 yield field, self._get_value(instance,
                                              field), destination_field
             except FieldAssignmentError as field_error:
                 errors.append(field_error)
             except Exception as ex:
                 errors.append(FieldAssignmentError(field, ex))
Пример #4
0
 def update_instance_from_dict(self, instance, input_dict):
     with field_errors_check() as errors:
         for field_name, value in input_dict.items():
             field = self[field_name]
             try:
                 self.set_value(instance, field, value)
             except FieldAssignmentError as fae:
                 if isinstance(fae.original_exception, AttributeError):
                     LOG.warning('Unable to assign {} to {}'.format(
                         value, field_name))
                 else:
                     errors[field] = fae
Пример #5
0
 def _iterate_instance(self, source):
     source_field_to_destination_field = dict(self._field_mappings)
     with field_errors_check() as errors:
         for source_field, value, destination_field in self._source_entity.iterate_instance(
             source, source_field_to_destination_field
         ):
             try:
                 if destination_field is not None:
                     if self._field_casts and source_field.name in self._field_casts:
                         value = self._field_casts[source_field.name](value, destination_field)
                     yield source_field, value, destination_field
             except FieldAssignmentError as field_error:
                 errors.append(field_error)
             except NotFound as nf:
                 errors.append(FieldAssignmentError(source_field, nf))
             except DATA_LOAD_ERRORS as ex:
                 errors.append(FieldAssignmentError(source_field, ex))
Пример #6
0
    def _type_cast(self, input_dict):
        cast_values = []
        with field_errors_check() as errors:
            for field in self._fields:
                try:
                    if field.name in input_dict:
                        raw_value = input_dict[field.name]
                    else:
                        raw_value = field.get_value(self, input_dict)
                    cast_values.append(
                        (field.name, field.type_cast(raw_value)))
                except FieldAssignmentError as fae:
                    errors.append(fae)
                except DATA_LOAD_ERRORS as ex:
                    errors.append(FieldAssignmentError(field, ex))

        return OrderedDict(cast_values)
Пример #7
0
    def _type_cast(self, input_dict):
        field_and_value = ((fieldname, self[fieldname], value)
                           for fieldname, value in input_dict.items())

        key_values = []

        with field_errors_check() as errors:
            for fieldname, field, value in field_and_value:
                try:
                    key_values.append((fieldname, field.type_cast(value)))
                except FieldAssignmentError as fae:
                    errors[field] = fae

            for field in self._fields:
                try:
                    computed_value = field.get_value(self, input_dict)
                    key_values.append((field.name, computed_value))
                except KeyError:
                    pass

        return OrderedDict(key_values)