Created
July 31, 2012 08:36
-
-
Save ugurcan377/3215031 to your computer and use it in GitHub Desktop.
Simple Routing API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
logger = logging.getLogger(__name__) | |
from spyne.model.fault import Fault | |
from werkzeug.routing import Map | |
from werkzeug.routing import Rule | |
class UrlMapNotBinded(Fault): | |
"""Raised when url_map is not binded to a wsgi environment""" | |
def __init__(self, faultstring="Please bind url_map first"): | |
Fault.__init__(self, 'Server.UrlMapNotBinded', faultstring) | |
class HttpRouter(object): | |
"""This class provides some basic functions for supporting url-routing""" | |
def __init__(self): | |
self.url_map = Map() | |
self.urls = None | |
def add_rule(self, url, end_point, method=None): | |
"""This function adds a url pattern to url map""" | |
if method: | |
self.url_map.add(Rule(url, endpoint=end_point, methods=method)) | |
else: | |
self.url_map.add(Rule(url, endpoint=end_point, methods=method)) | |
def bind(self, environ): | |
"""This function binds url map to a wsgi environment""" | |
self.urls = self.url_map.bind("foo.org","/") | |
# self.urls = self.url_map.bind_to_environ(environ) | |
return self.urls | |
def is_binded(self): | |
return self.urls != None | |
def build(self, end_point, method_=None, **kwargs): | |
"""This function generates a url from previous given patterns""" | |
print method_ | |
if self.urls: | |
if method_: | |
self.urls.build(end_point, method=method_, append_unknown=False) | |
else: | |
self.urls.build(end_point, kwargs, append_unknown=False) | |
else: | |
raise UrlMapNotBinded | |
def match(self, url, method_=None): | |
"""This function matches given url to one of patterns in url map""" | |
if self.urls: | |
if method_: | |
return self.urls.match(url, method=method_) | |
else: | |
return self.urls.match(url) | |
else: | |
raise UrlMapNotBinded |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment