<s> import logging <EOL> from functools import partial <EOL> from . action import ServiceAction <EOL> from . action import WaiterAction <EOL> from . base import ResourceMeta , ServiceResource <EOL> from . collection import CollectionFactory <EOL> from . model import ResourceModel <EOL> from . response import build_identifiers , ResourceHandler <EOL> from . . exceptions import ResourceLoadException <EOL> from . . docs import docstring <EOL> logger = logging . getLogger ( __name__ ) <EOL> class ResourceFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , emitter ) : <EOL> self . _collection_factory = CollectionFactory ( ) <EOL> self . _emitter = emitter <EOL> def load_from_definition ( self , resource_name , <EOL> single_resource_json_definition , service_context ) : <EOL> """<STR_LIT>""" <EOL> logger . debug ( '<STR_LIT>' , service_context . service_name , <EOL> resource_name ) <EOL> resource_model = ResourceModel ( <EOL> resource_name , single_resource_json_definition , <EOL> service_context . resource_json_definitions <EOL> ) <EOL> shape = None <EOL> if resource_model . shape : <EOL> shape = service_context . service_model . shape_for ( <EOL> resource_model . shape ) <EOL> resource_model . load_rename_map ( shape ) <EOL> meta = ResourceMeta ( <EOL> service_context . service_name , resource_model = resource_model ) <EOL> attrs = { <EOL> '<STR_LIT>' : meta , <EOL> } <EOL> self . _load_identifiers ( <EOL> attrs = attrs , meta = meta , resource_name = resource_name , <EOL> resource_model = resource_model <EOL> ) <EOL> self . _load_actions ( <EOL> attrs = attrs , resource_name = resource_name , <EOL> resource_model = resource_model , service_context = service_context <EOL> ) <EOL> self . _load_attributes ( <EOL> attrs = attrs , meta = meta , resource_name = resource_name , <EOL> resource_model = resource_model , <EOL> service_context = service_context ) <EOL> self . _load_collections ( <EOL> attrs = attrs , resource_model = resource_model , <EOL> service_context = service_context ) <EOL> self . _load_has_relations ( <EOL> attrs = attrs , resource_name = resource_name , <EOL> resource_model = resource_model , service_context = service_context <EOL> ) <EOL> self . _load_waiters ( <EOL> attrs = attrs , resource_name = resource_name , <EOL> resource_model = resource_model , service_context = service_context <EOL> ) <EOL> cls_name = resource_name <EOL> if service_context . service_name == resource_name : <EOL> cls_name = '<STR_LIT>' <EOL> cls_name = service_context . service_name + '<STR_LIT:.>' + cls_name <EOL> base_classes = [ ServiceResource ] <EOL> if self . _emitter is not None : <EOL> self . _emitter . emit ( <EOL> '<STR_LIT>' % cls_name , <EOL> class_attributes = attrs , base_classes = base_classes , <EOL> service_context = service_context ) <EOL> return type ( str ( cls_name ) , tuple ( base_classes ) , attrs ) <EOL> def _load_identifiers ( self , attrs , meta , resource_model , resource_name ) : <EOL> """<STR_LIT>""" <EOL> for identifier in resource_model . identifiers : <EOL> meta . identifiers . append ( identifier . name ) <EOL> attrs [ identifier . name ] = self . _create_identifier ( <EOL> identifier , resource_name ) <EOL> def _load_actions ( self , attrs , resource_name , resource_model , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> if resource_model . load : <EOL> attrs [ '<STR_LIT>' ] = self . _create_action ( <EOL> action_model = resource_model . load , resource_name = resource_name , <EOL> service_context = service_context , is_load = True ) <EOL> attrs [ '<STR_LIT>' ] = attrs [ '<STR_LIT>' ] <EOL> for action in resource_model . actions : <EOL> attrs [ action . name ] = self . _create_action ( <EOL> action_model = action , resource_name = resource_name , <EOL> service_context = service_context ) <EOL> def _load_attributes ( self , attrs , meta , resource_name , resource_model , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> if not resource_model . shape : <EOL> return <EOL> shape = service_context . service_model . shape_for ( <EOL> resource_model . shape ) <EOL> identifiers = dict ( ( i . member_name , i ) <EOL> for i in resource_model . identifiers if i . member_name ) <EOL> attributes = resource_model . get_attributes ( shape ) <EOL> for name , ( orig_name , member ) in attributes . items ( ) : <EOL> if name in identifiers : <EOL> prop = self . _create_identifier_alias ( <EOL> resource_name = resource_name , <EOL> identifier = identifiers [ name ] , <EOL> member_model = member , <EOL> service_context = service_context <EOL> ) <EOL> else : <EOL> prop = self . _create_autoload_property ( <EOL> resource_name = resource_name , <EOL> name = orig_name , snake_cased = name , <EOL> member_model = member , <EOL> service_context = service_context <EOL> ) <EOL> attrs [ name ] = prop <EOL> def _load_collections ( self , attrs , resource_model , service_context ) : <EOL> """<STR_LIT>""" <EOL> for collection_model in resource_model . collections : <EOL> attrs [ collection_model . name ] = self . _create_collection ( <EOL> resource_name = resource_model . name , <EOL> collection_model = collection_model , <EOL> service_context = service_context <EOL> ) <EOL> def _load_has_relations ( self , attrs , resource_name , resource_model , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> for reference in resource_model . references : <EOL> attrs [ reference . name ] = self . _create_reference ( <EOL> reference_model = reference , <EOL> resource_name = resource_name , <EOL> service_context = service_context <EOL> ) <EOL> for subresource in resource_model . subresources : <EOL> attrs [ subresource . name ] = self . _create_class_partial ( <EOL> subresource_model = subresource , <EOL> resource_name = resource_name , <EOL> service_context = service_context <EOL> ) <EOL> self . _create_available_subresources_command ( <EOL> attrs , resource_model . subresources ) <EOL> def _create_available_subresources_command ( self , attrs , subresources ) : <EOL> _subresources = [ subresource . name for subresource in subresources ] <EOL> _subresources = sorted ( _subresources ) <EOL> def get_available_subresources ( factory_self ) : <EOL> """<STR_LIT>""" <EOL> return _subresources <EOL> attrs [ '<STR_LIT>' ] = get_available_subresources <EOL> def _load_waiters ( self , attrs , resource_name , resource_model , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> for waiter in resource_model . waiters : <EOL> attrs [ waiter . name ] = self . _create_waiter ( <EOL> resource_waiter_model = waiter , <EOL> resource_name = resource_name , <EOL> service_context = service_context <EOL> ) <EOL> def _create_identifier ( factory_self , identifier , resource_name ) : <EOL> """<STR_LIT>""" <EOL> def get_identifier ( self ) : <EOL> return getattr ( self , '<STR_LIT:_>' + identifier . name , None ) <EOL> get_identifier . __name__ = str ( identifier . name ) <EOL> get_identifier . __doc__ = docstring . IdentifierDocstring ( <EOL> resource_name = resource_name , <EOL> identifier_model = identifier , <EOL> include_signature = False <EOL> ) <EOL> return property ( get_identifier ) <EOL> def _create_identifier_alias ( factory_self , resource_name , identifier , <EOL> member_model , service_context ) : <EOL> """<STR_LIT>""" <EOL> def get_identifier ( self ) : <EOL> return getattr ( self , '<STR_LIT:_>' + identifier . name , None ) <EOL> get_identifier . __name__ = str ( identifier . member_name ) <EOL> get_identifier . __doc__ = docstring . AttributeDocstring ( <EOL> service_name = service_context . service_name , <EOL> resource_name = resource_name , <EOL> attr_name = identifier . member_name , <EOL> event_emitter = factory_self . _emitter , <EOL> attr_model = member_model , <EOL> include_signature = False <EOL> ) <EOL> return property ( get_identifier ) <EOL> def _create_autoload_property ( factory_self , resource_name , name , <EOL> snake_cased , member_model , service_context ) : <EOL> """<STR_LIT>""" <EOL> def property_loader ( self ) : <EOL> if self . meta . data is None : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . load ( ) <EOL> else : <EOL> raise ResourceLoadException ( <EOL> '<STR_LIT>' . format ( self . __class__ . __name__ ) ) <EOL> return self . meta . data . get ( name ) <EOL> property_loader . __name__ = str ( snake_cased ) <EOL> property_loader . __doc__ = docstring . AttributeDocstring ( <EOL> service_name = service_context . service_name , <EOL> resource_name = resource_name , <EOL> attr_name = snake_cased , <EOL> event_emitter = factory_self . _emitter , <EOL> attr_model = member_model , <EOL> include_signature = False <EOL> ) <EOL> return property ( property_loader ) <EOL> def _create_waiter ( factory_self , resource_waiter_model , resource_name , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> waiter = WaiterAction ( resource_waiter_model , <EOL> waiter_resource_name = resource_waiter_model . name ) <EOL> def do_waiter ( self , * args , ** kwargs ) : <EOL> waiter ( self , * args , ** kwargs ) <EOL> do_waiter . __name__ = str ( resource_waiter_model . name ) <EOL> do_waiter . __doc__ = docstring . ResourceWaiterDocstring ( <EOL> resource_name = resource_name , <EOL> event_emitter = factory_self . _emitter , <EOL> service_model = service_context . service_model , <EOL> resource_waiter_model = resource_waiter_model , <EOL> service_waiter_model = service_context . service_waiter_model , <EOL> include_signature = False <EOL> ) <EOL> return do_waiter <EOL> def _create_collection ( factory_self , resource_name , collection_model , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> cls = factory_self . _collection_factory . load_from_definition ( <EOL> resource_name = resource_name , collection_model = collection_model , <EOL> service_context = service_context , <EOL> event_emitter = factory_self . _emitter ) <EOL> def get_collection ( self ) : <EOL> return cls ( <EOL> collection_model = collection_model , parent = self , <EOL> factory = factory_self , service_context = service_context ) <EOL> get_collection . __name__ = str ( collection_model . name ) <EOL> get_collection . __doc__ = docstring . CollectionDocstring ( <EOL> collection_model = collection_model , include_signature = False ) <EOL> return property ( get_collection ) <EOL> def _create_reference ( factory_self , reference_model , resource_name , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> handler = ResourceHandler ( <EOL> search_path = reference_model . resource . path , factory = factory_self , <EOL> resource_model = reference_model . resource , <EOL> service_context = service_context <EOL> ) <EOL> needs_data = any ( i . source == '<STR_LIT:data>' for i in <EOL> reference_model . resource . identifiers ) <EOL> def get_reference ( self ) : <EOL> if needs_data and self . meta . data is None and hasattr ( self , '<STR_LIT>' ) : <EOL> self . load ( ) <EOL> return handler ( self , { } , self . meta . data ) <EOL> get_reference . __name__ = str ( reference_model . name ) <EOL> get_reference . __doc__ = docstring . ReferenceDocstring ( <EOL> reference_model = reference_model , <EOL> include_signature = False <EOL> ) <EOL> return property ( get_reference ) <EOL> def _create_class_partial ( factory_self , subresource_model , resource_name , <EOL> service_context ) : <EOL> """<STR_LIT>""" <EOL> name = subresource_model . resource . type <EOL> def create_resource ( self , * args , ** kwargs ) : <EOL> positional_args = [ ] <EOL> json_def = service_context . resource_json_definitions . get ( name , { } ) <EOL> resource_cls = factory_self . load_from_definition ( <EOL> resource_name = name , <EOL> single_resource_json_definition = json_def , <EOL> service_context = service_context <EOL> ) <EOL> identifiers = subresource_model . resource . identifiers <EOL> if identifiers is not None : <EOL> for identifier , value in build_identifiers ( identifiers , self ) : <EOL> positional_args . append ( value ) <EOL> return partial ( resource_cls , * positional_args , <EOL> client = self . meta . client ) ( * args , ** kwargs ) <EOL> create_resource . __name__ = str ( name ) <EOL> create_resource . __doc__ = docstring . SubResourceDocstring ( <EOL> resource_name = resource_name , <EOL> sub_resource_model = subresource_model , <EOL> service_model = service_context . service_model , <EOL> include_signature = False <EOL> ) <EOL> return create_resource <EOL> def _create_action ( factory_self , action_model , resource_name , <EOL> service_context , is_load = False ) : <EOL> """<STR_LIT>""" <EOL> action = ServiceAction ( <EOL> action_model , factory = factory_self , <EOL> service_context = service_context <EOL> ) <EOL> if is_load : <EOL> def do_action ( self , * args , ** kwargs ) : <EOL> response = action ( self , * args , ** kwargs ) <EOL> self . meta . data = response <EOL> lazy_docstring = docstring . LoadReloadDocstring ( <EOL> action_name = action_model . name , <EOL> resource_name = resource_name , <EOL> event_emitter = factory_self . _emitter , <EOL> load_model = action_model , <EOL> service_model = service_context . service_model , <EOL> include_signature = False <EOL> ) <EOL> else : <EOL> def do_action ( self , * args , ** kwargs ) : <EOL> response = action ( self , * args , ** kwargs ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . meta . data = None <EOL> return response <EOL> lazy_docstring = docstring . ActionDocstring ( <EOL> resource_name = resource_name , <EOL> event_emitter = factory_self . _emitter , <EOL> action_model = action_model , <EOL> service_model = service_context . service_model , <EOL> include_signature = False <EOL> ) <EOL> do_action . __name__ = str ( action_model . name ) <EOL> do_action . __doc__ = lazy_docstring <EOL> return do_action </s>
