def adapt_persistent_to_rest(self, persistent_object, attribute_filter=None):
<EOL>if isinstance(attribute_filter, parser.AttributeFilter):<EOL><INDENT>attribute_filter = attribute_filter.as_immutable()<EOL><DEDENT>rest_model_instance = self.rest_model_class()<EOL>for attribute_key in rest_model_instance.get_attribute_keys():<EOL><INDENT>if isinstance(attribute_filter, (parser.AttributeFilter, parser.AttributeFilterImmutable)) andnot attribute_filter.is_attribute_visible(attribute_key):<EOL><INDENT>continue<EOL><DEDENT>rest_attr = getattr(self.rest_model_class, attribute_key)<EOL>if not hasattr(persistent_object, attribute_key):<EOL><INDENT>if isinstance(rest_attr, types.Model):<EOL><INDENT>try:<EOL><INDENT>setattr(rest_model_instance, attribute_key, None)<EOL><DEDENT>except TypeError as exp:<EOL><INDENT>raise TypeError('<STR_LIT>' % (attribute_key, str(exp)))<EOL><DEDENT><DEDENT>continue<EOL><DEDENT>elif inspect.ismethod(getattr(persistent_object, attribute_key)):<EOL><INDENT>import logging<EOL>logging.error("<STR_LIT>"+attribute_key)<EOL>continue<EOL><DEDENT>elif isinstance(rest_attr, types.Array):<EOL><INDENT>persistent_attr_value = getattr(persistent_object, attribute_key)<EOL>rest_model_array_handle = getattr(rest_model_instance, attribute_key)<EOL>for collection_element in persistent_attr_value:<EOL><INDENT>if rest_attr.is_scalar:<EOL><INDENT>rest_model_array_handle.append(collection_element)<EOL><DEDENT>else:<EOL><INDENT>element_adapter = registry.get_adapter_for_rest_model(rest_attr.element_template)<EOL>sub_attribute_filter = None<EOL>if attribute_filter and attribute_key in attribute_filter:<EOL><INDENT>sub_attribute_filter = getattr(attribute_filter, attribute_key)<EOL><DEDENT>adapted_rest_model = element_adapter.adapt_persistent_to_rest(<EOL>collection_element,<EOL>sub_attribute_filter<EOL>)<EOL>rest_model_array_handle.append(adapted_rest_model)<EOL><DEDENT><DEDENT><DEDENT>elif isinstance(rest_attr, types.Model):<EOL><INDENT>try:<EOL><INDENT>persistent_attr_value = getattr(persistent_object, attribute_key)<EOL>if persistent_attr_value is None:<EOL><INDENT>adapted_rest_model = None<EOL><DEDENT>else:<EOL><INDENT>model_adapter = registry.get_adapter_for_rest_model(rest_attr)<EOL>sub_attribute_filter = None<EOL>if isinstance(attribute_filter, (parser.AttributeFilter, parser.AttributeFilterImmutable)) andattribute_key in attribute_filter:<EOL><INDENT>sub_attribute_filter = getattr(attribute_filter, attribute_key)<EOL><DEDENT>adapted_rest_model = model_adapter.adapt_persistent_to_rest(<EOL>persistent_attr_value,<EOL>sub_attribute_filter<EOL>)<EOL><DEDENT>setattr(rest_model_instance, attribute_key, adapted_rest_model)<EOL><DEDENT>except TypeError as exp:<EOL><INDENT>raise TypeError('<STR_LIT>' % (attribute_key, str(exp)))<EOL><DEDENT>except exception.DataValidationException as exp:<EOL><INDENT>raise exception.InconsistentPersistentDataError(attribute_key, str(exp))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>persistent_attr_value = getattr(persistent_object, attribute_key)<EOL>setattr(rest_model_instance, attribute_key, persistent_attr_value)<EOL><DEDENT>except TypeError as exp:<EOL><INDENT>raise TypeError('<STR_LIT>' % (attribute_key, str(exp)))<EOL><DEDENT>except exception.ValidationError as exp:<EOL><INDENT>raise exception.InconsistentPersistentDataError(attribute_key, str(exp))<EOL><DEDENT><DEDENT><DEDENT>return rest_model_instance<EOL>
adapts a persistent model to a rest model by inspecting