<s> """<STR_LIT>""" <EOL> import sys <EOL> import netaddr <EOL> from neutron_lib import exceptions <EOL> import oslo_i18n <EOL> from oslo_log import log as logging <EOL> from oslo_policy import policy as oslo_policy <EOL> import six <EOL> import webob . dec <EOL> import webob . exc <EOL> from neutron . _i18n import _ , _LE , _LI <EOL> from neutron import wsgi <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class Request ( wsgi . Request ) : <EOL> pass <EOL> def Resource ( controller , faults = None , deserializers = None , serializers = None , <EOL> action_status = None ) : <EOL> """<STR_LIT>""" <EOL> default_deserializers = { '<STR_LIT:application/json>' : wsgi . JSONDeserializer ( ) } <EOL> default_serializers = { '<STR_LIT:application/json>' : wsgi . JSONDictSerializer ( ) } <EOL> format_types = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> action_status = action_status or dict ( create = <NUM_LIT> , delete = <NUM_LIT> ) <EOL> default_deserializers . update ( deserializers or { } ) <EOL> default_serializers . update ( serializers or { } ) <EOL> deserializers = default_deserializers <EOL> serializers = default_serializers <EOL> faults = faults or { } <EOL> @ webob . dec . wsgify ( RequestClass = Request ) <EOL> def resource ( request ) : <EOL> route_args = request . environ . get ( '<STR_LIT>' ) <EOL> if route_args : <EOL> args = route_args [ <NUM_LIT:1> ] . copy ( ) <EOL> else : <EOL> args = { } <EOL> args . pop ( '<STR_LIT>' , None ) <EOL> fmt = args . pop ( '<STR_LIT>' , None ) <EOL> action = args . pop ( '<STR_LIT:action>' , None ) <EOL> content_type = format_types . get ( fmt , <EOL> request . best_match_content_type ( ) ) <EOL> language = request . best_match_language ( ) <EOL> deserializer = deserializers . get ( content_type ) <EOL> serializer = serializers . get ( content_type ) <EOL> try : <EOL> if request . body : <EOL> args [ '<STR_LIT:body>' ] = deserializer . deserialize ( request . body ) [ '<STR_LIT:body>' ] <EOL> method = getattr ( controller , action ) <EOL> result = method ( request = request , ** args ) <EOL> except ( exceptions . NeutronException , <EOL> netaddr . AddrFormatError , <EOL> oslo_policy . PolicyNotAuthorized ) as e : <EOL> for fault in faults : <EOL> if isinstance ( e , fault ) : <EOL> mapped_exc = faults [ fault ] <EOL> break <EOL> else : <EOL> mapped_exc = webob . exc . HTTPInternalServerError <EOL> if <NUM_LIT> <= mapped_exc . code < <NUM_LIT> : <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:action>' : action , '<STR_LIT>' : e } ) <EOL> else : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , action ) <EOL> e = translate ( e , language ) <EOL> body = serializer . serialize ( <EOL> { '<STR_LIT>' : get_exception_data ( e ) } ) <EOL> kwargs = { '<STR_LIT:body>' : body , '<STR_LIT>' : content_type } <EOL> raise mapped_exc ( ** kwargs ) <EOL> except webob . exc . HTTPException as e : <EOL> type_ , value , tb = sys . exc_info ( ) <EOL> if hasattr ( e , '<STR_LIT:code>' ) and <NUM_LIT> <= e . code < <NUM_LIT> : <EOL> LOG . info ( _LI ( '<STR_LIT>' ) , <EOL> { '<STR_LIT:action>' : action , '<STR_LIT>' : e } ) <EOL> else : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , action ) <EOL> translate ( e , language ) <EOL> value . body = serializer . serialize ( <EOL> { '<STR_LIT>' : get_exception_data ( e ) } ) <EOL> value . content_type = content_type <EOL> six . reraise ( type_ , value , tb ) <EOL> except NotImplementedError as e : <EOL> e = translate ( e , language ) <EOL> body = serializer . serialize ( <EOL> { '<STR_LIT>' : get_exception_data ( e ) } ) <EOL> kwargs = { '<STR_LIT:body>' : body , '<STR_LIT>' : content_type } <EOL> raise webob . exc . HTTPNotImplemented ( ** kwargs ) <EOL> except Exception : <EOL> LOG . exception ( _LE ( '<STR_LIT>' ) , action ) <EOL> msg = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> msg = translate ( msg , language ) <EOL> body = serializer . serialize ( <EOL> { '<STR_LIT>' : get_exception_data ( <EOL> webob . exc . HTTPInternalServerError ( msg ) ) } ) <EOL> kwargs = { '<STR_LIT:body>' : body , '<STR_LIT>' : content_type } <EOL> raise webob . exc . HTTPInternalServerError ( ** kwargs ) <EOL> status = action_status . get ( action , <NUM_LIT:200> ) <EOL> body = serializer . serialize ( result ) <EOL> if status == <NUM_LIT> : <EOL> content_type = '<STR_LIT>' <EOL> body = None <EOL> return webob . Response ( request = request , status = status , <EOL> content_type = content_type , <EOL> body = body ) <EOL> setattr ( resource , '<STR_LIT>' , controller ) <EOL> return resource <EOL> def get_exception_data ( e ) : <EOL> """<STR_LIT>""" <EOL> err_data = { '<STR_LIT:type>' : e . __class__ . __name__ , <EOL> '<STR_LIT:message>' : e , '<STR_LIT>' : '<STR_LIT>' } <EOL> return err_data <EOL> def translate ( translatable , locale ) : <EOL> """<STR_LIT>""" <EOL> localize = oslo_i18n . translate <EOL> if isinstance ( translatable , exceptions . NeutronException ) : <EOL> translatable . msg = localize ( translatable . msg , locale ) <EOL> elif isinstance ( translatable , webob . exc . HTTPError ) : <EOL> translatable . detail = localize ( translatable . detail , locale ) <EOL> elif isinstance ( translatable , Exception ) : <EOL> translatable . message = localize ( translatable , locale ) <EOL> else : <EOL> return localize ( translatable , locale ) <EOL> return translatable </s>
