<s> import re <EOL> from django . utils . encoding import smart_unicode , smart_str <EOL> from django . http import HttpResponse <EOL> import errors <EOL> from http import Response <EOL> import util <EOL> class DataMapper ( object ) : <EOL> """<STR_LIT>""" <EOL> content_type = '<STR_LIT>' <EOL> charset = '<STR_LIT:utf-8>' <EOL> def format ( self , response ) : <EOL> """<STR_LIT>""" <EOL> res = self . _prepare_response ( response ) <EOL> res . content = self . _format_data ( res . content , self . charset ) <EOL> return self . _finalize_response ( res ) <EOL> def parse ( self , data , charset = None ) : <EOL> """<STR_LIT>""" <EOL> charset = charset or self . charset <EOL> return self . _parse_data ( data , charset ) <EOL> def _decode_data ( self , data , charset ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return smart_unicode ( data , charset ) <EOL> except UnicodeDecodeError : <EOL> raise errors . BadRequest ( '<STR_LIT>' ) <EOL> def _encode_data ( self , data ) : <EOL> """<STR_LIT>""" <EOL> return smart_str ( data , self . charset ) <EOL> def _format_data ( self , data , charset ) : <EOL> """<STR_LIT>""" <EOL> return self . _encode_data ( data ) if data else u'<STR_LIT>' <EOL> def _parse_data ( self , data , charset ) : <EOL> """<STR_LIT>""" <EOL> return self . _decode_data ( data , charset ) if data else u'<STR_LIT>' <EOL> def _prepare_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( response , Response ) : <EOL> return Response ( <NUM_LIT:0> , response ) <EOL> return response <EOL> def _finalize_response ( self , response ) : <EOL> """<STR_LIT>""" <EOL> res = HttpResponse ( content = response . content , <EOL> content_type = self . _get_content_type ( ) ) <EOL> res . status_code = response . code <EOL> return res <EOL> def _get_content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . content_type , self . charset ) <EOL> class DataMapperManager ( object ) : <EOL> """<STR_LIT>""" <EOL> _format_query_pattern = re . compile ( '<STR_LIT>' ) <EOL> _datamappers = { } <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _datamappers = { <EOL> '<STR_LIT>' : DataMapper ( ) <EOL> } <EOL> def register_mapper ( self , mapper , content_type , shortname = None ) : <EOL> """<STR_LIT>""" <EOL> self . _check_mapper ( mapper ) <EOL> cont_type_names = self . _get_content_type_names ( content_type , shortname ) <EOL> self . _datamappers . update ( dict ( [ ( name , mapper ) for name in cont_type_names ] ) ) <EOL> def select_formatter ( self , request , resource ) : <EOL> """<STR_LIT>""" <EOL> if resource . mapper : <EOL> return resource . mapper <EOL> mapper_name = self . _get_name_from_url ( request ) <EOL> if mapper_name : <EOL> return self . _get_mapper ( mapper_name ) <EOL> mapper_name = self . _get_name_from_accept ( request ) <EOL> if mapper_name : <EOL> return self . _get_mapper ( mapper_name ) <EOL> if resource . default_mapper : <EOL> return resource . default_mapper <EOL> return self . _get_default_mapper ( ) <EOL> def select_parser ( self , request , resource ) : <EOL> """<STR_LIT>""" <EOL> if resource . mapper : <EOL> return resource . mapper <EOL> mapper_name = self . _get_name_from_content_type ( request ) <EOL> if mapper_name : <EOL> return self . _get_mapper ( mapper_name ) <EOL> mapper_name = self . _get_name_from_url ( request ) <EOL> if mapper_name : <EOL> return self . _get_mapper ( mapper_name ) <EOL> if resource . default_mapper : <EOL> return resource . default_mapper <EOL> return self . _get_default_mapper ( ) <EOL> def get_mapper_by_content_type ( self , content_type ) : <EOL> """<STR_LIT>""" <EOL> content_type = util . strip_charset ( content_type ) <EOL> return self . _get_mapper ( content_type ) <EOL> def set_default_mapper ( self , mapper ) : <EOL> """<STR_LIT>""" <EOL> mapper = mapper or DataMapper ( ) <EOL> self . _datamappers [ '<STR_LIT>' ] = mapper <EOL> def _get_default_mapper ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _datamappers [ '<STR_LIT>' ] <EOL> def _get_mapper ( self , mapper_name ) : <EOL> """<STR_LIT>""" <EOL> if mapper_name in self . _datamappers : <EOL> return self . _datamappers [ mapper_name ] <EOL> else : <EOL> return self . _unknown_format ( mapper_name ) <EOL> def _get_name_from_content_type ( self , request ) : <EOL> """<STR_LIT>""" <EOL> content_type = request . META . get ( '<STR_LIT>' , None ) <EOL> if content_type : <EOL> return util . strip_charset ( content_type ) <EOL> return None <EOL> def _get_name_from_accept ( self , request ) : <EOL> """<STR_LIT>""" <EOL> accepts = util . parse_accept_header ( request . META . get ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> if not accepts : <EOL> return None <EOL> for accept in accepts : <EOL> if accept [ <NUM_LIT:0> ] in self . _datamappers : <EOL> return accept [ <NUM_LIT:0> ] <EOL> raise errors . NotAcceptable ( ) <EOL> def _get_name_from_url ( self , request ) : <EOL> """<STR_LIT>""" <EOL> format = request . GET . get ( '<STR_LIT>' , None ) <EOL> if not format : <EOL> match = self . _format_query_pattern . match ( request . path ) <EOL> if match and match . group ( '<STR_LIT>' ) : <EOL> format = match . group ( '<STR_LIT>' ) <EOL> return format <EOL> def _unknown_format ( self , format ) : <EOL> """<STR_LIT>""" <EOL> raise errors . NotAcceptable ( '<STR_LIT>' + format ) <EOL> def _get_content_type_names ( self , content_type , shortname ) : <EOL> """<STR_LIT:U+0020>""" <EOL> ret = [ shortname , content_type ] if shortname else [ content_type ] <EOL> mtype , subtype = content_type . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if mtype != '<STR_LIT:*>' and subtype != '<STR_LIT:*>' : <EOL> ret . append ( '<STR_LIT>' % ( mtype , ) ) <EOL> return ret <EOL> def _check_mapper ( self , mapper ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( mapper , '<STR_LIT>' ) or not callable ( mapper . parse ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if not hasattr ( mapper , '<STR_LIT>' ) or not callable ( mapper . format ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> manager = DataMapperManager ( ) <EOL> def format ( request , response , resource ) : <EOL> return manager . select_formatter ( request , resource ) . format ( response ) <EOL> def parse ( data , request , resource ) : <EOL> charset = util . get_charset ( request ) <EOL> return manager . select_parser ( request , resource ) . parse ( data , charset ) </s>
