<s> """<STR_LIT>""" <EOL> import re <EOL> import inspect <EOL> import simplejson <EOL> from xml . sax . saxutils import quoteattr <EOL> from functools import update_wrapper <EOL> from babel import Locale , UnknownLocaleError <EOL> from werkzeug . exceptions import MethodNotAllowed , BadRequest <EOL> from werkzeug import Response , escape <EOL> from solace . application import get_view <EOL> from solace . urls import url_map <EOL> from solace . templating import render_template <EOL> from solace . i18n import _ , has_section <EOL> from solace . utils . remoting import remote_export_primitive <EOL> from solace . utils . formatting import format_creole <EOL> XML_NS = '<STR_LIT>' <EOL> _escaped_newline_re = re . compile ( r'<STR_LIT>' ) <EOL> def debug_dump ( obj ) : <EOL> """<STR_LIT>""" <EOL> dump = _escaped_newline_re . sub ( '<STR_LIT:\n>' , <EOL> simplejson . dumps ( obj , ensure_ascii = False , indent = <NUM_LIT:2> ) ) <EOL> return render_template ( '<STR_LIT>' , dump = dump ) <EOL> def dump_xml ( obj ) : <EOL> """<STR_LIT>""" <EOL> def _dump ( obj ) : <EOL> if isinstance ( obj , dict ) : <EOL> d = dict ( obj ) <EOL> obj_type = d . pop ( '<STR_LIT>' , None ) <EOL> key = start = '<STR_LIT>' <EOL> if obj_type is not None : <EOL> if obj_type . startswith ( '<STR_LIT>' ) : <EOL> key = start = obj_type [ <NUM_LIT:7> : ] <EOL> else : <EOL> start += '<STR_LIT>' % quoteattr ( obj_type ) <EOL> return u'<STR_LIT>' % ( <EOL> start , <EOL> u'<STR_LIT>' . join ( ( u'<STR_LIT>' % ( key , _dump ( value ) , key ) <EOL> for key , value in d . iteritems ( ) ) ) , <EOL> key <EOL> ) <EOL> if isinstance ( obj , ( tuple , list ) ) : <EOL> def _item_dump ( obj ) : <EOL> if not isinstance ( obj , ( tuple , list , dict ) ) : <EOL> return u'<STR_LIT>' % _dump ( obj ) <EOL> return _dump ( obj ) <EOL> return u'<STR_LIT>' % ( u'<STR_LIT>' . join ( map ( _item_dump , obj ) ) ) <EOL> if isinstance ( obj , bool ) : <EOL> return obj and u'<STR_LIT:yes>' or u'<STR_LIT>' <EOL> return escape ( unicode ( obj ) ) <EOL> return ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> ) % ( XML_NS , _dump ( obj ) ) <EOL> def get_serializer ( request ) : <EOL> """<STR_LIT>""" <EOL> format = request . args . get ( '<STR_LIT>' ) <EOL> if format is not None : <EOL> rv = _serializer_map . get ( format ) <EOL> if rv is None : <EOL> raise BadRequest ( _ ( u'<STR_LIT>' ) % escape ( format ) ) <EOL> return rv <EOL> if request . user_agent . browser in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return _serializer_map [ '<STR_LIT>' ] <EOL> best_match = ( None , <NUM_LIT:0> ) <EOL> for mimetype , serializer in _serializer_for_mimetypes . iteritems ( ) : <EOL> quality = request . accept_mimetypes [ mimetype ] <EOL> if quality > best_match [ <NUM_LIT:1> ] : <EOL> best_match = ( serializer , quality ) <EOL> if best_match [ <NUM_LIT:0> ] is None : <EOL> raise BadRequest ( _ ( u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' ) ) <EOL> if best_match [ <NUM_LIT:0> ] != '<STR_LIT>' and best_match [ <NUM_LIT:1> ] == request . accept_mimetypes [ '<STR_LIT>' ] : <EOL> return _serializer_map [ '<STR_LIT>' ] <EOL> return _serializer_map [ best_match [ <NUM_LIT:0> ] ] <EOL> def prepare_api_request ( request ) : <EOL> """<STR_LIT>""" <EOL> request . in_api = True <EOL> lang = request . args . get ( '<STR_LIT>' ) <EOL> if lang is not None : <EOL> if not has_section ( lang ) : <EOL> raise BadRequest ( _ ( u'<STR_LIT>' ) ) <EOL> request . locale = lang <EOL> locale = request . args . get ( '<STR_LIT>' ) <EOL> if locale is not None : <EOL> try : <EOL> locale = Locale . parse ( locale ) <EOL> if not has_locale ( locale ) : <EOL> raise UnknownLocaleError ( ) <EOL> except UnknownLocaleError : <EOL> raise BadRquest ( _ ( u'<STR_LIT>' ) ) <EOL> request . view_lang = locale <EOL> def send_api_response ( request , result ) : <EOL> """<STR_LIT>""" <EOL> ro = remote_export_primitive ( result ) <EOL> serializer , mimetype = get_serializer ( request ) <EOL> return Response ( serializer ( ro ) , mimetype = mimetype ) <EOL> def api_method ( methods = ( '<STR_LIT:GET>' , ) ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( f ) : <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> if request . method not in methods : <EOL> raise MethodNotAllowed ( methods ) <EOL> prepare_api_request ( request ) <EOL> rv = f ( request , * args , ** kwargs ) <EOL> return send_api_response ( request , rv ) <EOL> f . is_api_method = True <EOL> f . valid_methods = tuple ( methods ) <EOL> return update_wrapper ( wrapper , f ) <EOL> return decorator <EOL> def list_api_methods ( ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for rule in url_map . iter_rules ( ) : <EOL> if rule . build_only : <EOL> continue <EOL> view = get_view ( rule . endpoint ) <EOL> if not getattr ( view , '<STR_LIT>' , False ) : <EOL> continue <EOL> handler = view . __name__ <EOL> if handler . startswith ( '<STR_LIT>' ) : <EOL> handler = handler [ <NUM_LIT:4> : ] <EOL> result . append ( dict ( <EOL> handler = handler , <EOL> valid_methods = view . valid_methods , <EOL> doc = format_creole ( ( inspect . getdoc ( view ) or '<STR_LIT>' ) . decode ( '<STR_LIT:utf-8>' ) ) , <EOL> url = unicode ( rule ) <EOL> ) ) <EOL> result . sort ( key = lambda x : ( x [ '<STR_LIT:url>' ] , x [ '<STR_LIT>' ] ) ) <EOL> return result <EOL> _serializer_for_mimetypes = { <EOL> '<STR_LIT:application/json>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> _serializer_map = { <EOL> '<STR_LIT>' : ( simplejson . dumps , '<STR_LIT:application/json>' ) , <EOL> '<STR_LIT>' : ( dump_xml , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : ( debug_dump , '<STR_LIT>' ) <EOL> } </s>
