Use setter and getter for core instance (feels uncomfortable, but it works)

This commit is contained in:
James Barnsley
2019-11-23 14:08:47 +13:00
parent 2f78d03782
commit d2ca54bae6
5 changed files with 27 additions and 28 deletions

View File

@ -9,6 +9,7 @@ from mopidy import config, ext
from .frontend import IrisFrontend from .frontend import IrisFrontend
from .handlers import WebsocketHandler, HttpHandler from .handlers import WebsocketHandler, HttpHandler
from .core import IrisCore from .core import IrisCore
from .mem import setIris, getIris
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
__version__ = '3.42.2' __version__ = '3.42.2'
@ -48,8 +49,8 @@ class Extension( ext.Extension ):
}) })
# create our core instance # create our core instance
mem.iris = IrisCore() setIris(IrisCore())
mem.iris.version = self.version getIris().version = self.version
# Add our frontend # Add our frontend
registry.add('frontend', IrisFrontend) registry.add('frontend', IrisFrontend)
@ -77,13 +78,6 @@ def iris_factory(config, core):
path = pathlib.Path(__file__).parent / "static" path = pathlib.Path(__file__).parent / "static"
return [ return [
(
r"/images/(.*)",
tornado.web.StaticFileHandler,
{
'path': config['local-images']['image_dir']
}
),
( (
r'/http/([^/]*)', r'/http/([^/]*)',
handlers.HttpHandler, handlers.HttpHandler,

View File

@ -4,7 +4,7 @@ from mopidy.core import CoreListener
import pykka import pykka
import logging import logging
from .mem import mem from .mem import getIris
# import logger # import logger
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -13,17 +13,17 @@ class IrisFrontend(pykka.ThreadingActor, CoreListener):
def __init__(self, config, core): def __init__(self, config, core):
super(IrisFrontend, self).__init__() super(IrisFrontend, self).__init__()
mem.iris.core = core getIris().core = core
mem.iris.config = config getIris().config = config
def on_start(self): def on_start(self):
mem.iris.start() getIris().start()
def on_stop(self): def on_stop(self):
mem.iris.stop() getIris().stop()
def track_playback_ended(self, tl_track, time_position): def track_playback_ended(self, tl_track, time_position):
mem.iris.check_for_radio_update() getIris().check_for_radio_update()
def tracklist_changed(self): def tracklist_changed(self):
mem.iris.clean_queue_metadata() getIris().clean_queue_metadata()

View File

@ -5,7 +5,7 @@ from tornado.escape import json_encode, json_decode
import tornado.ioloop, tornado.web, tornado.websocket, tornado.template import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
import random, string, logging, uuid, subprocess, pykka, ast, logging, json, urllib, requests, time import random, string, logging, uuid, subprocess, pykka, ast, logging, json, urllib, requests, time
from .mem import mem from .mem import getIris
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -28,14 +28,14 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
# Construct our initial client object, and add to our list of connections # Construct our initial client object, and add to our list of connections
client = { client = {
'connection_id': mem.iris.generateGuid(), 'connection_id': getIris().generateGuid(),
'ip': ip, 'ip': ip,
'created': datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S') 'created': datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
} }
self.connection_id = client['connection_id'] self.connection_id = client['connection_id']
mem.iris.add_connection(connection=self, client=client) getIris().add_connection(connection=self, client=client)
def on_message(self, message): def on_message(self, message):
@ -65,9 +65,9 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
if 'method' in message: if 'method' in message:
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, message['method']): if hasattr(iris, message['method']):
try: try:
getattr(mem.iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_result(id=id, method=message['method'], response=response, error=error)) getattr(iris, message['method'])(data=params, callback=lambda response, error=False: self.handle_result(id=id, method=message['method'], response=response, error=error))
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
@ -80,7 +80,7 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
def on_close(self): def on_close(self):
mem.iris.remove_connection(connection_id=self.connection_id) getIris().remove_connection(connection_id=self.connection_id)
## ##
# Handle a response from our core # Handle a response from our core
@ -114,7 +114,7 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
# Respond to the original request # Respond to the original request
data = request_response data = request_response
data['recipient'] = self.connection_id data['recipient'] = self.connection_id
mem.iris.send_message(data=data) getIris().send_message(data=data)
@ -142,9 +142,9 @@ class HttpHandler(tornado.web.RequestHandler):
id = int(time.time()) id = int(time.time())
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, slug): if hasattr(iris, slug):
try: try:
getattr(mem.iris, slug)(request=self, callback=lambda response, error=False: self.handle_result(id=id, method=slug, response=response, error=error)) getattr(iris, slug)(request=self, callback=lambda response, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
@ -163,9 +163,9 @@ class HttpHandler(tornado.web.RequestHandler):
return return
# make sure the method exists # make sure the method exists
if hasattr(mem.iris, slug): if hasattr(iris, slug):
try: try:
getattr(mem.iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_result(id=id, method=slug, response=response, error=error)) getattr(iris, slug)(data=params, request=self.request, callback=lambda response=False, error=False: self.handle_result(id=id, method=slug, response=response, error=error))
except HTTPError as e: except HTTPError as e:
self.handle_result(id=id, error={'code': 32601, 'message': "Invalid JSON payload"}) self.handle_result(id=id, error={'code': 32601, 'message': "Invalid JSON payload"})

View File

@ -1,3 +1,9 @@
iris = None iris = None
def setIris(irisCore):
global iris
iris = irisCore
def getIris():
return iris

View File

@ -22,7 +22,6 @@ setup(
install_requires=[ install_requires=[
'setuptools >= 3.3', 'setuptools >= 3.3',
'Mopidy >= 2.0', 'Mopidy >= 2.0',
'Mopidy-Local-Images >= 1.0',
'ConfigObj >= 5.0.6', 'ConfigObj >= 5.0.6',
'requests >= 2.0.0' 'requests >= 2.0.0'
], ],