Use setter and getter for core instance (feels uncomfortable, but it works)
This commit is contained in:
@ -9,6 +9,7 @@ from mopidy import config, ext
|
||||
from .frontend import IrisFrontend
|
||||
from .handlers import WebsocketHandler, HttpHandler
|
||||
from .core import IrisCore
|
||||
from .mem import setIris, getIris
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
__version__ = '3.42.2'
|
||||
@ -48,8 +49,8 @@ class Extension( ext.Extension ):
|
||||
})
|
||||
|
||||
# create our core instance
|
||||
mem.iris = IrisCore()
|
||||
mem.iris.version = self.version
|
||||
setIris(IrisCore())
|
||||
getIris().version = self.version
|
||||
|
||||
# Add our frontend
|
||||
registry.add('frontend', IrisFrontend)
|
||||
@ -77,13 +78,6 @@ def iris_factory(config, core):
|
||||
path = pathlib.Path(__file__).parent / "static"
|
||||
|
||||
return [
|
||||
(
|
||||
r"/images/(.*)",
|
||||
tornado.web.StaticFileHandler,
|
||||
{
|
||||
'path': config['local-images']['image_dir']
|
||||
}
|
||||
),
|
||||
(
|
||||
r'/http/([^/]*)',
|
||||
handlers.HttpHandler,
|
||||
|
||||
@ -4,7 +4,7 @@ from mopidy.core import CoreListener
|
||||
|
||||
import pykka
|
||||
import logging
|
||||
from .mem import mem
|
||||
from .mem import getIris
|
||||
|
||||
# import logger
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -13,17 +13,17 @@ class IrisFrontend(pykka.ThreadingActor, CoreListener):
|
||||
|
||||
def __init__(self, config, core):
|
||||
super(IrisFrontend, self).__init__()
|
||||
mem.iris.core = core
|
||||
mem.iris.config = config
|
||||
getIris().core = core
|
||||
getIris().config = config
|
||||
|
||||
def on_start(self):
|
||||
mem.iris.start()
|
||||
getIris().start()
|
||||
|
||||
def on_stop(self):
|
||||
mem.iris.stop()
|
||||
getIris().stop()
|
||||
|
||||
def track_playback_ended(self, tl_track, time_position):
|
||||
mem.iris.check_for_radio_update()
|
||||
getIris().check_for_radio_update()
|
||||
|
||||
def tracklist_changed(self):
|
||||
mem.iris.clean_queue_metadata()
|
||||
getIris().clean_queue_metadata()
|
||||
|
||||
@ -5,7 +5,7 @@ from tornado.escape import json_encode, json_decode
|
||||
import tornado.ioloop, tornado.web, tornado.websocket, tornado.template
|
||||
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__)
|
||||
|
||||
@ -28,14 +28,14 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
|
||||
# Construct our initial client object, and add to our list of connections
|
||||
client = {
|
||||
'connection_id': mem.iris.generateGuid(),
|
||||
'connection_id': getIris().generateGuid(),
|
||||
'ip': ip,
|
||||
'created': datetime.strftime(datetime.now(), '%Y-%m-%d %H:%M:%S')
|
||||
}
|
||||
|
||||
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):
|
||||
@ -65,9 +65,9 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
if 'method' in message:
|
||||
|
||||
# make sure the method exists
|
||||
if hasattr(mem.iris, message['method']):
|
||||
if hasattr(iris, message['method']):
|
||||
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:
|
||||
logger.error(str(e))
|
||||
|
||||
@ -80,7 +80,7 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
|
||||
|
||||
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
|
||||
@ -114,7 +114,7 @@ class WebsocketHandler(tornado.websocket.WebSocketHandler):
|
||||
# Respond to the original request
|
||||
data = request_response
|
||||
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())
|
||||
|
||||
# make sure the method exists
|
||||
if hasattr(mem.iris, slug):
|
||||
if hasattr(iris, slug):
|
||||
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:
|
||||
logger.error(str(e))
|
||||
|
||||
@ -163,9 +163,9 @@ class HttpHandler(tornado.web.RequestHandler):
|
||||
return
|
||||
|
||||
# make sure the method exists
|
||||
if hasattr(mem.iris, slug):
|
||||
if hasattr(iris, slug):
|
||||
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:
|
||||
self.handle_result(id=id, error={'code': 32601, 'message': "Invalid JSON payload"})
|
||||
|
||||
@ -1,3 +1,9 @@
|
||||
|
||||
iris = None
|
||||
|
||||
def setIris(irisCore):
|
||||
global iris
|
||||
iris = irisCore
|
||||
|
||||
def getIris():
|
||||
return iris
|
||||
Reference in New Issue
Block a user