<s> from __future__ import absolute_import <EOL> import functools <EOL> import inspect <EOL> import sys <EOL> import wsme <EOL> import wsme . rest . args <EOL> import wsme . rest . json <EOL> import wsme . rest . xml <EOL> import pecan <EOL> from wsme . utils import is_valid_code <EOL> class JSonRenderer ( object ) : <EOL> @ staticmethod <EOL> def __init__ ( path , extra_vars ) : <EOL> pass <EOL> @ staticmethod <EOL> def render ( template_path , namespace ) : <EOL> if '<STR_LIT>' in namespace : <EOL> return wsme . rest . json . encode_error ( None , namespace ) <EOL> return wsme . rest . json . encode_result ( <EOL> namespace [ '<STR_LIT:result>' ] , <EOL> namespace [ '<STR_LIT>' ] <EOL> ) <EOL> class XMLRenderer ( object ) : <EOL> @ staticmethod <EOL> def __init__ ( path , extra_vars ) : <EOL> pass <EOL> @ staticmethod <EOL> def render ( template_path , namespace ) : <EOL> if '<STR_LIT>' in namespace : <EOL> return wsme . rest . xml . encode_error ( None , namespace ) <EOL> return wsme . rest . xml . encode_result ( <EOL> namespace [ '<STR_LIT:result>' ] , <EOL> namespace [ '<STR_LIT>' ] <EOL> ) <EOL> pecan . templating . _builtin_renderers [ '<STR_LIT>' ] = JSonRenderer <EOL> pecan . templating . _builtin_renderers [ '<STR_LIT>' ] = XMLRenderer <EOL> pecan_json_decorate = pecan . expose ( <EOL> template = '<STR_LIT>' , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> generic = False ) <EOL> pecan_xml_decorate = pecan . expose ( <EOL> template = '<STR_LIT>' , <EOL> content_type = '<STR_LIT>' , <EOL> generic = False <EOL> ) <EOL> pecan_text_xml_decorate = pecan . expose ( <EOL> template = '<STR_LIT>' , <EOL> content_type = '<STR_LIT>' , <EOL> generic = False <EOL> ) <EOL> def wsexpose ( * args , ** kwargs ) : <EOL> sig = wsme . signature ( * args , ** kwargs ) <EOL> def decorate ( f ) : <EOL> sig ( f ) <EOL> funcdef = wsme . api . FunctionDefinition . get ( f ) <EOL> funcdef . resolve_types ( wsme . types . registry ) <EOL> @ functools . wraps ( f ) <EOL> def callfunction ( self , * args , ** kwargs ) : <EOL> return_type = funcdef . return_type <EOL> try : <EOL> args , kwargs = wsme . rest . args . get_args ( <EOL> funcdef , args , kwargs , pecan . request . params , None , <EOL> pecan . request . body , pecan . request . content_type <EOL> ) <EOL> if funcdef . pass_request : <EOL> kwargs [ funcdef . pass_request ] = pecan . request <EOL> result = f ( self , * args , ** kwargs ) <EOL> pecan . response . status = funcdef . status_code <EOL> if isinstance ( result , wsme . api . Response ) : <EOL> pecan . response . status = result . status_code <EOL> if result . status_code == <NUM_LIT> : <EOL> return_type = None <EOL> elif not isinstance ( result . return_type , <EOL> wsme . types . UnsetType ) : <EOL> return_type = result . return_type <EOL> result = result . obj <EOL> except : <EOL> try : <EOL> exception_info = sys . exc_info ( ) <EOL> orig_exception = exception_info [ <NUM_LIT:1> ] <EOL> orig_code = getattr ( orig_exception , '<STR_LIT:code>' , None ) <EOL> data = wsme . api . format_exception ( <EOL> exception_info , <EOL> pecan . conf . get ( '<STR_LIT>' , { } ) . get ( '<STR_LIT>' , False ) <EOL> ) <EOL> finally : <EOL> del exception_info <EOL> if orig_code and is_valid_code ( orig_code ) : <EOL> pecan . response . status = orig_code <EOL> else : <EOL> pecan . response . status = <NUM_LIT> <EOL> return data <EOL> if return_type is None : <EOL> pecan . request . pecan [ '<STR_LIT>' ] = None <EOL> pecan . response . content_type = None <EOL> return '<STR_LIT>' <EOL> return dict ( <EOL> datatype = return_type , <EOL> result = result <EOL> ) <EOL> if '<STR_LIT>' in funcdef . rest_content_types : <EOL> pecan_xml_decorate ( callfunction ) <EOL> pecan_text_xml_decorate ( callfunction ) <EOL> if '<STR_LIT>' in funcdef . rest_content_types : <EOL> pecan_json_decorate ( callfunction ) <EOL> pecan . util . _cfg ( callfunction ) [ '<STR_LIT>' ] = inspect . getargspec ( f ) <EOL> callfunction . _wsme_definition = funcdef <EOL> return callfunction <EOL> return decorate </s>
