<s> from inspect import ismethod , getmembers <EOL> import warnings <EOL> from webob import exc <EOL> import six <EOL> from . core import abort <EOL> from . decorators import expose <EOL> from . routing import lookup_controller , handle_lookup_traversal <EOL> from . util import iscontroller , getargspec <EOL> class RestController ( object ) : <EOL> '''<STR_LIT>''' <EOL> _custom_actions = { } <EOL> def __new__ ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for name , value in getmembers ( cls ) : <EOL> if iscontroller ( value ) and getattr ( value , '<STR_LIT>' , None ) : <EOL> raise ValueError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> cls . __module__ , cls . __name__ , value . __name__ <EOL> ) <EOL> ) <EOL> new = super ( RestController , cls ) . __new__ <EOL> if new is object . __new__ : <EOL> return new ( cls ) <EOL> return new ( cls , * args , ** kwargs ) <EOL> def _get_args_for_controller ( self , controller ) : <EOL> """<STR_LIT>""" <EOL> argspec = getargspec ( controller ) <EOL> from pecan import request <EOL> try : <EOL> request . path <EOL> except AttributeError : <EOL> return argspec . args [ <NUM_LIT:3> : ] <EOL> return argspec . args [ <NUM_LIT:1> : ] <EOL> def _handle_bad_rest_arguments ( self , controller , remainder , request ) : <EOL> """<STR_LIT>""" <EOL> argspec = self . _get_args_for_controller ( controller ) <EOL> fixed_args = len ( argspec ) - len ( <EOL> request . pecan . get ( '<STR_LIT>' , [ ] ) <EOL> ) <EOL> if len ( remainder ) < fixed_args : <EOL> abort ( <NUM_LIT> ) <EOL> def _lookup_child ( self , remainder ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> controller = getattr ( self , remainder , None ) <EOL> except UnicodeEncodeError : <EOL> return None <EOL> return controller <EOL> @ expose ( ) <EOL> def _route ( self , args , request = None ) : <EOL> '''<STR_LIT>''' <EOL> if request is None : <EOL> from pecan import request <EOL> method = request . params . get ( '<STR_LIT>' , request . method ) . lower ( ) <EOL> if request . method == '<STR_LIT:GET>' and method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> abort ( <NUM_LIT> ) <EOL> result = self . _find_sub_controllers ( args , request ) <EOL> if result : <EOL> return result <EOL> handler = getattr ( <EOL> self , <EOL> '<STR_LIT>' % method , <EOL> self . _handle_unknown_method <EOL> ) <EOL> try : <EOL> if len ( getargspec ( handler ) . args ) == <NUM_LIT:3> : <EOL> result = handler ( method , args ) <EOL> else : <EOL> result = handler ( method , args , request ) <EOL> argspec = self . _get_args_for_controller ( result [ <NUM_LIT:0> ] ) <EOL> num_args = len ( argspec ) <EOL> if num_args < len ( args ) : <EOL> _lookup_result = self . _handle_lookup ( args , request ) <EOL> if _lookup_result : <EOL> return _lookup_result <EOL> except ( exc . HTTPClientError , exc . HTTPNotFound , <EOL> exc . HTTPMethodNotAllowed ) as e : <EOL> _lookup_result = self . _handle_lookup ( args , request ) <EOL> if _lookup_result : <EOL> return _lookup_result <EOL> if isinstance ( e , exc . HTTPMethodNotAllowed ) : <EOL> def method_iter ( ) : <EOL> for func in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> if self . _find_controller ( func ) : <EOL> yield '<STR_LIT:GET>' <EOL> break <EOL> for method in ( '<STR_LIT>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) : <EOL> func = method . lower ( ) <EOL> if self . _find_controller ( func ) : <EOL> yield method <EOL> e . allow = sorted ( method_iter ( ) ) <EOL> raise <EOL> return result <EOL> def _handle_lookup ( self , args , request = None ) : <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . handle_lookup ) <EOL> args = list ( six . moves . filter ( bool , args ) ) <EOL> lookup = getattr ( self , '<STR_LIT>' , None ) <EOL> if args and iscontroller ( lookup ) : <EOL> result = handle_lookup_traversal ( lookup , args ) <EOL> if result : <EOL> obj , remainder = result <EOL> return lookup_controller ( obj , remainder , request ) <EOL> def _find_controller ( self , * args ) : <EOL> '''<STR_LIT>''' <EOL> for name in args : <EOL> obj = self . _lookup_child ( name ) <EOL> if obj and iscontroller ( obj ) : <EOL> return obj <EOL> return None <EOL> def _find_sub_controllers ( self , remainder , request ) : <EOL> '''<STR_LIT>''' <EOL> method = None <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if hasattr ( self , name ) : <EOL> method = name <EOL> break <EOL> if not method : <EOL> return <EOL> args = self . _get_args_for_controller ( getattr ( self , method ) ) <EOL> fixed_args = len ( args ) - len ( <EOL> request . pecan . get ( '<STR_LIT>' , [ ] ) <EOL> ) <EOL> var_args = getargspec ( getattr ( self , method ) ) . varargs <EOL> if var_args : <EOL> for i , item in enumerate ( remainder ) : <EOL> controller = self . _lookup_child ( item ) <EOL> if controller and not ismethod ( controller ) : <EOL> self . _set_routing_args ( request , remainder [ : i ] ) <EOL> return lookup_controller ( controller , remainder [ i + <NUM_LIT:1> : ] , <EOL> request ) <EOL> elif fixed_args < len ( remainder ) and hasattr ( <EOL> self , remainder [ fixed_args ] <EOL> ) : <EOL> controller = self . _lookup_child ( remainder [ fixed_args ] ) <EOL> if not ismethod ( controller ) : <EOL> self . _set_routing_args ( request , remainder [ : fixed_args ] ) <EOL> return lookup_controller ( <EOL> controller , <EOL> remainder [ fixed_args + <NUM_LIT:1> : ] , <EOL> request <EOL> ) <EOL> def _handle_unknown_method ( self , method , remainder , request = None ) : <EOL> '''<STR_LIT>''' <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . _handle_unknown_method ) <EOL> controller = self . _find_controller ( '<STR_LIT>' % method , method ) <EOL> if controller : <EOL> return controller , remainder <EOL> if remainder : <EOL> if self . _find_controller ( remainder [ <NUM_LIT:0> ] ) : <EOL> abort ( <NUM_LIT> ) <EOL> sub_controller = self . _lookup_child ( remainder [ <NUM_LIT:0> ] ) <EOL> if sub_controller : <EOL> return lookup_controller ( sub_controller , remainder [ <NUM_LIT:1> : ] , <EOL> request ) <EOL> abort ( <NUM_LIT> ) <EOL> def _handle_get ( self , method , remainder , request = None ) : <EOL> '''<STR_LIT>''' <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . _handle_get ) <EOL> if not remainder or remainder == [ '<STR_LIT>' ] : <EOL> remainder = list ( six . moves . filter ( bool , remainder ) ) <EOL> controller = self . _find_controller ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if controller : <EOL> self . _handle_bad_rest_arguments ( controller , remainder , request ) <EOL> return controller , [ ] <EOL> abort ( <NUM_LIT> ) <EOL> method_name = remainder [ - <NUM_LIT:1> ] <EOL> if method_name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if method_name == '<STR_LIT>' : <EOL> method_name = '<STR_LIT>' <EOL> controller = self . _find_controller ( method_name ) <EOL> if controller : <EOL> return controller , remainder [ : - <NUM_LIT:1> ] <EOL> match = self . _handle_custom_action ( method , remainder , request ) <EOL> if match : <EOL> return match <EOL> controller = self . _lookup_child ( remainder [ <NUM_LIT:0> ] ) <EOL> if controller and not ismethod ( controller ) : <EOL> return lookup_controller ( controller , remainder [ <NUM_LIT:1> : ] , request ) <EOL> controller = self . _find_controller ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if controller : <EOL> self . _handle_bad_rest_arguments ( controller , remainder , request ) <EOL> return controller , remainder <EOL> abort ( <NUM_LIT> ) <EOL> def _handle_delete ( self , method , remainder , request = None ) : <EOL> '''<STR_LIT>''' <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . _handle_delete ) <EOL> if remainder : <EOL> match = self . _handle_custom_action ( method , remainder , request ) <EOL> if match : <EOL> return match <EOL> controller = self . _lookup_child ( remainder [ <NUM_LIT:0> ] ) <EOL> if controller and not ismethod ( controller ) : <EOL> return lookup_controller ( controller , remainder [ <NUM_LIT:1> : ] , request ) <EOL> controller = self . _find_controller ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if controller : <EOL> return controller , remainder <EOL> if remainder : <EOL> if self . _find_controller ( remainder [ <NUM_LIT:0> ] ) : <EOL> abort ( <NUM_LIT> ) <EOL> sub_controller = self . _lookup_child ( remainder [ <NUM_LIT:0> ] ) <EOL> if sub_controller : <EOL> return lookup_controller ( sub_controller , remainder [ <NUM_LIT:1> : ] , <EOL> request ) <EOL> abort ( <NUM_LIT> ) <EOL> def _handle_post ( self , method , remainder , request = None ) : <EOL> '''<STR_LIT>''' <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . _handle_post ) <EOL> if remainder : <EOL> match = self . _handle_custom_action ( method , remainder , request ) <EOL> if match : <EOL> return match <EOL> controller = self . _lookup_child ( remainder [ <NUM_LIT:0> ] ) <EOL> if controller and not ismethod ( controller ) : <EOL> return lookup_controller ( controller , remainder [ <NUM_LIT:1> : ] , request ) <EOL> controller = self . _find_controller ( method ) <EOL> if controller : <EOL> return controller , remainder <EOL> abort ( <NUM_LIT> ) <EOL> def _handle_put ( self , method , remainder , request = None ) : <EOL> return self . _handle_post ( method , remainder , request ) <EOL> def _handle_custom_action ( self , method , remainder , request = None ) : <EOL> if request is None : <EOL> self . _raise_method_deprecation_warning ( self . _handle_custom_action ) <EOL> remainder = [ r for r in remainder if r ] <EOL> if remainder : <EOL> if method in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> method_name = remainder [ <NUM_LIT:0> ] <EOL> remainder = remainder [ <NUM_LIT:1> : ] <EOL> else : <EOL> method_name = remainder [ - <NUM_LIT:1> ] <EOL> remainder = remainder [ : - <NUM_LIT:1> ] <EOL> if method . upper ( ) in self . _custom_actions . get ( method_name , [ ] ) : <EOL> controller = self . _find_controller ( <EOL> '<STR_LIT>' % ( method , method_name ) , <EOL> method_name <EOL> ) <EOL> if controller : <EOL> return controller , remainder <EOL> def _set_routing_args ( self , request , args ) : <EOL> '''<STR_LIT>''' <EOL> request . pecan . setdefault ( '<STR_LIT>' , [ ] ) . extend ( args ) <EOL> def _raise_method_deprecation_warning ( self , handler ) : <EOL> warnings . warn ( <EOL> ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> handler . __name__ , <EOL> handler . __name__ <EOL> ) <EOL> ) , <EOL> DeprecationWarning <EOL> ) </s>
