#NEWLINE# def add_resource( self , name , resource_handler = None , path = None ) : #NEWLINE# #INDENT# "\n        Add a resource handler to the api object.\n        \n        Required Arguments:\n        \n            name\n                The name of the resource.  This is generally the basic name \n                of the resource on the API.  For example '/api/v0/users/' \n                would likely be called 'users' and will be accessible as\n                'api.users' from which additional calls can be made.  For \n                example 'api.users.get()'.  \n        \n        Optional Arguments:\n        \n            resource_handler\n                The resource handler class to use.  Defaults to \n                self._meta.resource_handler.\n            \n            path\n                The path to the resource on the API (after the base url).\n                Defaults to '/<name>/'.\n        \n        \n        Nested Resources:\n            \n        It is possible to attach resources in a 'nested' fashion.  For example\n        passing a name of 'my.nested.users' would be accessible as \n        api.my.nested.users.get().\n        \n        Usage:\n        \n        .. code-block:: python\n        \n            api.add_resource('users')\n            response = api.users.get()\n            \n            # Or for nested resources\n            api.add_resource('my.nested.users', path='/users/')\n            response = api.my.nested.users.get()\n            \n        " #NEWLINE# safe_list = [ '.' , '_' ] #NEWLINE# for char in name : #NEWLINE# #INDENT# if ( char in safe_list ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# if ( not char . isalnum ( ) ) : #NEWLINE# #INDENT# raise exc . dRestResourceError ( 'resource name must be alpha-numeric.' ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not path ) : #NEWLINE# #INDENT# path = ( '%s' % name ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# path = path . strip ( '/' ) #NEWLINE# #UNINDENT# if ( not resource_handler ) : #NEWLINE# #INDENT# resource_handler = self . _meta . resource_handler #NEWLINE# #UNINDENT# resource . validate ( resource_handler ) #NEWLINE# handler = resource_handler ( self , name , path ) #NEWLINE# if hasattr ( self , name ) : #NEWLINE# #INDENT# raise exc . dRestResourceError ( ( "The object '%s' already exist on '%s'" % ( name , self ) ) ) #NEWLINE# #UNINDENT# parts = name . split ( '.' ) #NEWLINE# if ( len ( parts ) == 1 ) : #NEWLINE# #INDENT# setattr ( self , name , handler ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( len ( parts ) > 1 ) : #NEWLINE# #INDENT# first = parts . pop ( 0 ) #NEWLINE# last = parts . pop ( ) #NEWLINE# setattr ( self , first , resource . NestedResource ( ) ) #NEWLINE# first_obj = getattr ( self , first ) #NEWLINE# current_obj = first_obj #NEWLINE# for part in parts : #NEWLINE# #INDENT# setattr ( current_obj , part , resource . NestedResource ( ) ) #NEWLINE# current_obj = getattr ( current_obj , part ) #NEWLINE# #UNINDENT# setattr ( current_obj , last , handler ) #NEWLINE# #UNINDENT# #UNINDENT# self . _resources . append ( name )