Commit 38cb1a57 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

working on multi Resource support

parent 61c4994d
BSD License
--------------------------------------------------------------------
Copyright (c) 2011 Jacek Furmankiewicz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\ No newline at end of file
'''
A CorePost submodule that can be merged into the main CorePost Resource
'''
from corepost.web import CorePost
from corepost.enums import Http
submodule = CorePost()
submodule.isLeaf = True
@submodule.route("/test",Http.GET)
def submodule_get(request,**kwargs):
return request.path
\ No newline at end of file
...@@ -8,6 +8,7 @@ from corepost.enums import Http ...@@ -8,6 +8,7 @@ from corepost.enums import Http
from twisted.internet import defer from twisted.internet import defer
app = CorePost() app = CorePost()
app.isLeaf = True
@app.route("/",Http.GET) @app.route("/",Http.GET)
@defer.inlineCallbacks @defer.inlineCallbacks
...@@ -29,4 +30,7 @@ def test_post(request,**kwargs): ...@@ -29,4 +30,7 @@ def test_post(request,**kwargs):
return "%s" % kwargs return "%s" % kwargs
if __name__ == '__main__': if __name__ == '__main__':
# hookup submodule
#from submodule_test import submodule
app.putChild("submodule", submodule)
app.run() app.run()
\ No newline at end of file
...@@ -99,6 +99,7 @@ class CorePost(Resource): ...@@ -99,6 +99,7 @@ class CorePost(Resource):
''' '''
Constructor Constructor
''' '''
Resource.__init__(self)
self.__urls = defaultdict(dict) self.__urls = defaultdict(dict)
self.__cachedUrls = defaultdict(dict) self.__cachedUrls = defaultdict(dict)
self.__methods = {} self.__methods = {}
...@@ -140,17 +141,19 @@ class CorePost(Resource): ...@@ -140,17 +141,19 @@ class CorePost(Resource):
def __renderUrl(self,request): def __renderUrl(self,request):
"""Finds the appropriate router and dispatches the request to the registered function""" """Finds the appropriate router and dispatches the request to the registered function"""
# see if already cached # see if already cached
path = '/' + '/'.join(request.postpath)
urlrouter, pathargs = None, None urlrouter, pathargs = None, None
if request.path in self.__cachedUrls[request.method]: if path in self.__cachedUrls[request.method]:
cachedUrl = self.__cachedUrls[request.method][request.path] cachedUrl = self.__cachedUrls[request.method][path]
urlrouter,pathargs = cachedUrl.router, cachedUrl.args urlrouter,pathargs = cachedUrl.router, cachedUrl.args
else: else:
# first time this URL is called # first time this URL is called
for router in self.__urls[request.method].values(): for router in self.__urls[request.method].values():
args = router.getArguments(request.path) args = router.getArguments(path)
if args != None: if args != None:
if router.cache: if router.cache:
self.__cachedUrls[request.method][request.path] = CachedUrl(router, args) self.__cachedUrls[request.method][path] = CachedUrl(router, args)
urlrouter,pathargs = router,args urlrouter,pathargs = router,args
#actual call #actual call
...@@ -175,6 +178,9 @@ class CorePost(Resource): ...@@ -175,6 +178,9 @@ class CorePost(Resource):
return val return val
else: else:
# could be part of a submodule
return self.__renderError(request,404,"URL '%s' not found\n" % request.path) return self.__renderError(request,404,"URL '%s' not found\n" % request.path)
def __renderError(self,request,code,message): def __renderError(self,request,code,message):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment