http: Avoid tornado import before extension is loaded, group all handlers

This commit is contained in:
Stein Magnus Jodal 2014-05-20 22:36:15 +02:00
parent acad8ab7a2
commit ea5a317b00
4 changed files with 17 additions and 17 deletions

View File

@ -3,8 +3,6 @@ from __future__ import unicode_literals
import logging
import os
import tornado.web
import mopidy
from mopidy import config as config_lib, exceptions, ext
@ -42,13 +40,6 @@ class Extension(ext.Extension):
registry.add('frontend', HttpFrontend)
class StaticFileHandler(tornado.web.StaticFileHandler):
def set_extra_headers(self, path):
self.set_header('Cache-Control', 'no-cache')
self.set_header(
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
class Router(object):
"""
HTTP router interface.
@ -104,9 +95,11 @@ class Router(object):
Must return a list of request handlers compatible with
:class:`tornado.web.Application`.
"""
if self.static_file_path is None:
raise ValueError('Undefined static file path in %s' % self)
from mopidy.http.handlers import StaticFileHandler
logger.info(
'Serving HTTP extension %s at %s', type(self), self.get_root_url())
return [

View File

@ -13,7 +13,7 @@ import tornado.websocket
from mopidy import models, zeroconf
from mopidy.core import CoreListener
from mopidy.http import StaticFileHandler, handlers
from mopidy.http import handlers
logger = logging.getLogger(__name__)
@ -54,7 +54,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
static_dir = self.config['http']['static_dir']
# either default mopidy or user defined path to files
primary_dir = (r'/(.*)', StaticFileHandler, {
primary_dir = (r'/(.*)', handlers.StaticFileHandler, {
'path': static_dir if static_dir else mopidy_dir,
'default_filename': 'index.html'
})
@ -69,7 +69,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener):
routes.extend([
(r'/mopidy/ws/?', handlers.WebSocketHandler, {'actor': self}),
(r'/mopidy/rpc', handlers.JsonRpcHandler, {'actor': self}),
(r'/mopidy/(.*)', StaticFileHandler, {
(r'/mopidy/(.*)', handlers.StaticFileHandler, {
'path': mopidy_dir, 'default_filename': 'mopidy.html'
}),
primary_dir,

View File

@ -115,3 +115,10 @@ class JsonRpcHandler(tornado.web.RequestHandler):
self.set_header(
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))
self.set_header('Content-Type', 'application/json; utf-8')
class StaticFileHandler(tornado.web.StaticFileHandler):
def set_extra_headers(self, path):
self.set_header('Cache-Control', 'no-cache')
self.set_header(
'X-Mopidy-Version', mopidy.__version__.encode('utf-8'))

View File

@ -11,6 +11,7 @@ from tornado.web import Application
import mopidy
from mopidy import http
from mopidy.http import handlers
try:
@ -51,11 +52,10 @@ class HttpRouterTest(unittest.TestCase):
def test_default_router(self):
router = TestRouter(self.config)
handlers = router.get_request_handlers()
(pattern, handler_class, kwargs) = handlers[0]
(pattern, handler_class, kwargs) = router.get_request_handlers()[0]
self.assertEqual(pattern, r'/test/(.*)')
self.assertIs(handler_class, http.StaticFileHandler)
self.assertIs(handler_class, handlers.StaticFileHandler)
self.assertEqual(
kwargs['path'], os.path.join(os.path.dirname(__file__), 'static'))
@ -74,7 +74,7 @@ class HttpRouterTest(unittest.TestCase):
class StaticFileHandlerTest(AsyncHTTPTestCase):
def get_app(self):
app = Application([(r'/(.*)', http.StaticFileHandler, {
app = Application([(r'/(.*)', handlers.StaticFileHandler, {
'path': os.path.dirname(__file__),
'default_filename': 'test_router.py'
})])