<s> import logging <EOL> from nefertari . utils import snake2camel , maybe_dotted <EOL> log = logging . getLogger ( __name__ ) <EOL> ACTIONS = [ <EOL> '<STR_LIT:index>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:replace>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> PERMISSIONS = { <EOL> '<STR_LIT:index>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:replace>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> DEFAULT_ID_NAME = '<STR_LIT:id>' <EOL> def get_app_package_name ( config ) : <EOL> """<STR_LIT>""" <EOL> if config . includepath : <EOL> return config . includepath [ <NUM_LIT:0> ] . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] <EOL> return config . package_name <EOL> def get_root_resource ( config ) : <EOL> """<STR_LIT>""" <EOL> app_package_name = get_app_package_name ( config ) <EOL> return config . registry . _root_resources . setdefault ( <EOL> app_package_name , Resource ( config ) ) <EOL> def get_resource_map ( request ) : <EOL> return request . registry . _resources_map <EOL> def add_resource_routes ( config , view , member_name , collection_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> view = maybe_dotted ( view ) <EOL> path_prefix = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> name_prefix = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if config . route_prefix : <EOL> name_prefix = "<STR_LIT>" % ( config . route_prefix , name_prefix ) <EOL> if collection_name : <EOL> id_name = '<STR_LIT>' % ( kwargs . pop ( '<STR_LIT>' , None ) or DEFAULT_ID_NAME ) <EOL> else : <EOL> id_name = '<STR_LIT>' <EOL> path = path_prefix . strip ( '<STR_LIT:/>' ) + '<STR_LIT:/>' + ( collection_name or member_name ) <EOL> _factory = kwargs . pop ( '<STR_LIT>' , None ) <EOL> _auth = kwargs . pop ( '<STR_LIT>' , None ) and _factory <EOL> _traverse = ( kwargs . pop ( '<STR_LIT>' , None ) or id_name ) if _factory else None <EOL> action_route = { } <EOL> added_routes = { } <EOL> def add_route_and_view ( config , action , route_name , path , request_method , <EOL> ** route_kwargs ) : <EOL> if route_name not in added_routes : <EOL> config . add_route ( <EOL> route_name , path , factory = _factory , <EOL> request_method = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> ** route_kwargs ) <EOL> added_routes [ route_name ] = path <EOL> action_route [ action ] = route_name <EOL> if _auth : <EOL> permission = PERMISSIONS [ action ] <EOL> else : <EOL> permission = None <EOL> config . add_view ( view = view , attr = action , route_name = route_name , <EOL> request_method = request_method , <EOL> permission = permission , <EOL> ** kwargs ) <EOL> config . commit ( ) <EOL> if collection_name == member_name : <EOL> collection_name = collection_name + '<STR_LIT>' <EOL> if collection_name : <EOL> add_route_and_view ( <EOL> config , '<STR_LIT:index>' , name_prefix + collection_name , path , <EOL> '<STR_LIT:GET>' ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + collection_name , path , <EOL> '<STR_LIT>' ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + member_name , path + id_name , <EOL> '<STR_LIT:GET>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + member_name , path + id_name , <EOL> '<STR_LIT>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT:replace>' , name_prefix + member_name , path + id_name , <EOL> '<STR_LIT>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + member_name , path + id_name , <EOL> '<STR_LIT>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + ( collection_name or member_name ) , path , <EOL> '<STR_LIT:POST>' ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , name_prefix + member_name , path + id_name , <EOL> '<STR_LIT>' , traverse = _traverse ) <EOL> if collection_name : <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , <EOL> name_prefix + ( collection_name or member_name ) , <EOL> path , '<STR_LIT>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , <EOL> name_prefix + ( collection_name or member_name ) , <EOL> path , '<STR_LIT>' , traverse = _traverse ) <EOL> add_route_and_view ( <EOL> config , '<STR_LIT>' , <EOL> name_prefix + ( collection_name or member_name ) , <EOL> path , '<STR_LIT>' , traverse = _traverse ) <EOL> return action_route <EOL> def get_default_view_path ( resource ) : <EOL> "<STR_LIT>" <EOL> parts = [ a . member_name for a in resource . ancestors ] + [ resource . collection_name or resource . member_name ] <EOL> if resource . prefix : <EOL> parts . insert ( - <NUM_LIT:1> , resource . prefix ) <EOL> view_file = '<STR_LIT:%s>' % '<STR_LIT:_>' . join ( parts ) <EOL> view = '<STR_LIT>' % ( view_file , snake2camel ( view_file ) ) <EOL> app_package_name = get_app_package_name ( resource . config ) <EOL> return '<STR_LIT>' % ( app_package_name , view ) <EOL> class Resource ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config , member_name = '<STR_LIT>' , collection_name = '<STR_LIT>' , <EOL> parent = None , uid = '<STR_LIT>' , children = None , id_name = '<STR_LIT>' , prefix = '<STR_LIT>' , <EOL> auth = False , http_cache = <NUM_LIT:0> , default_factory = None ) : <EOL> self . __dict__ . update ( locals ( ) ) <EOL> self . children = children or [ ] <EOL> self . _ancestors = [ ] <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . uid ) <EOL> def get_ancestors ( self ) : <EOL> "<STR_LIT>" <EOL> if self . _ancestors : <EOL> return self . _ancestors <EOL> if not self . parent : <EOL> return [ ] <EOL> obj = self . resource_map . get ( self . parent . uid ) <EOL> while obj and obj . member_name : <EOL> self . _ancestors . append ( obj ) <EOL> obj = obj . parent <EOL> self . _ancestors . reverse ( ) <EOL> return self . _ancestors <EOL> ancestors = property ( get_ancestors ) <EOL> resource_map = property ( lambda self : self . config . registry . _resources_map ) <EOL> model_collections = property ( <EOL> lambda self : self . config . registry . _model_collections ) <EOL> is_root = property ( lambda self : not self . member_name ) <EOL> is_singular = property ( <EOL> lambda self : not self . is_root and not self . collection_name ) <EOL> def add ( self , member_name , collection_name = '<STR_LIT>' , parent = None , uid = '<STR_LIT>' , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> parent = ( self . resource_map . get ( parent ) if type ( parent ) <EOL> is str else parent or self ) <EOL> prefix = kwargs . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> uid = ( uid or <EOL> '<STR_LIT::>' . join ( filter ( bool , [ parent . uid , prefix , member_name ] ) ) ) <EOL> if uid in self . resource_map : <EOL> raise ValueError ( '<STR_LIT>' % uid ) <EOL> id_name = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not id_name and parent : <EOL> id_name = parent . id_name <EOL> new_resource = Resource ( self . config , member_name = member_name , <EOL> collection_name = collection_name , <EOL> parent = parent , uid = uid , <EOL> id_name = id_name , <EOL> prefix = prefix ) <EOL> view = maybe_dotted ( <EOL> kwargs . pop ( '<STR_LIT>' , None ) or get_default_view_path ( new_resource ) ) <EOL> for name , val in kwargs . pop ( '<STR_LIT>' , { } ) . items ( ) : <EOL> setattr ( view , name , val ) <EOL> root_resource = self . config . get_root_resource ( ) <EOL> view . root_resource = root_resource <EOL> new_resource . view = view <EOL> path_segs = [ ] <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> for res in new_resource . ancestors : <EOL> if not res . is_singular : <EOL> if res . id_name : <EOL> id_full = res . id_name <EOL> else : <EOL> id_full = "<STR_LIT>" % ( res . member_name , DEFAULT_ID_NAME ) <EOL> path_segs . append ( '<STR_LIT>' % ( res . collection_name , id_full ) ) <EOL> else : <EOL> path_segs . append ( res . member_name ) <EOL> if path_segs : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:/>' . join ( path_segs ) <EOL> if prefix : <EOL> kwargs [ '<STR_LIT>' ] += '<STR_LIT:/>' + prefix <EOL> name_segs = [ a . member_name for a in new_resource . ancestors ] <EOL> name_segs . insert ( <NUM_LIT:1> , prefix ) <EOL> name_segs = [ seg for seg in name_segs if seg ] <EOL> if name_segs : <EOL> kwargs [ '<STR_LIT>' ] = '<STR_LIT:_>' . join ( name_segs ) + '<STR_LIT::>' <EOL> new_resource . renderer = kwargs . setdefault ( <EOL> '<STR_LIT>' , view . _default_renderer ) <EOL> kwargs . setdefault ( '<STR_LIT>' , root_resource . auth ) <EOL> kwargs . setdefault ( '<STR_LIT>' , root_resource . default_factory ) <EOL> _factory = maybe_dotted ( kwargs [ '<STR_LIT>' ] ) <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , root_resource . auth ) <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( <EOL> '<STR_LIT>' , root_resource . http_cache ) <EOL> new_resource . action_route_map = add_resource_routes ( <EOL> self . config , view , member_name , collection_name , <EOL> ** kwargs ) <EOL> self . resource_map [ uid ] = new_resource <EOL> self . resource_map . update ( dict . fromkeys ( <EOL> list ( new_resource . action_route_map . values ( ) ) , <EOL> new_resource ) ) <EOL> model = new_resource . view . Model <EOL> is_collection = model is not None and not new_resource . is_singular <EOL> if is_collection : <EOL> is_needed = ( model . __name__ not in self . model_collections or <EOL> new_resource . parent is root_resource ) <EOL> if is_needed : <EOL> self . model_collections [ model . __name__ ] = new_resource <EOL> parent . children . append ( new_resource ) <EOL> view . _resource = new_resource <EOL> view . _factory = _factory <EOL> return new_resource <EOL> def add_from_child ( self , resource , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> new_resource = self . add ( <EOL> resource . member_name , resource . collection_name , ** kwargs ) <EOL> for child in resource . children : <EOL> new_resource . add_from_child ( child , ** kwargs ) </s>
