<s> import logging <EOL> from inflection import singularize <EOL> from . views import generate_rest_view <EOL> from . acl import generate_acl <EOL> from . utils import ( <EOL> is_dynamic_uri , resource_view_attrs , generate_model_name , <EOL> dynamic_part_name , attr_subresource , singular_subresource , <EOL> get_static_parent ) <EOL> log = logging . getLogger ( __name__ ) <EOL> def _get_nefertari_parent_resource ( <EOL> raml_resource , generated_resources , default ) : <EOL> parent_raml_res = get_static_parent ( raml_resource ) <EOL> if parent_raml_res is not None : <EOL> parent_resource = generated_resources . get ( parent_raml_res . path ) <EOL> return parent_resource or default <EOL> return default <EOL> def generate_resource ( config , raml_resource , parent_resource ) : <EOL> """<STR_LIT>""" <EOL> from . models import get_existing_model <EOL> resource_uri = raml_resource . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] . strip ( ) <EOL> if is_dynamic_uri ( resource_uri ) : <EOL> if parent_resource . is_root : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return <EOL> clean_uri = route_name = resource_uri . strip ( '<STR_LIT:/>' ) <EOL> log . info ( '<STR_LIT>' . format ( <EOL> route_name , parent_resource . uid or '<STR_LIT:root>' ) ) <EOL> is_singular = singular_subresource ( raml_resource , route_name ) <EOL> is_attr_res = attr_subresource ( raml_resource , route_name ) <EOL> if not parent_resource . is_root and ( is_attr_res or is_singular ) : <EOL> model_cls = parent_resource . view . Model <EOL> else : <EOL> model_name = generate_model_name ( route_name ) <EOL> model_cls = get_existing_model ( model_name ) <EOL> resource_kwargs = { } <EOL> log . info ( '<STR_LIT>' . format ( route_name ) ) <EOL> resource_kwargs [ '<STR_LIT>' ] = generate_acl ( <EOL> config , <EOL> model_cls = model_cls , <EOL> raml_resource = raml_resource ) <EOL> if not is_singular : <EOL> resource_kwargs [ '<STR_LIT>' ] = dynamic_part_name ( <EOL> raml_resource = raml_resource , <EOL> clean_uri = clean_uri , <EOL> pk_field = model_cls . pk_field ( ) ) <EOL> log . info ( '<STR_LIT>' . format ( route_name ) ) <EOL> view_attrs = resource_view_attrs ( raml_resource , is_singular ) <EOL> resource_kwargs [ '<STR_LIT>' ] = generate_rest_view ( <EOL> config , <EOL> model_cls = model_cls , <EOL> attrs = view_attrs , <EOL> attr_view = is_attr_res , <EOL> singular = is_singular , <EOL> ) <EOL> if is_singular : <EOL> model_name = generate_model_name ( route_name ) <EOL> view_cls = resource_kwargs [ '<STR_LIT>' ] <EOL> view_cls . _parent_model = view_cls . Model <EOL> view_cls . Model = get_existing_model ( model_name ) <EOL> log . info ( '<STR_LIT>' . format ( route_name ) ) <EOL> resource_args = ( singularize ( clean_uri ) , ) <EOL> if not is_singular : <EOL> resource_args += ( clean_uri , ) <EOL> return parent_resource . add ( * resource_args , ** resource_kwargs ) <EOL> def generate_server ( raml_root , config ) : <EOL> """<STR_LIT>""" <EOL> log . info ( '<STR_LIT>' ) <EOL> if not raml_root . resources : <EOL> return <EOL> root_resource = config . get_root_resource ( ) <EOL> generated_resources = { } <EOL> for raml_resource in raml_root . resources : <EOL> if raml_resource . path in generated_resources : <EOL> continue <EOL> parent_resource = _get_nefertari_parent_resource ( <EOL> raml_resource , generated_resources , root_resource ) <EOL> new_resource = generate_resource ( <EOL> config , raml_resource , parent_resource ) <EOL> if new_resource is not None : <EOL> generated_resources [ raml_resource . path ] = new_resource <EOL> def generate_models ( config , raml_resources ) : <EOL> """<STR_LIT>""" <EOL> from . models import handle_model_generation <EOL> if not raml_resources : <EOL> return <EOL> for raml_resource in raml_resources : <EOL> if is_dynamic_uri ( raml_resource . path ) : <EOL> continue <EOL> if raml_resource . method . upper ( ) != '<STR_LIT:POST>' : <EOL> continue <EOL> route_name = raml_resource . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> route_name = route_name . strip ( '<STR_LIT:/>' ) <EOL> if not attr_subresource ( raml_resource , route_name ) : <EOL> log . info ( '<STR_LIT>' . format ( route_name ) ) <EOL> model_cls , is_auth_model = handle_model_generation ( <EOL> config , raml_resource , route_name ) <EOL> if is_auth_model : <EOL> config . registry . auth_model = model_cls </s>
