def decorator(cls): # parent is the parent class of the relation cls_name = cls.__name__.lower() handlers = getattr(self.__class__, "handlers", None) if _rule_re.match(route): ######################################## # new style Werkzeug route ######################################## r = Rule(route, endpoint=cls_name) m = Map() m.add(r) c = m.bind( cfg.server_settings["host"] + ":" + cfg.server_settings["host"], "/") r.compile() #print("r1: " + str(r._regex.pattern)) pattern = r._regex.pattern.replace('^\|', "") #print("r1: " + str(pattern)) fin_route = pattern # convert the HTTP Methods in dispatch to lowercase dispatch_lower = dict( (k.lower(), v) for k, v in dispatch.items()) route_tuple = (fin_route, cls, dispatch_lower) handlers.append((route_tuple, pos)) else: ################################### # old style regex route ################################### # BETA: this regex is added to every route to make # 1.the slash at the end optional # 2.it possible to add a .format paramter. # Example: route = /test -> also test.json will work # Example: route = /test/([0-9]+) -> also /test/12.xml will work if cfg.beta_settings["dot_format"]: fin_route = route + r"(?:/?\.\w+)?/?" else: fin_route = route # convert the HTTP Methods in dispatch to lowercase dispatch_lower = dict( (k.lower(), v) for k, v in dispatch.items()) route_tuple = (fin_route, cls, dispatch_lower) #route_tuple = (fin_route,cls, dispatch) handlers.append((route_tuple, pos)) #print("handlers: " + str(self.handlers)) #print("ROUTING: added route for: " + cls.__name__ + ": " + route + " -> " + fin_route + " dispatch") print( "STD ROUTE (+) : handler: {}, route: {}, fin_route: {}, dispatch(lower): {} " .format(str(cls.__name__), route, fin_route, str(dispatch_lower))) return cls
def decorator(cls): # # first, check all methods for the method decorator mark # #for name, method in cls.__dict__.iteritems(): # if hasattr(method, "has_route"): # # do something with the method and class # print("Method route: {}, {}".format(name, str(cls))) # parent is the parent class of the relation cls_name = cls.__name__.lower() #print("in @app.route") for name, method in cls.__dict__.items(): if hasattr(method, "routed"): #print(30*"--") #print(" routed Method ") #print(" * name: {}, method: {}". format(name,method)) #print(" ** routes: {}".format(str(method.route))) #print(30*"--") # Create the route parameters from the route marked by @route # route, dispatch, pos route=method.route.get("route", None) # construct the dispatch dict from the http_vers list # ["get"] => # { "get" : method_name } # dispatch = {key: name for key in method.route.get("dispatch", [])} pos = method.route.get("pos", -1) # now just do the same as for the class decorator handlers=getattr(self.__class__, "handlers", None) if _rule_re.match(route): ######################################## # new style Werkzeug route ######################################## r=Rule(route, endpoint=cls_name) m = Map() m.add(r) c=m.bind(cfg.server_settings["host"]+":"+cfg.server_settings["host"], "/") r.compile() #print("r1: " + str(r._regex.pattern)) pattern = r._regex.pattern.replace('^\|', "") #print("r1: " + str(pattern)) fin_route = pattern # convert the HTTP Methods in dispatch to lowercase dispatch_lower=dict((k.lower(), v) for k,v in dispatch.items()) route_tuple = (fin_route,cls, dispatch_lower) handlers.append((route_tuple,pos)) else: ################################### # old style regex route ################################### # BETA: this regex is added to every route to make # 1.the slash at the end optional # 2.it possible to add a .format paramter. # Example: route = /test -> also test.json will work # Example: route = /test/([0-9]+) -> also /test/12.xml will work if cfg.beta_settings["dot_format"]: fin_route = route + r"(?:/?\.\w+)?/?" else: fin_route = route # convert the HTTP Methods in dispatch to lowercase dispatch_lower=dict((k.lower(), v) for k,v in dispatch.items()) route_tuple = (fin_route,cls, dispatch_lower) #route_tuple = (fin_route,cls, dispatch) handlers.append((route_tuple,pos)) #print("handlers: " + str(self.handlers)) #print("ROUTING: added route for: " + cls.__name__ + ": " + route + " -> " + fin_route + " dispatch") #print("ROUTING: METHOD ROUTE (+) : handler: {}, route: {}, fin_route: {}, dispatch(lower): {} ".format( # str(cls.__name__), route, fin_route, str(dispatch_lower))) print("ROUTING: METHOD ROUTE (+) : route: {:30} handler: {:20} dispatch: {:15} ".format( route, str(cls.__name__), str(list(dispatch_lower.keys())))) return cls
def is_werkzeug_route(self, route): """ does it look like a werkzeug route or direct reg exp. of tornado. """ return _rule_re.match(route)