"""
This is router class
"""

import re
from message import trim_resource

def compile_resource(resource):
    """
    Return compiled regex for resource matching
    """
    return re.compile("^" + trim_resource(re.sub(r":(\w+)", r"(?P<\1>\w+?)", resource)) +
        r"(\?(?P<querystring>.*))?$")


class Route(object):
    """
    Route class
    """
    def __init__(self, resource):

        # syntax: /network/cellular/:id
        self.resource = trim_resource(resource)
        self.resource_regex = compile_resource(resource)
        self.handlers = []
        
        for method in ["get", "post", "put", "delete", "all"]:
            self.__setattr__(method, self.create_handler_func(method))

    def create_handler_func(self, method):

        def _handler(callback):
            # reentrant default is False [POST, DELETE, PUT]
            reentrant = False
            if method == "get":
                reentrant = True

            self.handlers.append({
                "method": method,
                "callback": callback,
                "reentrant": reentrant
            })
            return self

        return _handler

    def dispatch(self, message):
        callbacks = []
        for handler in self.handlers:
            if handler["method"] != message.method:
                continue

            callbacks.append(handler["callback"])
        return callbacks


class Router(object):
    """
    Router class
    """
    def __init__(self):
        self.routes = {}
        for method in ["get", "post", "put", "delete", "all"]:
            self.__setattr__(method, self.create_route_func(method=method))

    def route(self, resource):
        route = self.routes.get(resource, Route(resource))
        self.routes.update({resource: route})
        # self.routes.append(route)

        return route

    def create_route_func(self, method):
        def _route(resource, handler):
            route = self.routes.get(resource, Route(resource))
            route.__getattribute__(method)(handler)
            self.routes.update({resource: route})
            # self.routes.append(route)
            return self

        return _route

    def dispatch(self, message):
        results = []
        # match routes
        for resource, route in self.routes.items():
            __message = message.match(route)
            if __message == None:
                continue
            results.append({
                "callbacks": route.dispatch(__message),
                "message": __message
            })

        return results

@@ -381,17 +381,16 @@
 ource):%0A
-%0A
         
@@ -744,40 +744,144 @@
 d):%0A
-%0A        def _handler(callback):
+        %22%22%22%0A        create_handler_func%0A        %22%22%22%0A        def _handler(callback):%0A            %22%22%22%0A            _handler%0A            %22%22%22
 %0A   
@@ -1270,32 +1270,73 @@
 self, message):%0A
+        %22%22%22%0A        dispatch%0A        %22%22%22%0A
         callback
@@ -1802,32 +1802,70 @@
 elf, resource):%0A
+        %22%22%22%0A        route%0A        %22%22%22%0A
         route = 
@@ -1957,45 +1957,8 @@
 e%7D)%0A
-        # self.routes.append(route)%0A%0A
     
@@ -2028,38 +2028,139 @@
     
-def _route(resource, handler):
+%22%22%22%0A        create_route_func%0A        %22%22%22%0A        def _route(resource, handler):%0A            %22%22%22%0A            _route%0A            %22%22%22
 %0A   
@@ -2325,48 +2325,8 @@
 e%7D)%0A
-            # self.routes.append(route)%0A
     
@@ -2394,32 +2394,73 @@
 self, message):%0A
+        %22%22%22%0A        dispatch%0A        %22%22%22%0A
         results 
@@ -2643,16 +2643,135 @@
 ontinue%0A
+%0A            route_result = route.dispatch(__message)%0A            if len(route_result) == 0:%0A                continue%0A%0A
         
@@ -2825,36 +2825,23 @@
 %22: route
-.dispatch(__message)
+_result
 ,%0A      

Update router.py