Commit 1e4b1c51 authored by Jacek Furmankiewicz's avatar Jacek Furmankiewicz

0.0.13: perf fix to avoid string concatenation in URL routing

parent 64af036f
...@@ -26,7 +26,7 @@ class UrlRouter: ...@@ -26,7 +26,7 @@ class UrlRouter:
''' Common class for containing info related to routing a request to a function ''' ''' Common class for containing info related to routing a request to a function '''
__urlMatcher = re.compile(r"<(int|float|):?([a-zA-Z0-9]+)>") __urlMatcher = re.compile(r"<(int|float|):?([a-zA-Z0-9]+)>")
__urlRegexReplace = {"":r"(?P<arg>.+)","int":r"(?P<arg>\d+)","float":r"(?P<arg>\d+.?\d*)"} __urlRegexReplace = {"":r"(?P<arg>([a-zA-Z0-9]+))","int":r"(?P<arg>\d+)","float":r"(?P<arg>\d+.?\d*)"}
__typeConverters = {"int":int,"float":float} __typeConverters = {"int":int,"float":float}
def __init__(self,f,url,methods,accepts,produces,cache): def __init__(self,f,url,methods,accepts,produces,cache):
...@@ -187,10 +187,11 @@ class RequestRouter: ...@@ -187,10 +187,11 @@ class RequestRouter:
# if specified, add class path to each function's path # if specified, add class path to each function's path
rq = func.corepostRequestRouter rq = func.corepostRequestRouter
rq.url = "%s%s" % (rootPath,rq.url) rq.url = "%s%s" % (rootPath,rq.url)
# remove trailing '/' to standardize URLs # remove first and trailing '/' to standardize URLs
if rq.url != "/" and rq.url[-1] == "/": start = 1 if rq.url[0:1] == "/" else 0
rq.url = rq.url[:-1] end = -1 if rq.url[len(rq.url) -1] == '/' else len(rq.url)
rq.url = rq.url[start:end]
# now that the full URL is set, compile the matcher for it # now that the full URL is set, compile the matcher for it
rq.compileMatcherForFullUrl() rq.compileMatcherForFullUrl()
...@@ -199,6 +200,7 @@ class RequestRouter: ...@@ -199,6 +200,7 @@ class RequestRouter:
urlRouterInstance = UrlRouterInstance(service,rq) urlRouterInstance = UrlRouterInstance(service,rq)
self.__urls[method][rq.url][accepts] = urlRouterInstance self.__urls[method][rq.url][accepts] = urlRouterInstance
self.__urlRouterInstances[func] = urlRouterInstance # needed so that we can lookup the urlRouterInstance for a specific function self.__urlRouterInstances[func] = urlRouterInstance # needed so that we can lookup the urlRouterInstance for a specific function
def getResponse(self,request): def getResponse(self,request):
"""Finds the appropriate instance and dispatches the request to the registered function. Returns the appropriate Response object""" """Finds the appropriate instance and dispatches the request to the registered function. Returns the appropriate Response object"""
...@@ -210,8 +212,8 @@ class RequestRouter: ...@@ -210,8 +212,8 @@ class RequestRouter:
# standardize URL and remove trailing "/" if necessary # standardize URL and remove trailing "/" if necessary
standardized_postpath = request.postpath if (request.postpath[-1] != '' or request.postpath == ['']) else request.postpath[:-1] standardized_postpath = request.postpath if (request.postpath[-1] != '' or request.postpath == ['']) else request.postpath[:-1]
path = '/' + '/'.join(standardized_postpath) path = '/'.join(standardized_postpath)
contentType = MediaType.WILDCARD if HttpHeader.CONTENT_TYPE not in request.received_headers else request.received_headers[HttpHeader.CONTENT_TYPE] contentType = MediaType.WILDCARD if HttpHeader.CONTENT_TYPE not in request.received_headers else request.received_headers[HttpHeader.CONTENT_TYPE]
urlRouterInstance, pathargs = None, None urlRouterInstance, pathargs = None, None
......
...@@ -101,7 +101,8 @@ Links ...@@ -101,7 +101,8 @@ Links
Changelog Changelog
````````` `````````
* 0.0.13:
- perf fix to avoid unnecessary string concatenation when doing URL routing, after code review (thanks to Gerald Tremblay)
* 0.0.12: * 0.0.12:
- backwards incompatible change: added advanced URL routing for nested REST services. - backwards incompatible change: added advanced URL routing for nested REST services.
CorePost object is gone, REST services are now just standard classes. CorePost object is gone, REST services are now just standard classes.
...@@ -131,7 +132,7 @@ from setuptools import setup ...@@ -131,7 +132,7 @@ from setuptools import setup
setup( setup(
name="CorePost", name="CorePost",
version="0.0.12", version="0.0.13",
author="Jacek Furmankiewicz", author="Jacek Furmankiewicz",
author_email="jacek99@gmail.com", author_email="jacek99@gmail.com",
description=("A Twisted Web REST micro-framework"), description=("A Twisted Web REST micro-framework"),
......
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