pythontornado

Is there a way to print the configured route handlers on a tornado web server internally?


given the following (sample) handlers (taken from here):

handlers = [
            (r"/", MainHandler),
            (r"/auth/login", AuthLoginHandler),
            (r"/auth/logout", AuthLogoutHandler),
        ]

is there a way to programmatically print the handlers on a separate page? I was thinking something along the lines of:

handlers = [
            (r"/", MainHandler),
            (r"/auth/login", AuthLoginHandler),
            (r"/auth/logout", AuthLogoutHandler),
            (r"/routes", RoutePrinter),
        ]

...

class RoutePrinter(...):
    def get(self):
       self.write(str(self.application.routes))

which gives me [(<_sre.SRE_Pattern object at 0x216c130>, [, , , , , , , , , , , , , , , , , , , , , , ])]

I've tried a few different accessors but doesn't really help. Is it possible to generate a list of my routes?

EDIT

Based on further searching, I have come across ways to print a pattern/flag from these regex objects. The problem is that I am having a hard time understanding how to unnest them since it is not as intuitive as self.application.handlers[0][0] and then self.application.handlers[1][0]. The second is "out of range" even though it looks like it should be "/auth/login".

What am I missing?


Solution

  • What about this?

    [handler.regex.pattern for handler in self.application.handlers[0][1]]
    

    If you also want the names of the handler classes:

    [(handler.regex.pattern, handler.handler_class) for handler in self.application.handlers[0][1]]