def register(handlers=None, subdomain=None, parent_dir=None, urls=None):
'''
Register additional handlers to the server.
:param handlers: List of handler classes to register.
:param subdomain: The desired subdomain
:param parent_dir: The directory under which all the handlers should be
placed
:param urls: List of lists like [['path', Handler]].
Overwrites handlers input.
'''
# parse input
if subdomain is None:
subdomain = "[A-Za-z0-9.]*"
else:
subdomain = r'{}\.[A-Za-z0-9.]*'.format(subdomain)
if parent_dir is None:
parent_dir = ''
else:
parent_dir = '/{}'.format(parent_dir)
# create proper URLSpec handlers and callback
if not urls:
def doc_url(handler):
""" reads the url regexp from the handler's docstring. """
docs = handler.__doc__
pieces = docs.strip().split('\n\n')[0].split('\n')
return ''.join([piece.strip() for piece in pieces])
handlers = [tornado.web.URLSpec('{}{}'.format(parent_dir,
doc_url(handler)),
handler)
for handler in handlers]
else:
handlers = urls
def add_handler_callback(server, subdmn, hndls):
""" add handler to server """
server.add_handlers(subdmn, hndls)
# schedule handler addition to next IOLoop iteration
ioloop = tornado.ioloop.IOLoop.instance()
ioloop.add_callback(add_handler_callback, SERVER, subdomain, handlers)
评论列表
文章目录